update_gpts_user_info_logic.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package User
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  5. "github.com/zeromicro/go-zero/core/errorx"
  6. "wechat-api/internal/utils"
  7. jwtutils "wechat-api/internal/utils/jwt"
  8. "wechat-api/internal/svc"
  9. "wechat-api/internal/types"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type UpdateGptsUserInfoLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewUpdateGptsUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGptsUserInfoLogic {
  18. return &UpdateGptsUserInfoLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *UpdateGptsUserInfoLogic) UpdateGptsUserInfo(req *types.UserInfo, tokenStr string) (*types.BaseDataInfo, error) {
  24. claims, err := jwtutils.ParseJwtToken(l.svcCtx.Config.Auth.AccessSecret, tokenStr)
  25. if err != nil {
  26. return nil, errorx.NewInvalidArgumentError("用户未登录")
  27. }
  28. userId, ok := claims["userId"].(string)
  29. if !ok || userId == "" {
  30. return nil, errorx.NewInvalidArgumentError("用户需要登录")
  31. }
  32. userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
  33. valid := utils.CheckGptLogin(userInfo.RoleIds)
  34. if !valid {
  35. return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
  36. }
  37. resp := types.BaseDataInfo{}
  38. var mobile, nickname, email string
  39. if req.Email != nil && *req.Email != "" {
  40. email = *req.Email
  41. }
  42. if req.Mobile != nil && *req.Mobile != "" {
  43. mobile = *req.Mobile
  44. }
  45. if req.Nickname != nil && *req.Nickname != "" {
  46. nickname = *req.Nickname
  47. }
  48. _, err = l.svcCtx.CoreRpc.UpdateUser(l.ctx, &core.UserInfo{
  49. Id: &userId,
  50. Mobile: &mobile,
  51. Nickname: &nickname,
  52. Email: &email,
  53. })
  54. if err != nil {
  55. return nil, errorx.NewInvalidArgumentError(err.Error())
  56. }
  57. return &resp, nil
  58. }