get_api_token_list_logic.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  7. "wechat-api/ent/predicate"
  8. "wechat-api/ent/token"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "wechat-api/internal/svc"
  11. "wechat-api/internal/types"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetApiTokenListLogic struct {
  15. logx.Logger
  16. ctx context.Context
  17. svcCtx *svc.ServiceContext
  18. }
  19. func NewGetApiTokenListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetApiTokenListLogic {
  20. return &GetApiTokenListLogic{
  21. Logger: logx.WithContext(ctx),
  22. ctx: ctx,
  23. svcCtx: svcCtx}
  24. }
  25. func (l *GetApiTokenListLogic) GetApiTokenList(req *types.TokenListReq) (*types.TokenListResp, error) {
  26. organizationId := l.ctx.Value("organizationId").(uint64)
  27. var predicates []predicate.Token
  28. predicates = append(predicates, token.OrganizationID(organizationId))
  29. data, err := l.svcCtx.DB.Token.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  30. if err != nil {
  31. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  32. }
  33. resp := &types.TokenListResp{}
  34. resp.Msg = errormsg.Success
  35. resp.Data.Total = data.PageDetails.Total
  36. for _, v := range data.List {
  37. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  38. if err != nil {
  39. l.Error("获取部门信息失败", err)
  40. }
  41. var agent types.AgentInfo
  42. if v.AgentID == 0 {
  43. agentName := "定制 AI 角色"
  44. agent = types.AgentInfo{
  45. Name: &agentName,
  46. }
  47. } else {
  48. if v.Edges.Agent != nil {
  49. agent = types.AgentInfo{
  50. BaseIDInfo: types.BaseIDInfo{
  51. Id: &v.AgentID,
  52. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  53. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  54. },
  55. Name: &v.Edges.Agent.Name,
  56. Role: &v.Edges.Agent.Role,
  57. Status: &v.Edges.Agent.Status,
  58. Background: &v.Edges.Agent.Background,
  59. Examples: &v.Edges.Agent.Examples,
  60. }
  61. }
  62. }
  63. expireAtStr := v.ExpireAt.Format("2006-01-02 15:04:05")
  64. resp.Data.Data = append(resp.Data.Data,
  65. types.TokenInfo{
  66. BaseIDInfo: types.BaseIDInfo{
  67. Id: &v.ID,
  68. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  69. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  70. },
  71. ExpireAt: pointy.GetUnixMilliPointer(v.ExpireAt.UnixMilli()),
  72. ExpireAtStr: &expireAtStr,
  73. Token: &v.Token,
  74. Mac: &v.MAC,
  75. OrganizationId: &v.OrganizationID,
  76. OrganizationName: departmentInfo.Name,
  77. AgentId: &v.AgentID,
  78. AgentInfo: &agent,
  79. CustomAgentBase: &v.CustomAgentBase,
  80. CustomAgentKey: &v.CustomAgentKey,
  81. OpenaiBase: &v.OpenaiBase,
  82. OpenaiKey: &v.OpenaiKey,
  83. })
  84. }
  85. return resp, nil
  86. }