get_token_list_logic.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package token
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  5. "wechat-api/ent/predicate"
  6. "wechat-api/ent/token"
  7. "wechat-api/internal/svc"
  8. "wechat-api/internal/types"
  9. "wechat-api/internal/utils/dberrorhandler"
  10. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  11. "github.com/suyuan32/simple-admin-common/utils/pointy"
  12. "github.com/zeromicro/go-zero/core/logx"
  13. )
  14. type GetTokenListLogic struct {
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. logx.Logger
  18. }
  19. func NewGetTokenListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTokenListLogic {
  20. return &GetTokenListLogic{
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. Logger: logx.WithContext(ctx),
  24. }
  25. }
  26. func (l *GetTokenListLogic) GetTokenList(req *types.TokenListReq) (*types.TokenListResp, error) {
  27. organizationId := l.ctx.Value("organizationId").(uint64)
  28. isAdmin := l.ctx.Value("isAdmin").(bool)
  29. var predicates []predicate.Token
  30. if !isAdmin {
  31. predicates = append(predicates, token.OrganizationIDEQ(organizationId))
  32. } else {
  33. if req.OrganizationId != nil {
  34. predicates = append(predicates, token.OrganizationIDEQ(*req.OrganizationId))
  35. }
  36. if req.OrganizationName != nil {
  37. departmentList, _ := l.svcCtx.CoreRpc.GetDepartmentList(l.ctx, &core.DepartmentListReq{Name: req.OrganizationName})
  38. organizationIds := make([]uint64, 0)
  39. for _, department := range departmentList.Data {
  40. organizationIds = append(organizationIds, *department.Id)
  41. }
  42. predicates = append(predicates, token.OrganizationIDIn(organizationIds...))
  43. }
  44. }
  45. if req.Token != nil {
  46. predicates = append(predicates, token.TokenContains(*req.Token))
  47. }
  48. if req.Mac != nil {
  49. predicates = append(predicates, token.MACContains(*req.Mac))
  50. }
  51. data, err := l.svcCtx.DB.Token.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize)
  52. if err != nil {
  53. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  54. }
  55. resp := &types.TokenListResp{}
  56. resp.Msg = errormsg.Success
  57. resp.Data.Total = data.PageDetails.Total
  58. for _, v := range data.List {
  59. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  60. if err != nil {
  61. l.Error("获取部门信息失败", err)
  62. }
  63. var agent types.AgentInfo
  64. if v.AgentID == 0 {
  65. agentName := "定制 AI 角色"
  66. agent = types.AgentInfo{
  67. Name: &agentName,
  68. }
  69. } else {
  70. if v.Edges.Agent != nil {
  71. agent = types.AgentInfo{
  72. BaseIDInfo: types.BaseIDInfo{
  73. Id: &v.AgentID,
  74. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  75. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  76. },
  77. Name: &v.Edges.Agent.Name,
  78. Role: &v.Edges.Agent.Role,
  79. Status: &v.Edges.Agent.Status,
  80. Background: &v.Edges.Agent.Background,
  81. Examples: &v.Edges.Agent.Examples,
  82. }
  83. }
  84. }
  85. expireAtStr := v.ExpireAt.Format("2006-01-02 15:04:05")
  86. resp.Data.Data = append(resp.Data.Data,
  87. types.TokenInfo{
  88. BaseIDInfo: types.BaseIDInfo{
  89. Id: &v.ID,
  90. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  91. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  92. },
  93. ExpireAt: pointy.GetUnixMilliPointer(v.ExpireAt.UnixMilli()),
  94. ExpireAtStr: &expireAtStr,
  95. Token: &v.Token,
  96. Mac: &v.MAC,
  97. OrganizationId: &v.OrganizationID,
  98. OrganizationName: departmentInfo.Name,
  99. AgentId: &v.AgentID,
  100. AgentInfo: &agent,
  101. CustomAgentBase: &v.CustomAgentBase,
  102. CustomAgentKey: &v.CustomAgentKey,
  103. OpenaiBase: &v.OpenaiBase,
  104. OpenaiKey: &v.OpenaiKey,
  105. })
  106. }
  107. return resp, nil
  108. }