get_gpts_user_balance_logic.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package User
  2. import (
  3. "context"
  4. "github.com/alibabacloud-go/tea/tea"
  5. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  6. "github.com/zeromicro/go-zero/core/errorx"
  7. "wechat-api/ent"
  8. "wechat-api/ent/creditbalance"
  9. "wechat-api/internal/utils"
  10. jwtutils "wechat-api/internal/utils/jwt"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type GetGptsUserBalanceLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewGetGptsUserBalanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetGptsUserBalanceLogic {
  21. return &GetGptsUserBalanceLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *GetGptsUserBalanceLogic) GetGptsUserBalance(tokenStr string) (*types.UserBalanceResp, error) {
  27. resp := types.UserBalanceResp{}
  28. resp.Data.Balance = tea.Float32(0.0)
  29. claims, err := jwtutils.ParseJwtToken(l.svcCtx.Config.Auth.AccessSecret, tokenStr)
  30. if err != nil {
  31. return nil, errorx.NewInvalidArgumentError("用户未登录")
  32. }
  33. userId, ok := claims["userId"].(string)
  34. if !ok || userId == "" {
  35. return nil, errorx.NewInvalidArgumentError("用户需要登录")
  36. }
  37. userInfo, _ := l.svcCtx.CoreRpc.GetUserById(l.ctx, &core.UUIDReq{Id: userId})
  38. valid := utils.CheckGptLogin(userInfo.RoleIds)
  39. if !valid {
  40. return nil, errorx.NewInvalidArgumentError("用户不允许登陆")
  41. }
  42. balance, err := l.svcCtx.DB.CreditBalance.Query().Where(creditbalance.UserID(userId)).First(l.ctx)
  43. if err != nil && !ent.IsNotFound(err) {
  44. l.Logger.Errorf("get credit_balance failed:%v\n", err)
  45. return &resp, nil
  46. }
  47. if balance != nil {
  48. resp.Data.Balance = &balance.Balance
  49. }
  50. return &resp, nil
  51. }