diff --git a/apps/develop/api/table.go b/apps/develop/api/table.go index efc33ff..217a789 100644 --- a/apps/develop/api/table.go +++ b/apps/develop/api/table.go @@ -132,7 +132,7 @@ func (g *GenTableApi) Insert(rc *ctx.ReqCtx) { // @Tags 工具 / 生成工具 // @Accept application/json // @Product application/json -// @Param data body tools.SysTables true "body" +// @Param data body entity.DevGenTable true "body" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": -1, "message": "添加失败"}" // @Router /develop/code/table [put] diff --git a/apps/resource/api/email.go b/apps/resource/api/email.go new file mode 100644 index 0000000..534526f --- /dev/null +++ b/apps/resource/api/email.go @@ -0,0 +1,157 @@ +package api + +import ( + "pandax/apps/resource/api/from" + "pandax/apps/resource/entity" + "pandax/apps/resource/services" + "pandax/base/biz" + "pandax/base/ctx" + "pandax/base/ginx" + email "pandax/base/mail" + "pandax/base/utils" +) + +/** + * @Description 添加qq群467890197 交流学习 + * @Author 熊猫 + * @Date 2022/1/14 15:23 + **/ + +type ResEmailsApi struct { + ResEmailsApp services.ResEmailsModel +} + +// @Summary ResEmails列表数据 +// @Tags ResEmails +// @Param pageSize query int false "页条数" +// @Param pageNum query int false "页码" +// @Param status query string false "状态" +// @Param category query string false "分类" +// @Success 200 {string} string "{"code": 200, "data": [...]}" +// @Router /resource/email/list [get] +// @Security +func (p *ResEmailsApi) GetResEmailsList(rc *ctx.ReqCtx) { + + pageNum := ginx.QueryInt(rc.GinCtx, "pageNum", 1) + pageSize := ginx.QueryInt(rc.GinCtx, "pageSize", 10) + status := rc.GinCtx.Query("status") + category := rc.GinCtx.Query("category") + + data := entity.ResEmail{Status: status, Category: category} + list, total := p.ResEmailsApp.FindListPage(pageNum, pageSize, data) + li := *list + for i, data := range li { + data.From = utils.DdmMail(data.From) + data.Secret = utils.DdmPassword(data.Secret) + li[i] = data + } + rc.ResData = map[string]interface{}{ + "data": list, + "total": total, + "pageNum": pageNum, + "pageSize": pageSize, + } +} + +// @Summary 获取ResEmails +// @Description 获取JSON +// @Tags ResEmails +// @Param mailId path int true "mailId" +// @Success 200 {string} string "{"code": 200, "data": [...]}" +// @Router /resource/email/{mailId }[get] +// @Security +func (p *ResEmailsApi) GetResEmails(rc *ctx.ReqCtx) { + mailId := ginx.PathParamInt(rc.GinCtx, "mailId") + p.ResEmailsApp.FindOne(int64(mailId)) +} + +// @Summary 添加ResEmails +// @Description 获取JSON +// @Tags ResEmails +// @Accept application/json +// @Product application/json +// @Param data body entity.ResEmail true "data" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": 400, "message": "添加失败"}" +// @Router /resource/email [post] +// @Security X-TOKEN +func (p *ResEmailsApi) InsertResEmails(rc *ctx.ReqCtx) { + var data entity.ResEmail + ginx.BindJsonAndValid(rc.GinCtx, &data) + + p.ResEmailsApp.Insert(data) +} + +// @Summary 修改ResEmails +// @Description 获取JSON +// @Tags ResEmails +// @Accept application/json +// @Product application/json +// @Param data body entity.ResEmail true "body" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": 400, "message": "添加失败"}" +// @Router /resource/email [put] +// @Security X-TOKEN +func (p *ResEmailsApi) UpdateResEmails(rc *ctx.ReqCtx) { + var data entity.ResEmail + ginx.BindJsonAndValid(rc.GinCtx, &data) + + p.ResEmailsApp.Update(data) +} + +// @Summary 删除ResEmails +// @Description 删除数据 +// @Tags ResEmails +// @Param mailId path string true "mailId" +// @Success 200 {string} string "{"code": 200, "message": "删除成功"}" +// @Success 200 {string} string "{"code": 400, "message": "删除失败"}" +// @Router /resource/email/{mailId } [delete] +func (p *ResEmailsApi) DeleteResEmails(rc *ctx.ReqCtx) { + + mailId := rc.GinCtx.Param("mailId") + mailIds := utils.IdsStrToIdsIntGroup(mailId) + p.ResEmailsApp.Delete(mailIds) +} + +// @Summary 删除ResEmails +// @Description 获取JSON +// @Tags ResOsses +// @Accept application/json +// @Product application/json +// @Param data body entity.ResEmail true "body" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": 400, "message": "添加失败"}" +// @Router /resource/oss [put] +// @Security X-TOKEN +func (p *ResEmailsApi) UpdateMailStatus(rc *ctx.ReqCtx) { + var data entity.ResEmail + ginx.BindJsonAndValid(rc.GinCtx, &data) + + p.ResEmailsApp.Update(entity.ResEmail{MailId: data.MailId, Status: data.Status}) +} + +// @Summary 测试发邮件 +// @Description 获取JSON +// @Tags ResEmails +// @Accept application/json +// @Product application/json +// @Param data body from.SendMail true "data" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": 400, "message": "添加失败"}" +// @Router /resource/email/debugMail [post] +// @Security X-TOKEN +func (p *ResEmailsApi) DebugMail(rc *ctx.ReqCtx) { + var data from.SendMail + ginx.BindJsonAndValid(rc.GinCtx, &data) + + one := p.ResEmailsApp.FindOne(data.MailId) + ml := email.Mail{ + Host: one.Host, + Port: one.Port, + From: one.From, + Nickname: one.Nickname, + Secret: one.Secret, + IsSSL: one.IsSSL, + } + biz.ErrIsNil(ml.Email(data.To, email.TEXTTYPE, data.Subject, data.Body), "邮件发送失败") +} diff --git a/apps/resource/api/from/mail.go b/apps/resource/api/from/mail.go new file mode 100644 index 0000000..1096938 --- /dev/null +++ b/apps/resource/api/from/mail.go @@ -0,0 +1,14 @@ +package from + +/** + * @Description 添加qq群467890197 交流学习 + * @Author 熊猫 + * @Date 2022/1/14 16:53 + **/ + +type SendMail struct { + MailId int64 `json:"mailId"` + To string `json:"to"` + Subject string `json:"subject"` + Body string `json:"body"` +} diff --git a/apps/resource/api/oss.go b/apps/resource/api/oss.go index cd4385a..9928561 100644 --- a/apps/resource/api/oss.go +++ b/apps/resource/api/oss.go @@ -1,11 +1,15 @@ package api import ( + "fmt" "pandax/apps/resource/entity" "pandax/apps/resource/services" + "pandax/base/biz" "pandax/base/ctx" "pandax/base/ginx" + "pandax/base/oss" "pandax/base/utils" + "time" ) /** @@ -22,8 +26,11 @@ type ResOssesApi struct { // @Tags ResOsses // @Param pageSize query int false "页条数" // @Param pageNum query int false "页码" +// @Param status query string false "状态" +// @Param category query string false "分类" +// @Param ossCode query string false "编号" // @Success 200 {string} string "{"code": 200, "data": [...]}" -// @Router /admin/resOsses/list [get] +// @Router /resource/oss/list [get] // @Security func (p *ResOssesApi) GetResOssesList(rc *ctx.ReqCtx) { @@ -37,8 +44,8 @@ func (p *ResOssesApi) GetResOssesList(rc *ctx.ReqCtx) { list, total := p.ResOssesApp.FindListPage(pageNum, pageSize, data) li := *list for i, data := range li { - data.AccessKey = utils.Ddm(data.AccessKey) - data.SecretKey = utils.Ddm(data.SecretKey) + data.AccessKey = utils.DdmKey(data.AccessKey) + data.SecretKey = utils.DdmKey(data.SecretKey) li[i] = data } rc.ResData = map[string]interface{}{ @@ -54,7 +61,7 @@ func (p *ResOssesApi) GetResOssesList(rc *ctx.ReqCtx) { // @Tags ResOsses // @Param ossId path int true "ossId" // @Success 200 {string} string "{"code": 200, "data": [...]}" -// @Router /admin/resOsses/{ossId }[get] +// @Router /resource/oss/{ossId }[get] // @Security func (p *ResOssesApi) GetResOsses(rc *ctx.ReqCtx) { ossId := ginx.PathParamInt(rc.GinCtx, "ossId") @@ -66,10 +73,10 @@ func (p *ResOssesApi) GetResOsses(rc *ctx.ReqCtx) { // @Tags ResOsses // @Accept application/json // @Product application/json -// @Param data body entity.ResOsses true "data" +// @Param data body entity.ResOss true "data" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": 400, "message": "添加失败"}" -// @Router /admin/resOsses [post] +// @Router /resource/oss [post] // @Security X-TOKEN func (p *ResOssesApi) InsertResOsses(rc *ctx.ReqCtx) { var data entity.ResOss @@ -83,10 +90,10 @@ func (p *ResOssesApi) InsertResOsses(rc *ctx.ReqCtx) { // @Tags ResOsses // @Accept application/json // @Product application/json -// @Param data body entity.ResOsses true "body" +// @Param data body entity.ResOss true "body" // @Success 200 {string} string "{"code": 200, "message": "添加成功"}" // @Success 200 {string} string "{"code": 400, "message": "添加失败"}" -// @Router /admin/resOsses [put] +// @Router /resource/oss [put] // @Security X-TOKEN func (p *ResOssesApi) UpdateResOsses(rc *ctx.ReqCtx) { var data entity.ResOss @@ -101,10 +108,64 @@ func (p *ResOssesApi) UpdateResOsses(rc *ctx.ReqCtx) { // @Param ossId path string true "ossId" // @Success 200 {string} string "{"code": 200, "message": "删除成功"}" // @Success 200 {string} string "{"code": 400, "message": "删除失败"}" -// @Router /admin/resOsses/{ossId } [delete] +// @Router /resource/oss/{ossId} [delete] func (p *ResOssesApi) DeleteResOsses(rc *ctx.ReqCtx) { ossId := rc.GinCtx.Param("ossId") ossIds := utils.IdsStrToIdsIntGroup(ossId) p.ResOssesApp.Delete(ossIds) } + +// @Summary 上传文件ResOsses +// @Description 上传文件 +// @Tags ResOsses +// @Success 200 {string} string "{"code": 200, "message": "删除成功"}" +// @Success 200 {string} string "{"code": 400, "message": "删除失败"}" +// @Router /resource/oss/uploadFile [post] +func (p *ResOssesApi) UplaodResOsses(rc *ctx.ReqCtx) { + file, _ := rc.GinCtx.FormFile("file") + ossCode, _ := rc.GinCtx.GetQuery("ossCode") + list := p.ResOssesApp.FindList(entity.ResOss{OssCode: ossCode}) + li := *list + yunFileTmpPath := "uploads/" + time.Now().Format("2006-01-02") + "/" + file.Filename + // 读取本地文件。 + f, openError := file.Open() + biz.ErrIsNil(openError, "function file.Open() Failed") + biz.ErrIsNil(NewOss(li[0]).PutObj(yunFileTmpPath, f), "上传OSS失败") + + rc.ResData = fmt.Sprintf("https://%s.%s/%s", li[0].BucketName, li[0].Endpoint, yunFileTmpPath) +} + +// @Summary 修改ResOsses状态 +// @Description 获取JSON +// @Tags ResOsses +// @Accept application/json +// @Product application/json +// @Param data body entity.ResOss true "body" +// @Success 200 {string} string "{"code": 200, "message": "添加成功"}" +// @Success 200 {string} string "{"code": 400, "message": "添加失败"}" +// @Router /resource/oss [put] +// @Security X-TOKEN +func (p *ResOssesApi) UpdateOssStatus(rc *ctx.ReqCtx) { + var data entity.ResOss + ginx.BindJsonAndValid(rc.GinCtx, &data) + + p.ResOssesApp.Update(entity.ResOss{OssId: data.OssId, Status: data.Status}) +} + +func NewOss(ens entity.ResOss) oss.Driver { + switch ens.Category { + case "0": + return oss.NewAliOss(oss.AliConfig{ + AccessKey: ens.AccessKey, + SecretKey: ens.SecretKey, + Bucket: ens.BucketName, + Endpoint: ens.Endpoint, + }) + case "1": + //return oss.NewQnOss() + case "2": + //return oss.NewTencentOss() + } + return nil +} diff --git a/apps/resource/entity/mail.go b/apps/resource/entity/mail.go index 646d1ec..9e8387c 100644 --- a/apps/resource/entity/mail.go +++ b/apps/resource/entity/mail.go @@ -1,7 +1,21 @@ package entity +import "pandax/base/model" + /** * @Description 添加qq群467890197 交流学习 * @Author 熊猫 * @Date 2022/1/13 14:47 **/ +type ResEmail struct { + MailId int64 `json:"mailId" gorm:"primaryKey;AUTO_INCREMENT;comment:主键编码"` + Category string `json:"category" grom:"type:varchar(1);comment:分类qq或163"` // 0 163邮箱,1 qq邮箱 2 企业邮箱 + Host string `json:"host" grom:"type:varchar(64);comment:服务器地址"` // 服务器地址 + Port int `json:"port" grom:"type:int;comment:服务器端口"` // 服务器端口 + From string `json:"from" grom:"type:varchar(64);comment:邮箱账号"` // 邮箱账号 + Nickname string `json:"nickname" grom:"type:varchar(64);comment:发件人"` // 发件人 + Secret string `json:"secret" grom:"type:varchar(64);comment:邮箱密码"` // 邮箱密码 + IsSSL bool `json:"isSsl" grom:"comment:是否开启ssl"` // 是否开启ssl + Status string `json:"status" grom:"type:varchar(1);comment:状态"` + model.BaseModel +} diff --git a/apps/resource/router/email.go b/apps/resource/router/email.go new file mode 100644 index 0000000..c5fa882 --- /dev/null +++ b/apps/resource/router/email.go @@ -0,0 +1,56 @@ +package router + +import ( + "github.com/gin-gonic/gin" + "pandax/apps/resource/api" + "pandax/apps/resource/services" + "pandax/base/ctx" +) + +/** + * @Description 添加qq群467890197 交流学习 + * @Author 熊猫 + * @Date 2022/1/14 15:24 + **/ + +func InitResEmailsRouter(router *gin.RouterGroup) { + s := &api.ResEmailsApi{ + ResEmailsApp: services.ResEmailsModelDao, + } + routerGroup := router.Group("email") + + ResEmailsListLog := ctx.NewLogInfo("获取ResEmails分页列表") + routerGroup.GET("list", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(ResEmailsListLog).Handle(s.GetResEmailsList) + }) + + ResEmailsLog := ctx.NewLogInfo("获取ResEmails信息") + routerGroup.GET(":mailId", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(ResEmailsLog).Handle(s.GetResEmails) + }) + + insertResEmailsLog := ctx.NewLogInfo("添加ResEmails信息") + routerGroup.POST("", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(insertResEmailsLog).Handle(s.InsertResEmails) + }) + + updateResEmailsLog := ctx.NewLogInfo("修改ResEmails信息") + routerGroup.PUT("", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(updateResEmailsLog).Handle(s.UpdateResEmails) + }) + + deleteResEmailsLog := ctx.NewLogInfo("删除ResEmails信息") + routerGroup.DELETE(":mailId", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(deleteResEmailsLog).Handle(s.DeleteResEmails) + }) + + updateStatusEmailLog := ctx.NewLogInfo("修改状态") + routerGroup.PUT("changeStatus", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(updateStatusEmailLog).Handle(s.UpdateMailStatus) + }) + + debugMailEmailLog := ctx.NewLogInfo("修改状态") + routerGroup.POST("debugMail", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(debugMailEmailLog).Handle(s.DebugMail) + }) +} diff --git a/apps/resource/router/oss.go b/apps/resource/router/oss.go index db29060..8e87247 100644 --- a/apps/resource/router/oss.go +++ b/apps/resource/router/oss.go @@ -43,4 +43,14 @@ func InitResOssRouter(router *gin.RouterGroup) { routerGroup.DELETE(":ossId", func(c *gin.Context) { ctx.NewReqCtxWithGin(c).WithLog(deleteResOssesLog).Handle(s.DeleteResOsses) }) + + uplaodResOssesLog := ctx.NewLogInfo("测试文件上传") + routerGroup.POST("uploadFile", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(uplaodResOssesLog).Handle(s.UplaodResOsses) + }) + + updateStatusOssLog := ctx.NewLogInfo("修改状态") + routerGroup.PUT("/changeStatus", func(c *gin.Context) { + ctx.NewReqCtxWithGin(c).WithLog(updateStatusOssLog).Handle(s.UpdateOssStatus) + }) } diff --git a/apps/resource/services/email.go b/apps/resource/services/email.go new file mode 100644 index 0000000..16a2872 --- /dev/null +++ b/apps/resource/services/email.go @@ -0,0 +1,94 @@ +package services + +import ( + "pandax/apps/resource/entity" + "pandax/base/biz" + "pandax/base/global" +) + +/** + * @Description 添加qq群467890197 交流学习 + * @Author 熊猫 + * @Date 2022/1/14 15:21 + **/ + +type ( + ResEmailsModel interface { + Insert(data entity.ResEmail) *entity.ResEmail + FindOne(mailId int64) *entity.ResEmail + FindListPage(page, pageSize int, data entity.ResEmail) (*[]entity.ResEmail, int64) + FindList(data entity.ResEmail) *[]entity.ResEmail + Update(data entity.ResEmail) *entity.ResEmail + Delete(mailIds []int64) + } + + emailModelImpl struct { + table string + } +) + +var ResEmailsModelDao ResEmailsModel = &emailModelImpl{ + table: `res_emails`, +} + +func (m *emailModelImpl) Insert(data entity.ResEmail) *entity.ResEmail { + err := global.Db.Table(m.table).Create(&data).Error + biz.ErrIsNil(err, "添加ResEmails失败") + return &data +} + +func (m *emailModelImpl) FindOne(mailId int64) *entity.ResEmail { + resData := new(entity.ResEmail) + err := global.Db.Table(m.table).Where("mail_id = ?", mailId).First(resData).Error + biz.ErrIsNil(err, "查询ResEmails失败") + return resData +} + +func (m *emailModelImpl) FindListPage(page, pageSize int, data entity.ResEmail) (*[]entity.ResEmail, int64) { + list := make([]entity.ResEmail, 0) + var total int64 = 0 + offset := pageSize * (page - 1) + db := global.Db.Table(m.table) + // 此处填写 where参数判断 + if data.MailId != 0 { + db = db.Where("mail_id = ?", data.MailId) + } + if data.Status != "" { + db = db.Where("status = ?", data.Status) + } + if data.Category != "" { + db = db.Where("category = ?", data.Category) + } + db.Where("delete_time IS NULL") + err := db.Count(&total).Error + err = db.Order("create_time").Limit(pageSize).Offset(offset).Find(&list).Error + biz.ErrIsNil(err, "查询ResEmails分页列表失败") + return &list, total +} + +func (m *emailModelImpl) FindList(data entity.ResEmail) *[]entity.ResEmail { + list := make([]entity.ResEmail, 0) + db := global.Db.Table(m.table) + // 此处填写 where参数判断 + if data.MailId != 0 { + db = db.Where("mail_id = ?", data.MailId) + } + if data.Status != "" { + db = db.Where("status = ?", data.Status) + } + if data.Category != "" { + db = db.Where("category = ?", data.Category) + } + db.Where("delete_time IS NULL") + biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询ResEmails列表失败") + return &list +} + +func (m *emailModelImpl) Update(data entity.ResEmail) *entity.ResEmail { + biz.ErrIsNil(global.Db.Table(m.table).Updates(&data).Error, "修改ResEmails失败") + return &data +} + +func (m *emailModelImpl) Delete(mailIds []int64) { + biz.ErrIsNil(global.Db.Table(m.table).Delete(&entity.ResEmail{}, "mail_id in (?)", mailIds).Error, "删除ResEmails失败") +} diff --git a/apps/resource/services/oss.go b/apps/resource/services/oss.go index 9e5ee24..d7bc532 100644 --- a/apps/resource/services/oss.go +++ b/apps/resource/services/oss.go @@ -60,7 +60,7 @@ func (m *resOssesModelImpl) FindListPage(page, pageSize int, data entity.ResOss) db = db.Where("oss_id = ?", data.OssId) } if data.OssCode != "" { - db = db.Where("oss_code like ?", "%"+data.OssCode+"%") + db = db.Where("oss_code = ?", data.OssCode) } if data.Status != "" { db = db.Where("status = ?", data.Status) @@ -83,8 +83,10 @@ func (m *resOssesModelImpl) FindList(data entity.ResOss) *[]entity.ResOss { if data.OssId != 0 { db = db.Where("oss_id = ?", data.OssId) } - - db.Where("delete_time IS NULL") + if data.OssCode != "" { + db = db.Where("oss_code = ?", data.OssCode) + } + db.Where("status = '0' AND delete_time IS NULL") biz.ErrIsNil(db.Order("create_time").Find(&list).Error, "查询ResOsses列表失败") return &list } diff --git a/base/file/file.go b/base/file/file.go new file mode 100644 index 0000000..d008fea --- /dev/null +++ b/base/file/file.go @@ -0,0 +1,33 @@ +package utilFile + +import ( + "io" + "net/http" + "os" +) + +/** + * @Description 添加qq群467890197 交流学习 + * @Author 熊猫 + * @Date 2022/1/14 11:13 + **/ + +// DownloadFile 会将url下载到本地文件,它会在下载时写入,而不是将整个文件加载到内存中。 +func DownloadFile(url, filepath string) error { + // Get the data + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + + // Create the file + out, err := os.Create(filepath) + if err != nil { + return err + } + defer out.Close() + // Write the body to file + _, err = io.Copy(out, resp.Body) + return err +} diff --git a/base/file/zipfiles.go b/base/file/zipfiles.go new file mode 100644 index 0000000..b0ba911 --- /dev/null +++ b/base/file/zipfiles.go @@ -0,0 +1,66 @@ +package utilFile + +import ( + "archive/zip" + "io" + "os" + "strings" +) + +func ZipFiles(filename string, files []string, oldForm, newForm string) error { + + newZipFile, err := os.Create(filename) + if err != nil { + return err + } + defer func() { + _ = newZipFile.Close() + }() + + zipWriter := zip.NewWriter(newZipFile) + defer func() { + _ = zipWriter.Close() + }() + + // 把files添加到zip中 + for _, file := range files { + + err = func(file string) error { + zipFile, err := os.Open(file) + if err != nil { + return err + } + defer zipFile.Close() + // 获取file的基础信息 + info, err := zipFile.Stat() + if err != nil { + return err + } + + header, err := zip.FileInfoHeader(info) + if err != nil { + return err + } + + // 使用上面的FileInforHeader() 就可以把文件保存的路径替换成我们自己想要的了,如下面 + header.Name = strings.Replace(file, oldForm, newForm, -1) + + // 优化压缩 + // 更多参考see http://golang.org/pkg/archive/zip/#pkg-constants + header.Method = zip.Deflate + + writer, err := zipWriter.CreateHeader(header) + if err != nil { + return err + } + if _, err = io.Copy(writer, zipFile); err != nil { + return err + } + return nil + }(file) + if err != nil { + return err + } + } + return nil +} diff --git a/base/mail/email.go b/base/mail/email.go new file mode 100644 index 0000000..ee3c4a0 --- /dev/null +++ b/base/mail/email.go @@ -0,0 +1,78 @@ +package email + +import ( + "crypto/tls" + "errors" + "fmt" + "net/smtp" + "strings" + + "github.com/jordan-wright/email" +) + +type Mail struct { + Host string `json:"host"` // 服务器地址 + Port int `json:"port"` // 服务器端口 + From string `json:"from"` // 邮箱账号 + Nickname string `json:"nickname"` // 发件人 + Secret string `json:"secret"` // 邮箱密码 + IsSSL bool `json:"isSsl"` // 是否开启ssl +} + +const ( + TEXTTYPE = "TEXT" + HTMLTYPE = "HTML" +) + +func (m Mail) Email(to, ty, subject string, body string) error { + tos := strings.Split(to, ",") + return m.send(tos, ty, subject, body) +} + +//@function: ErrorToEmail +//@description: 给email中间件错误发送邮件到指定邮箱 +//@param: subject string, body string +//@return: error + +func (m Mail) ErrorToEmail(to, ty, subject string, body string) error { + tos := strings.Split(to, ",") + if tos[len(to)-1] == "" { // 判断切片的最后一个元素是否为空,为空则移除 + tos = tos[:len(tos)-1] + } + return m.send(tos, ty, subject, body) +} + +//@function: send +//@description: Email发送方法 +//@param: subject string, body string +//@return: error + +func (m Mail) send(to []string, ty, subject string, body string) error { + + auth := smtp.PlainAuth("", m.From, m.Secret, m.Host) + e := email.NewEmail() + if m.Nickname != "" { + e.From = fmt.Sprintf("%s <%s>", m.Nickname, m.From) + } else { + e.From = m.From + } + e.To = to + e.Subject = subject + + if ty == TEXTTYPE { + e.Text = []byte(body) + } else if ty == HTMLTYPE { + e.HTML = []byte(body) + } else { + errors.New("邮件类型不正确") + } + e.HTML = []byte(body) + var err error + hostAddr := fmt.Sprintf("%s:%d", m.Host, m.Port) + if m.IsSSL { + err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: m.Host}) + } else { + err = e.Send(hostAddr, auth) + } + return err +} diff --git a/base/oss/aliyun.go b/base/oss/aliyun.go index f080cec..421b123 100644 --- a/base/oss/aliyun.go +++ b/base/oss/aliyun.go @@ -21,7 +21,7 @@ func (c *AliConfig) CreateBucket() (bucket *aliOssSdk.Bucket, err error) { endpoint := c.Endpoint // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。 accessKeyId := c.AccessKey - accessKeySecret := c.AccessKey + accessKeySecret := c.SecretKey bucketName := c.Bucket // 创建OSSClient实例。 ossClient, err := aliOssSdk.New(endpoint, accessKeyId, accessKeySecret) diff --git a/base/oss/qiniu.go b/base/oss/qiniu.go index ce8af00..f91c1e7 100644 --- a/base/oss/qiniu.go +++ b/base/oss/qiniu.go @@ -3,6 +3,7 @@ package oss import ( "context" "io" + utilFile "pandax/base/file" "github.com/pkg/errors" "github.com/qiniu/go-sdk/v7/auth/qbox" @@ -105,7 +106,7 @@ func (q *qiniuOss) PutObj(objectName string, file io.Reader) error { func (q *qiniuOss) Get(objectName, downloadedFileName string) error { publicAccessURL := storage.MakePublicURL(q.config.Domain, objectName) - err := util.DownLoad.DownloadFile(publicAccessURL, downloadedFileName) + err := utilFile.DownloadFile(publicAccessURL, downloadedFileName) if err != nil { return errors.Wrapf(err, "qiniu oss get file fail") } diff --git a/base/sms/aliyun.go b/base/sms/aliyun.go index ef0c945..ce1d84f 100644 --- a/base/sms/aliyun.go +++ b/base/sms/aliyun.go @@ -5,21 +5,6 @@ import ( "github.com/pkg/errors" ) -type AliYun struct { -} - -func NewAliYun() *AliYun { - return &AliYun{} -} - -func (a *AliYun) GetBusiness(key string) Sms { - return a -} - -func (a *AliYun) Send(phone []string, templateParam interface{}) error { - panic("implement me") -} - type AliConfig struct { accessKeyId string accessSecret string @@ -36,7 +21,7 @@ func NewAliSms(AliConfig AliConfig) *AliSms { } } -func (a *AliSms) send(PhoneNumbers, SignName, TemplateCode, TemplateParam string) error { +func (a *AliSms) Send(PhoneNumbers, SignName, TemplateCode, TemplateParam string) error { //客户端 client, err := dysmsapi.NewClientWithAccessKey(a.config.RegionId, a.config.accessKeyId, a.config.accessSecret) if err != nil { diff --git a/base/sms/sms.go b/base/sms/sms.go index c826af0..a3bf74e 100644 --- a/base/sms/sms.go +++ b/base/sms/sms.go @@ -1,14 +1,13 @@ package sms type Sms interface { - GetBusiness(key string) Sms - Send(phone []string, templateParam interface{}) error + Send(PhoneNumbers, SignName, TemplateCode, TemplateParam string) error } func NewDefaultSms(use string) Sms { switch use { case "AliYun": - return NewAliYun() + return NewAliSms(AliConfig{}) default: panic("sms driver err") } diff --git a/base/utils/ddm.go b/base/utils/ddm.go index d30295e..47658fe 100644 --- a/base/utils/ddm.go +++ b/base/utils/ddm.go @@ -6,9 +6,17 @@ package utils * @Date 2022/1/13 17:16 **/ -func Ddm(data string) string { +func DdmKey(data string) string { if len(data) < 6 { return data } return data[:3] + "****" + data[len(data)-3:] } + +func DdmMail(data string) string { + return data[:3] + "****" + data[len(data)-8:] +} + +func DdmPassword(data string) string { + return "******" +} diff --git a/go.mod b/go.mod index 471217d..e4fdb5f 100644 --- a/go.mod +++ b/go.mod @@ -21,6 +21,7 @@ require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/golang/protobuf v1.5.2 // indirect github.com/google/uuid v1.3.0 + github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible // indirect github.com/json-iterator/go v1.1.11 // indirect github.com/kakuilan/kgo v0.1.8 github.com/lib/pq v1.10.4 diff --git a/go.sum b/go.sum index a3a68a3..bc189eb 100644 --- a/go.sum +++ b/go.sum @@ -179,6 +179,8 @@ github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible h1:jdpOPRN1zP63Td1hDQbZW73xKmzDvZHzVdNYxhnTMDA= +github.com/jordan-wright/email v4.0.1-0.20210109023952-943e75fe5223+incompatible/go.mod h1:1c7szIrayyPPB/987hsnvNzLushdWf4o/79s3P08L8A= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.5/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= diff --git a/initialize/router.go b/initialize/router.go index ec9ff58..b11cd3d 100644 --- a/initialize/router.go +++ b/initialize/router.go @@ -87,6 +87,7 @@ func InitRouter() *gin.Engine { res := router.Group("resource") { resRouter.InitResOssRouter(res) + resRouter.InitResEmailsRouter(res) } return router } diff --git a/initialize/table.go b/initialize/table.go index 1a30509..04cc09b 100644 --- a/initialize/table.go +++ b/initialize/table.go @@ -40,6 +40,7 @@ func InitTable() { devEntity.DevGenTable{}, devEntity.DevGenTableColumn{}, resSourceEntity.ResOss{}, + resSourceEntity.ResEmail{}, ), "初始化表失败") diff --git a/resource/template/vue/edit-vue.template b/resource/template/vue/edit-vue.template index d1ad6e5..a7af81b 100644 --- a/resource/template/vue/edit-vue.template +++ b/resource/template/vue/edit-vue.template @@ -21,7 +21,7 @@ - +