This commit is contained in:
eson
2023-09-04 16:23:11 +08:00
parent c062848a95
commit dc21c51a0c
8 changed files with 84 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ package logic
import (
"bytes"
"fmt"
"fusenapi/utils/check"
"fusenapi/utils/fstpl"
"log"
@@ -52,6 +53,7 @@ func init() {
}
type EmailFormat struct {
TemplateName string // 模板名字
UniqueKey string // 用于处理唯一的任务,重发都会被利用到
TargetEmail string // 发送的目标email
CompanyName string // fs公司名
@@ -61,6 +63,48 @@ type EmailFormat struct {
Extend map[string]string
}
func (eformat *EmailFormat) Vaild() error {
// 1. 检查模板名称
if tpls.Lookup(eformat.TemplateName) == nil {
return fmt.Errorf("%s template name is not found", eformat.TemplateName)
}
// 2. 检查公司名称
if eformat.CompanyName == "" {
return fmt.Errorf("company name cannot be empty")
}
// 3. 检查确认链接
if eformat.ConfirmationLink == "" {
return fmt.Errorf("confirmation link cannot be empty")
}
// 4. 检查发送人名称
if eformat.SenderName == "" {
return fmt.Errorf("sender name cannot be empty")
}
// 5. 检查发送人头衔
if eformat.SenderTitle == "" {
return fmt.Errorf("sender title cannot be empty")
}
// 6. 检查目标邮箱
if eformat.TargetEmail == "" {
return fmt.Errorf("target email cannot be empty")
}
// 7. 检查唯一键
if eformat.UniqueKey == "" {
return fmt.Errorf("unique key cannot be empty")
}
// 所有校验通过
return nil
}
// EmailSender
type EmailSender struct {
lock sync.Mutex
@@ -87,8 +131,9 @@ func (m *EmailSender) ProcessEmailTasks() {
break
}
if emailformat.UniqueKey == "" {
logx.Error("email UniqueKey must be exists")
err := emailformat.Vaild()
if err != nil {
logx.Error(err)
continue
}
@@ -111,7 +156,9 @@ func (m *EmailSender) ProcessEmailTasks() {
go func() {
defer func() { <-m.semaphore }() // Release a token
content := RenderEmailRegisterTemplate(emailformat.CompanyName,
content := RenderEmailTemplate(
emailformat.TemplateName,
emailformat.CompanyName,
emailformat.ConfirmationLink,
emailformat.SenderName,
emailformat.SenderTitle,
@@ -162,7 +209,7 @@ func (m *EmailSender) ClearExpiredTasks() {
}
}
func RenderEmailRegisterTemplate(companyName, confirmationLink, senderName, senderTitle string, extend map[string]string) []byte {
func RenderEmailTemplate(templateName, companyName, confirmationLink, senderName, senderTitle string, extend map[string]string) []byte {
data := map[string]string{
"CompanyName": companyName,
@@ -176,7 +223,7 @@ func RenderEmailRegisterTemplate(companyName, confirmationLink, senderName, send
}
var result bytes.Buffer
err := tpls.ExecuteTemplate(&result, "email_register.tpl", data)
err := tpls.ExecuteTemplate(&result, templateName, data)
if err != nil {
log.Fatal(err)
}

View File

@@ -75,6 +75,7 @@ func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegist
userName := token.Extend["first_name"].(string) + " " + token.Extend["last_name"].(string)
// 进入发送邮箱的系统
EmailManager.EmailTasks <- &EmailFormat{
TemplateName: "email_register.tpl",
UniqueKey: "register-" + req.Email,
TargetEmail: req.Email,
CompanyName: "fusen",

View File

@@ -74,6 +74,7 @@ func (l *UserRegisterLogic) UserRegister(req *types.RequestUserRegister, userinf
// 进入发送邮箱的系统
EmailManager.EmailTasks <- &EmailFormat{
TemplateName: "email_register.tpl",
UniqueKey: "register-" + req.Email,
TargetEmail: req.Email,
CompanyName: "fusen",

View File

@@ -28,7 +28,8 @@ type RequestUserRegister struct {
}
type RequestUserResetToken struct {
Wid string `json:"wid"`
Wid string `json:"wid"`
Email string `json:"email"` // email
}
type DataResetToken struct {

View File

@@ -76,7 +76,7 @@ func (mquery *ModuleQuery) EncodeEmpty() map[string]any {
query = "." + query
}
k := fmt.Sprintf("%s%s", mquery.ModuleName, query)
qstr[k] = map[string]any{}
qstr[k] = nil
}
return qstr