107 lines
2.8 KiB
Go
107 lines
2.8 KiB
Go
package logic
|
|
|
|
import (
|
|
"fusenapi/utils/auth"
|
|
"fusenapi/utils/basic"
|
|
|
|
"context"
|
|
|
|
"fusenapi/server/auth/internal/svc"
|
|
"fusenapi/server/auth/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UserEmailRegisterLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewUserEmailRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserEmailRegisterLogic {
|
|
return &UserEmailRegisterLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
// 处理进入前逻辑w,r
|
|
// func (l *UserEmailRegisterLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
|
// }
|
|
|
|
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
|
// func (l *UserEmailRegisterLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
|
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
|
// }
|
|
|
|
func (l *UserEmailRegisterLogic) UserEmailRegister(req *types.RequestEmailRegister, userinfo *auth.UserInfo) (resp *basic.Response) {
|
|
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
|
// userinfo 传入值时, 一定不为null
|
|
|
|
// 进入邮件注册流程
|
|
// 这里是注册模块, 发邮件, 通过邮件注册确认邮箱存在
|
|
|
|
// 邮箱验证格式错误
|
|
if !auth.ValidateEmail(req.Email) {
|
|
return resp.SetStatus(basic.CodeOAuthEmailErr)
|
|
}
|
|
|
|
if !TimeLimit.Is(req.Email) {
|
|
return resp.SetStatus(basic.CodeEmailTimeShortErr)
|
|
}
|
|
|
|
token, err := l.svcCtx.OAuthTokenManger.Decrypt(req.RegisterToken)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
}
|
|
|
|
if token.OperateType != auth.OpTypeRegister {
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
}
|
|
|
|
// 确认email 重新序列化
|
|
token.Email = req.Email
|
|
token.Wid = req.Wid
|
|
token.GuestId = userinfo.GuestId
|
|
|
|
clurl, err := l.svcCtx.OAuthTokenManger.Generate(token)
|
|
if err != nil {
|
|
logx.Error(err)
|
|
return resp.SetStatus(basic.CodeOAuthRegisterTokenErr)
|
|
}
|
|
|
|
var tplsel string = "email_register.tpl"
|
|
if token.Platform == auth.PLATFORM_FUSEN {
|
|
tplsel = "email_register.tpl"
|
|
} else {
|
|
|
|
tplsel = "email_register_oauth2.tpl"
|
|
}
|
|
|
|
userName := token.Extend["first_name"].(string) + " " + token.Extend["last_name"].(string)
|
|
var platformAndId string
|
|
if oauthId, ok := token.Extend["oauth_id"]; ok {
|
|
platformAndId = token.Platform + " Id: " + oauthId.(string)
|
|
}
|
|
|
|
// 进入发送邮箱的系统
|
|
EmailManager.EmailTasks <- &EmailFormat{
|
|
TemplateName: tplsel,
|
|
UniqueKey: "register-" + req.Email,
|
|
TargetEmail: req.Email,
|
|
CompanyName: "fusen",
|
|
ConfirmationLink: clurl,
|
|
SenderName: "support@fusenpack.com",
|
|
SenderTitle: "register-valid",
|
|
Extend: map[string]string{
|
|
"UserName": userName,
|
|
"Email": req.Email,
|
|
"PlatformAndID": platformAndId,
|
|
},
|
|
} // email进入队
|
|
|
|
return resp.SetStatus(basic.CodeOK)
|
|
}
|