package logic import ( "fmt" "fusenapi/model/gmodel" "fusenapi/utils/auth" "fusenapi/utils/basic" "context" "fusenapi/server/auth/internal/svc" "fusenapi/server/auth/internal/types" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type UserResetPasswordLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUserResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserResetPasswordLogic { return &UserResetPasswordLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } // 处理进入前逻辑w,r // func (l *UserResetPasswordLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { // } func (l *UserResetPasswordLogic) UserResetPassword(req *types.RequestUserResetPassword, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null if !userinfo.IsUser() { return resp.SetStatus(basic.CodeUnAuth) } rt, err := l.svcCtx.ResetTokenManger.Decrypt(req.ResetToken) if err != nil { logx.Error(err) return resp.SetStatus(basic.CodeOAuthResetTokenDecryptErr, err.Error()) } // TODO: 存储 if rt.OperateType != auth.OpTypeResetToken { return resp.SetStatus(basic.CodeOAuthTypeErr, "error OperateType: rt.OperateType != auth.OpTypeResetToken") } err = l.svcCtx.AllModels.FsUser.Transaction(l.ctx, func(tx *gorm.DB) error { user := &gmodel.FsUser{Id: int64(rt.UserId)} err := tx.Take(user).Error if err != nil { return err } if *user.PasswordHash != rt.OldPassword { return fmt.Errorf("password had beed updated") } return tx.Update("PasswordHash", req.Password).Error }) if err != nil { return resp.SetStatus(basic.CodeDbSqlErr, err.Error()) } return resp.SetStatus(basic.CodeOK) } // 处理逻辑后 w,r 如:重定向, resp 必须重新处理 // func (l *UserResetPasswordLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { // // httpx.OkJsonCtx(r.Context(), w, resp) // }