get_gpts_user_info_logic.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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/svc"
  7. "wechat-api/internal/types"
  8. "wechat-api/internal/utils"
  9. jwtutils "wechat-api/internal/utils/jwt"
  10. "github.com/zeromicro/go-zero/core/logx"
  11. )
  12. type GetGptsUserInfoLogic struct {
  13. logx.Logger
  14. ctx context.Context
  15. svcCtx *svc.ServiceContext
  16. }
  17. func NewGetGptsUserInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGptsUserInfoLogic {
  18. return &GetGptsUserInfoLogic{
  19. Logger: logx.WithContext(ctx),
  20. ctx: ctx,
  21. svcCtx: svcCtx}
  22. }
  23. func (l *GetGptsUserInfoLogic) GetGptsUserInfo(tokenStr string) (*types.UserInfoResp, error) {
  24. resp := types.UserInfoResp{}
  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. resp.Data.Username = userInfo.Username
  39. resp.Data.Nickname = userInfo.Nickname
  40. resp.Data.Id = &userId
  41. resp.Data.Status = userInfo.Status
  42. resp.Data.Mobile = userInfo.Mobile
  43. resp.Data.Email = userInfo.Email
  44. return &resp, nil
  45. }