Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
laodaming 2023-09-04 16:43:42 +08:00
commit 2eb552e303
8 changed files with 84 additions and 17 deletions

View File

@ -6,7 +6,7 @@ Thank you for registering an account with {{ .CompanyName }}. We are thrilled to
Please take a moment to confirm your email address by clicking the button below so we can activate your account:
<a href="{{ .ConfirmationLink }}" target="_blank" style="background-color: #008CBA; color: #FFFFFF; text-decoration: none; padding: 10px 15px; border-radius: 3px; display:inline-block; font-weight: bold;">Confirm Account: .UserName</a>
<a href="{{ .ConfirmationLink }}" target="_blank" style="background-color: #008CBA; color: #FFFFFF; text-decoration: none; padding: 10px 15px; border-radius: 3px; display:inline-block; font-weight: bold;">Confirm Account: {{ .UserName }}</a>
Confirming your email ensures your account is properly secured. This also lets you access member-only tools and content.

View File

@ -0,0 +1,16 @@
Dear {{ .UserName }},
We have received your request to reset your {{ .CompanyName }} account password.
Please click the button below to confirm your new password:
<h1 style="color: red; font-weight: bold;">{{ .MaskedPassword }}</h1> (This can be replaced by the masked new password)
<a href="{{ .ConfirmationLink }}" target="_blank" style="background-color: #008CBA; color: #FFFFFF; text-decoration: none; padding: 10px 15px; border-radius: 3px; font-weight: bold;">Confirm New Password</a>
This password reset confirmation link will expire in 60 minutes. Please let us know if you have any other questions!
Regards,
{{ .SenderName }}
{{ .SenderTitle }}
{{ .CompanyName }}

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

View File

@ -12,28 +12,28 @@ import "basic.api"
service auth {
@handler UserLoginHandler
post /api/auth/login(RequestUserLogin) returns (response);
@handler UserRegisterHandler
post /api/auth/register(RequestUserRegister) returns (response);
@handler AcceptCookieHandler
post /api/auth/accept-cookie(request) returns (response);
@handler UserGoogleLoginHandler
get /api/auth/oauth2/login/google(RequestGoogleLogin) returns (response);
@handler UserEmailConfirmationHandler
get /api/auth/email/confirmation(RequestEmailConfirmation) returns (response);
@handler UserEmailRegisterHandler
post /api/auth/oauth2/register(RequestEmailRegister) returns (response);
@handler UserResetTokenHandler
post /api/auth/reset/token(RequestUserResetToken) returns (response);
@handler UserResetPasswordHandler
post /api/auth/reset/password(RequestUserResetPassword) returns (response);
@handler DebugAuthDeleteHandler
post /api/auth/debug/delete(RequestAuthDelete) returns (response);
}
@ -79,7 +79,8 @@ type (
// RequestUserResetToken 请求重置token, 一定不为null
RequestUserResetToken {
Wid string `json:"wid"`
Wid string `json:"wid"`
Email string `json:"email"` // email
}
// UserResetTokenHandler 返回重置token