get_api_token_list_logic.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package token
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  5. "github.com/suyuan32/simple-admin-common/utils/pointy"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/ent/token"
  8. "wechat-api/internal/utils/dberrorhandler"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type GetApiTokenListLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewGetApiTokenListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiTokenListLogic {
  19. return &GetApiTokenListLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx}
  23. }
  24. func (l *GetApiTokenListLogic) GetApiTokenList(req *types.TokenListReq) (*types.TokenListResp, error) {
  25. organizationId := l.ctx.Value("organizationId").(uint64)
  26. var predicates []predicate.Token
  27. predicates = append(predicates, token.OrganizationID(organizationId))
  28. data, err := l.svcCtx.DB.Token.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  29. if err != nil {
  30. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  31. }
  32. resp := &types.TokenListResp{}
  33. resp.Msg = errormsg.Success
  34. resp.Data.Total = data.PageDetails.Total
  35. for _, v := range data.List {
  36. expireAtStr := v.ExpireAt.Format("2006-01-02 15:04:05")
  37. resp.Data.Data = append(resp.Data.Data,
  38. types.TokenInfo{
  39. BaseIDInfo: types.BaseIDInfo{
  40. Id: &v.ID,
  41. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  42. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  43. },
  44. ExpireAt: pointy.GetUnixMilliPointer(v.ExpireAt.UnixMilli()),
  45. ExpireAtStr: &expireAtStr,
  46. Token: &v.Token,
  47. Mac: &v.MAC,
  48. OrganizationId: &v.OrganizationID,
  49. AgentId: &v.AgentID,
  50. CustomAgentBase: &v.CustomAgentBase,
  51. CustomAgentKey: &v.CustomAgentKey,
  52. OpenaiBase: &v.OpenaiBase,
  53. OpenaiKey: &v.OpenaiKey,
  54. })
  55. }
  56. return resp, nil
  57. }