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

115 lines
3.3 KiB
Go
Raw Normal View History

2023-08-11 09:39:18 +00:00
package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
2023-08-25 07:37:35 +00:00
"github.com/google/uuid"
2023-08-11 09:39:18 +00:00
"github.com/zeromicro/go-zero/core/logx"
)
type UserResetTokenLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUserResetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserResetTokenLogic {
return &UserResetTokenLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
// func (l *UserResetTokenLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
func (l *UserResetTokenLogic) UserResetToken(req *types.RequestUserResetToken, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-09-04 08:59:43 +00:00
user, err := l.svcCtx.AllModels.FsUser.FindUserByEmail(context.TODO(), req.Email)
2023-08-11 09:39:18 +00:00
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeRequestParamsErr, err.Error())
}
token := &auth.ResetToken{
// 操作的类型, 验证的token 必须要继承这个
OperateType: auth.OpTypeResetToken,
2023-09-05 07:00:45 +00:00
UserId: user.Id,
2023-08-11 09:39:18 +00:00
Wid: req.Wid,
2023-09-04 08:59:43 +00:00
Email: req.Email,
OldPassword: *user.PasswordHash,
2023-08-25 07:37:35 +00:00
TraceId: uuid.NewString(),
2023-09-04 08:59:43 +00:00
CreateAt: time.Now().UTC(),
2023-08-11 09:39:18 +00:00
}
2023-09-05 07:00:45 +00:00
resetToken, err := l.svcCtx.ResetTokenManger.Encrypt(token)
if err != nil {
logx.Error(err)
return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error())
}
2023-09-05 07:00:45 +00:00
userName := *user.FirstName + " " + *user.LastName
// 进入发送邮箱的系统
EmailManager.EmailTasks <- &EmailFormat{
2023-09-05 07:38:53 +00:00
TemplateName: "get_reset_password_html.tpl",
2023-09-05 07:00:45 +00:00
UniqueKey: "reset_password-" + req.Email,
TargetEmail: req.Email,
CompanyName: "fusen",
2023-09-05 07:38:53 +00:00
ConfirmationLink: l.svcCtx.Config.MainAddress + "/api/auth/reset/password/html?reset_token=" + resetToken, // 跳转连接
2023-09-05 07:00:45 +00:00
SenderName: "support@fusenpack.com",
2023-09-05 07:38:53 +00:00
SenderTitle: "reset password",
2023-09-05 07:00:45 +00:00
Extend: map[string]string{
2023-09-05 07:38:53 +00:00
"UserName": userName,
2023-09-05 07:00:45 +00:00
},
} // email进入队
return resp.SetStatus(basic.CodeOK)
// return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "废弃")
// user, err := l.svcCtx.AllModels.FsUser.FindUserByEmail(context.TODO(), req.Email)
// if err != nil {
// logx.Error(err)
// return resp.SetStatus(basic.CodeRequestParamsErr, err.Error())
// }
// token := &auth.ResetToken{
// // 操作的类型, 验证的token 必须要继承这个
// OperateType: auth.OpTypeResetToken,
// UserId: userinfo.UserId,
// Wid: req.Wid,
// Email: req.Email,
// OldPassword: *user.PasswordHash,
// TraceId: uuid.NewString(),
// CreateAt: time.Now().UTC(),
// }
// rtoken, err := l.svcCtx.ResetTokenManger.Encrypt(token)
// if err != nil {
// logx.Error(err)
// return resp.SetStatus(basic.CodeOAuthResetTokenEncryptErr, err.Error())
// }
// data := types.DataResetToken{
// ResetToken: rtoken,
// }
2023-08-11 09:39:18 +00:00
2023-09-05 07:00:45 +00:00
// return resp.SetStatus(basic.CodeOK, data)
2023-08-11 09:39:18 +00:00
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserResetTokenLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }