From a40ab2d2180191c6eaf95578e171ca5342be2953 Mon Sep 17 00:00:00 2001 From: tianjun Date: Thu, 2 Feb 2023 17:59:03 +0800 Subject: [PATCH] add: email module --- go.mod | 3 +++ go.sum | 6 ++++++ main.go | 20 +++++++++++++++++++ pkg/util/mail_util.go | 36 +++++++++++++++++++++++++++++++++++ pkg/util/my_util.go | 33 ++++++++++++++++++++++++++++++++ refs/database.go | 9 ++++++++- resources/application-dev.yml | 2 +- resources/application.yml | 2 +- 8 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 pkg/util/mail_util.go diff --git a/go.mod b/go.mod index a2acb77..b4ac11c 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/druidcaesa/gotool v0.0.0-20210722000450-b972ac6f3771 github.com/gin-gonic/gin v1.7.3 github.com/go-basic/uuid v1.0.0 + github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df github.com/go-sql-driver/mysql v1.6.0 github.com/go-xorm/xorm v0.7.9 github.com/gomodule/redigo v1.8.5 @@ -21,5 +22,7 @@ require ( github.com/xuri/excelize/v2 v2.4.1 github.com/yinheli/mahonia v0.0.0-20131226213531-0eef680515cc // indirect github.com/yinheli/qqwry v0.0.0-20160229183603-f50680010f4a + gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df // indirect gopkg.in/yaml.v2 v2.2.8 ) diff --git a/go.sum b/go.sum index 17ea3f8..42de960 100644 --- a/go.sum +++ b/go.sum @@ -33,6 +33,8 @@ github.com/gin-gonic/gin v1.7.3 h1:aMBzLJ/GMEYmv1UWs2FFTcPISLrQH2mRgL9Glz8xows= github.com/gin-gonic/gin v1.7.3/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= github.com/go-basic/uuid v1.0.0 h1:Faqtetcr8uwOzR2qp8RSpkahQiv4+BnJhrpuXPOo63M= github.com/go-basic/uuid v1.0.0/go.mod h1:yVtVnsXcmaLc9F4Zw7hTV7R0+vtuQw00mdXi+F6tqco= +github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df h1:Bao6dhmbTA1KFVxmJ6nBoMuOJit2yjEgLJpIMYpop0E= +github.com/go-gomail/gomail v0.0.0-20160411212932-81ebce5c23df/go.mod h1:GJr+FCSXshIwgHBtLglIg9M2l2kQSi6QjVAngtzI08Y= github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= @@ -242,11 +244,15 @@ google.golang.org/genproto v0.0.0-20190404172233-64821d5d2107/go.mod h1:VzzqZJRn google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs= google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= +gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df h1:n7WqCuqOuCbNr617RXOY0AWRXxgwEyPp2z+p0+hgMuE= +gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df/go.mod h1:LRQQ+SO6ZHR7tOkpBDuZnXENFzX8qRjMDMyPD6BRkCw= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go index f36a3f6..ab65a7a 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main // init函数执行顺序自上而下, 最后执行main包里面的init函数 import ( + "cutego/modules/core/dataobject" "cutego/pkg/config" _ "cutego/pkg/cronjob" _ "cutego/pkg/gin" @@ -10,10 +11,29 @@ import ( "cutego/refs" "fmt" "github.com/gin-gonic/gin" + "time" ) func main() { //go testChangeJob() + //starter() + user := dataobject.SysUser{} + user.UserId = 1 + user.UserName = "test" + user.Password = "123456" + user.LoginDate = time.Now() + jsonString := util.ToJSONString(user) + fmt.Println(jsonString) + fmt.Println(util.FormatDateTime(user.LoginDate)) + fmt.Println(util.FormatDate(user.LoginDate)) + fmt.Println(util.FormatTime(user.LoginDate)) + + sysUser := dataobject.SysUser{} + util.ParseJSONStruct(jsonString, &sysUser) + fmt.Println(sysUser) +} + +func starter() { gin.SetMode(util.IF(config.AppEnvConfig.Server.RunMode == "", "debug", config.AppEnvConfig.Server.RunMode).(string)) err := refs.CoolGin.Run(fmt.Sprintf(":%d", config.AppEnvConfig.Server.Port)) if err != nil { diff --git a/pkg/util/mail_util.go b/pkg/util/mail_util.go new file mode 100644 index 0000000..4ecd2a0 --- /dev/null +++ b/pkg/util/mail_util.go @@ -0,0 +1,36 @@ +package util + +import "cutego/pkg/config" +import "github.com/go-gomail/gomail" + +// BatchSendCode 批量发送验证码 +// Param: targetUserEmails 接收者邮箱地址 +// Param: code 验证码 +func BatchSendCode(targetUserEmails []string, code string) error { + mailConfig := config.AppCoreConfig.CuteGoConfig.Mail + sender := mailConfig.Username + authCode := mailConfig.Password + mailTitle := "CuteGo验证码" + mailBody := "您的验证码为: " + code + + message := gomail.NewMessage() + message.SetHeader("From", sender) + message.SetHeader("To", targetUserEmails...) + message.SetHeader("Subject", mailTitle) + message.SetBody("text/html", mailBody) + + // 添加附件 + //zipPath := "./xxxx.zip" + //message.Attach(zipPath) + + dialer := gomail.NewDialer(mailConfig.Host, mailConfig.Port, sender, authCode) + return dialer.DialAndSend(message) +} + +// SendCode 发送验证码 +// Param: targetUserEmail 接收者邮箱地址 +// Param: code 验证码 +func SendCode(targetUserEmail string, code string) error { + targetUserEmails := []string{targetUserEmail} + return BatchSendCode(targetUserEmails, code) +} diff --git a/pkg/util/my_util.go b/pkg/util/my_util.go index 07743ad..643f42d 100644 --- a/pkg/util/my_util.go +++ b/pkg/util/my_util.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "strconv" + "time" ) // IF 三元表达式 @@ -78,3 +79,35 @@ func IsFileOrDirExist(path string) bool { } return true } + +// ToJSONString 转json字符串 +// v 对象, 非对象地址 +func ToJSONString(v interface{}) string { + output, _ := json.Marshal(&v) + return string(output) +} + +// ParseJSONStruct 解析json字符串 +// jsonStr json字符串 +// v 对象, 非对象地址 +func ParseJSONStruct(jsonStr string, v interface{}) { + err := json.Unmarshal([]byte(jsonStr), &v) + if err != nil { + panic("JSON解析失败, " + err.Error()) + } +} + +// FormatDateTime 格式化日期时间 +func FormatDateTime(date time.Time) string { + return date.Format("2006-01-02 15:04:05") +} + +// FormatDate 格式化日期 +func FormatDate(date time.Time) string { + return date.Format("2006-01-02") +} + +// FormatTime 格式化时间 +func FormatTime(date time.Time) string { + return date.Format("15:04:05") +} diff --git a/refs/database.go b/refs/database.go index f936c60..75508b0 100644 --- a/refs/database.go +++ b/refs/database.go @@ -15,7 +15,7 @@ func init() { var err error // 配置mysql数据库 ds := config.AppEnvConfig.DataSource - jdbc := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True&loc=Local", + jdbc := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=True", ds.Username, ds.Password, ds.Host, @@ -44,5 +44,12 @@ func init() { SqlDB.ShowSQL(true) // 开启缓存 SqlDB.SetDefaultCacher(xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)) + // 切换标准时区 + location, err := time.LoadLocation("Asia/Shanghai") + if err != nil { + panic("加载时区异常, " + err.Error()) + } + SqlDB.SetTZLocation(location) + SqlDB.SetTZDatabase(location) logging.InfoLog("database init end...") } diff --git a/resources/application-dev.yml b/resources/application-dev.yml index d9e0428..2f8b40e 100644 --- a/resources/application-dev.yml +++ b/resources/application-dev.yml @@ -20,7 +20,7 @@ redis: database: 1 host: 127.0.0.1 port: 6379 - password: + password: 123456 # 连接超时时间 timeout: 5000 pool: diff --git a/resources/application.yml b/resources/application.yml index 2a19a2f..21036d6 100644 --- a/resources/application.yml +++ b/resources/application.yml @@ -6,7 +6,7 @@ cutego: #邮件 mail: host: smtp.qq.com - port: 465 + port: 587 username: tianjun@odboy.cn password: xxxxxxxxxxx default-encoding: UTF-8