update_gpts_user_pwd_logic.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package User
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  6. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  7. "github.com/zeromicro/go-zero/core/errorx"
  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 := claims["userId"].(string)
  30. departmentId := claims["deptId"].(float64)
  31. fmt.Printf("user=%v depid=%v \n", userId, departmentId)
  32. if userId == "" || departmentId != float64(15) {
  33. return nil, errorx.NewInvalidArgumentError("用户需要登录")
  34. }
  35. if req.Password2 == nil || req.Password == nil {
  36. return nil, errorx.NewInvalidArgumentError("参数不能为空")
  37. }
  38. password := *req.Password
  39. password2 := *req.Password2
  40. if password == "" || password2 == "" || password != password2 {
  41. return nil, errorx.NewInvalidArgumentError("2次密码需相同,不能为空")
  42. }
  43. _, err = l.svcCtx.CoreRpc.UpdateUser(l.ctx, &core.UserInfo{
  44. Id: &userId,
  45. Password: &password,
  46. })
  47. if err != nil {
  48. return nil, err
  49. }
  50. return &types.BaseDataInfo{Msg: errormsg.Success}, nil
  51. }