fusenapi/server/auth/internal/logic/userresettokenlogic.go
2023-08-24 11:47:22 +08:00

74 lines
1.9 KiB
Go

package logic
import (
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/auth/internal/svc"
"fusenapi/server/auth/internal/types"
"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
if !userinfo.IsUser() {
return resp.SetStatus(basic.CodeUnAuth)
}
user, err := l.svcCtx.AllModels.FsUser.FindUserById(context.TODO(), userinfo.UserId)
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: *user.Email,
OldPassword: *user.PasswordHash,
CreateAt: time.Now(),
}
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,
}
return resp.SetStatus(basic.CodeOK, data)
}
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *UserResetTokenLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }