fusenapi/server/auth/internal/logic/email_manager.go

265 lines
6.9 KiB
Go
Raw Normal View History

2023-07-24 09:22:06 +00:00
package logic
import (
"bytes"
2023-09-04 08:23:11 +00:00
"fmt"
2023-08-31 03:12:49 +00:00
"fusenapi/utils/check"
2023-09-04 04:30:24 +00:00
"fusenapi/utils/fstpl"
2023-09-05 10:24:20 +00:00
"html/template"
2023-07-24 09:22:06 +00:00
"log"
"net/smtp"
"sync"
"time"
2023-08-31 04:20:05 +00:00
"github.com/zeromicro/go-zero/core/logx"
2023-07-24 09:22:06 +00:00
)
2023-08-31 04:20:05 +00:00
var EmailTaskResendTime = time.Second * 30
2023-08-31 03:12:49 +00:00
var TimeLimit *check.TimeLimit[string]
2023-07-24 09:22:06 +00:00
var EmailManager *EmailSender
2023-09-04 03:32:17 +00:00
var tpls *template.Template
2023-09-04 02:59:17 +00:00
2023-08-31 03:12:49 +00:00
func init() {
2023-09-04 03:32:17 +00:00
var err error
2023-08-31 03:12:49 +00:00
2023-09-04 04:30:24 +00:00
tpls = fstpl.AutoParseTplFiles()
2023-09-04 02:59:17 +00:00
if err != nil {
log.Fatal(err)
}
2023-10-09 06:41:57 +00:00
TimeLimit = check.NewTimeLimit[string](EmailTaskResendTime)
2023-08-31 03:12:49 +00:00
// Initialize the email manager
EmailManager = &EmailSender{
EmailTasks: make(chan *EmailFormat, 10),
Auth: smtp.PlainAuth(
2023-09-05 07:53:04 +00:00
"fusen support",
2023-08-31 03:12:49 +00:00
"support@fusenpack.com",
"wfbjpdgvaozjvwah",
"smtp.gmail.com",
),
FromEmail: "support@fusenpack.com",
emailSending: make(map[string]*EmailTask, 10),
2023-08-31 04:20:05 +00:00
ResendTimeLimit: EmailTaskResendTime,
semaphore: make(chan struct{}, 100), // Initialize semaphore with a capacity of 10
2023-08-31 03:12:49 +00:00
}
// Start processing email tasks
go EmailManager.ProcessEmailTasks()
// Start clearing expired tasks
go EmailManager.ClearExpiredTasks()
}
2023-07-27 08:48:43 +00:00
type EmailFormat struct {
2023-10-09 06:41:57 +00:00
TemplateName string // 模板名字
UniqueKey string // 用于处理唯一的任务,重发都会被利用到
TargetEmail string // 发送的目标email
CompanyName string // fs公司名
ConfirmationLink string // fs确认连接
SenderName string // 发送人
SenderTitle string // 发送标题
Extend map[string]string // 扩展参数
2023-07-27 08:48:43 +00:00
}
2023-10-09 06:41:57 +00:00
// 验证邮件格式是否符合要求
2023-09-04 08:23:11 +00:00
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
}
2023-07-24 09:22:06 +00:00
// EmailSender
type EmailSender struct {
lock sync.Mutex
2023-07-27 08:48:43 +00:00
EmailTasks chan *EmailFormat
2023-07-24 11:43:56 +00:00
Auth smtp.Auth
FromEmail string
ResendTimeLimit time.Duration
2023-07-27 08:48:43 +00:00
emailSending map[string]*EmailTask
semaphore chan struct{}
2023-07-24 09:22:06 +00:00
}
// EmailTask
type EmailTask struct {
2023-07-27 08:48:43 +00:00
Email *EmailFormat // email
SendTime time.Time // 处理的任务时间
2023-07-24 09:22:06 +00:00
}
2023-10-09 06:41:57 +00:00
// ProcessEmailTasks 是 EmailSender 结构体的方法,用于处理邮件任务。
2023-07-24 09:22:06 +00:00
func (m *EmailSender) ProcessEmailTasks() {
for {
2023-10-09 06:41:57 +00:00
// 从 EmailTasks 通道中接收邮件任务
2023-07-27 08:48:43 +00:00
emailformat, ok := <-m.EmailTasks
2023-07-24 09:22:06 +00:00
if !ok {
log.Println("Email task channel closed")
break
}
2023-10-09 06:41:57 +00:00
// 验证邮件格式是否合法
2023-09-04 08:23:11 +00:00
err := emailformat.Vaild()
if err != nil {
logx.Error(err)
2023-08-31 04:20:05 +00:00
continue
}
2023-10-09 06:41:57 +00:00
// 加锁,避免并发修改 emailSending 字典
2023-07-24 09:22:06 +00:00
m.lock.Lock()
2023-10-09 06:41:57 +00:00
// 检查 emailSending 字典中是否已存在相同的任务
2023-08-31 04:20:05 +00:00
_, isSending := m.emailSending[emailformat.UniqueKey]
2023-07-24 09:22:06 +00:00
if isSending {
m.lock.Unlock()
continue
}
2023-10-09 06:41:57 +00:00
// 将邮件任务添加到 emailSending 字典中
2023-08-31 04:20:05 +00:00
m.emailSending[emailformat.UniqueKey] = &EmailTask{
2023-07-27 08:48:43 +00:00
Email: emailformat,
2023-08-24 10:28:01 +00:00
SendTime: time.Now().UTC(),
2023-07-24 09:22:06 +00:00
}
m.lock.Unlock()
2023-10-09 06:41:57 +00:00
// 获取一个信号量,限制同时发送的邮件任务数量
2023-07-24 11:43:56 +00:00
m.semaphore <- struct{}{}
go func() {
2023-10-09 06:41:57 +00:00
defer func() { <-m.semaphore }() // 释放一个信号量
2023-07-24 11:43:56 +00:00
2023-10-09 06:41:57 +00:00
// 渲染邮件模板内容
2023-09-04 08:23:11 +00:00
content := RenderEmailTemplate(
emailformat.TemplateName,
emailformat.CompanyName,
2023-09-04 05:29:36 +00:00
emailformat.ConfirmationLink,
emailformat.SenderName,
emailformat.SenderTitle,
emailformat.Extend,
)
2023-10-09 06:41:57 +00:00
// 发送邮件
2023-07-27 08:48:43 +00:00
err := smtp.SendMail("smtp.gmail.com:587", m.Auth, m.FromEmail, []string{emailformat.TargetEmail}, content)
2023-07-24 11:43:56 +00:00
if err != nil {
2023-07-27 08:48:43 +00:00
log.Printf("Failed to send email to %s: %v\n", emailformat, err)
2023-10-09 06:41:57 +00:00
// 重新发送邮件
2023-08-31 04:20:05 +00:00
m.Resend(emailformat.UniqueKey, content)
2023-07-24 11:43:56 +00:00
}
}()
2023-07-24 09:22:06 +00:00
}
}
2023-10-09 06:41:57 +00:00
// Resend 是 EmailSender 结构体的方法,用于重发邮件。
2023-08-31 04:20:05 +00:00
func (m *EmailSender) Resend(uniqueKey string, content []byte) {
2023-10-09 06:41:57 +00:00
// 等待一段时间后重发邮件,避免频繁重发
2023-07-24 09:22:06 +00:00
time.Sleep(m.ResendTimeLimit)
m.lock.Lock()
defer m.lock.Unlock()
2023-10-09 06:41:57 +00:00
// 检查邮件任务是否仍存在并且未成功发送
2023-08-31 04:20:05 +00:00
if task, ok := m.emailSending[uniqueKey]; ok && task.SendTime.Add(m.ResendTimeLimit).After(time.Now().UTC()) {
err := smtp.SendMail(task.Email.TargetEmail, m.Auth, m.FromEmail, []string{task.Email.TargetEmail}, content)
2023-07-24 09:22:06 +00:00
if err != nil {
2023-08-31 04:20:05 +00:00
log.Printf("Failed to resend email to %s: %v\n", task.Email.TargetEmail, err)
2023-07-24 09:22:06 +00:00
} else {
2023-10-09 06:41:57 +00:00
// 从 emailSending 字典中删除已成功发送的邮件任务
2023-08-31 04:20:05 +00:00
delete(m.emailSending, uniqueKey)
2023-07-24 09:22:06 +00:00
}
}
}
2023-10-09 06:41:57 +00:00
// ClearExpiredTasks 是 EmailSender 结构体的方法,用于清除过期的邮件任务。
2023-07-24 09:22:06 +00:00
func (m *EmailSender) ClearExpiredTasks() {
2023-10-09 06:41:57 +00:00
// 每分钟触发一次清除操作
2023-07-24 09:22:06 +00:00
ticker := time.NewTicker(time.Minute)
defer ticker.Stop()
for {
<-ticker.C
m.lock.Lock()
2023-10-09 06:41:57 +00:00
// 遍历 emailSending 字典,删除发送时间超过一定限制的过期任务
2023-07-24 09:22:06 +00:00
for email, task := range m.emailSending {
2023-08-24 10:28:01 +00:00
if task.SendTime.Add(m.ResendTimeLimit).Before(time.Now().UTC()) {
2023-07-24 09:22:06 +00:00
delete(m.emailSending, email)
}
}
m.lock.Unlock()
}
}
2023-10-09 06:41:57 +00:00
// RenderEmailTemplate 是一个渲染邮件模板的函数,根据给定的参数生成邮件内容。
// 参数:
// - templateName: 邮件模板的名称
// - companyName: 公司名称
// - confirmationLink: 确认链接
// - senderName: 发件人姓名
// - senderTitle: 发件人职务
// - extend: 扩展字段,包含其他自定义键值对的映射
// 返回值:
// - []byte: 渲染后的邮件内容(以字节切片形式返回)
2023-09-04 02:50:21 +00:00
2023-10-09 06:41:57 +00:00
func RenderEmailTemplate(templateName, companyName, confirmationLink, senderName, senderTitle string, extend map[string]string) []byte {
// 创建一个包含邮件模板所需数据的映射
2023-07-24 09:22:06 +00:00
data := map[string]string{
"CompanyName": companyName,
"ConfirmationLink": confirmationLink,
"SenderName": senderName,
"SenderTitle": senderTitle,
}
2023-10-09 06:41:57 +00:00
// 将扩展字段中的键值对添加到数据映射中
2023-09-04 05:29:36 +00:00
for k, v := range extend {
data[k] = v
}
2023-10-09 06:41:57 +00:00
// 创建一个字节缓冲区,用于存储渲染后的邮件内容
2023-07-24 09:22:06 +00:00
var result bytes.Buffer
2023-10-09 06:41:57 +00:00
// 使用给定的数据映射执行邮件模板渲染
2023-09-04 08:23:11 +00:00
err := tpls.ExecuteTemplate(&result, templateName, data)
2023-07-24 09:22:06 +00:00
if err != nil {
log.Fatal(err)
}
2023-10-09 06:41:57 +00:00
// 将渲染后的邮件内容转换为字节切片并返回
2023-07-24 11:43:56 +00:00
return result.Bytes()
2023-07-24 09:22:06 +00:00
}