update_gpts_user_pwd_logic.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package User
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "wechat-api/internal/utils"
  8. jwtutils "wechat-api/internal/utils/jwt"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type UpdateGptsUserPwdLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewUpdateGptsUserPwdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGptsUserPwdLogic {
  19. return &UpdateGptsUserPwdLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *UpdateGptsUserPwdLogic) UpdateGptsUserPwd(tokenStr string, req *types.PasswordReq) (*types.BaseDataInfo, error) {
  25. claims, err := jwtutils.ParseJwtToken(l.svcCtx.Config.Auth.AccessSecret, tokenStr)
  26. if err != nil {
  27. return nil, errorx.NewInvalidArgumentError("用户未登录")
  28. }
  29. userId, ok := claims["userId"].(string)
  30. if !ok || userId == "" {
  31. return nil, errorx.NewInvalidArgumentError("用户需要登录")
  32. }
  33. userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
  34. valid := utils.CheckGptLogin(userInfo.RoleIds)
  35. if !valid {
  36. return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
  37. }
  38. if req.Password2 == nil || req.Password == nil {
  39. return nil, errorx.NewInvalidArgumentError("参数不能为空")
  40. }
  41. password := *req.Password
  42. password2 := *req.Password2
  43. if password == "" || password2 == "" || password != password2 {
  44. return nil, errorx.NewInvalidArgumentError("2次密码需相同,不能为空")
  45. }
  46. _, err = l.svcCtx.CoreRpc.UpdateUser(l.ctx, &core.UserInfo{
  47. Id: &userId,
  48. Password: &password,
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &types.BaseDataInfo{Msg: errormsg.Success}, nil
  54. }