get_wx_list_logic.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package Wx
  2. import (
  3. "context"
  4. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  5. "wechat-api/ent"
  6. "wechat-api/ent/predicate"
  7. "wechat-api/ent/wx"
  8. "wechat-api/hook"
  9. "wechat-api/internal/svc"
  10. "wechat-api/internal/types"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  13. "github.com/suyuan32/simple-admin-common/utils/pointy"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type GetWxListLogic struct {
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. logx.Logger
  20. }
  21. func NewGetWxListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxListLogic {
  22. return &GetWxListLogic{
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. Logger: logx.WithContext(ctx),
  26. }
  27. }
  28. func (l *GetWxListLogic) GetWxList(req *types.WxListReq) (*types.WxListResp, error) {
  29. organizationId := l.ctx.Value("organizationId").(uint64)
  30. isAdmin := l.ctx.Value("isAdmin").(bool)
  31. servers, err := l.svcCtx.DB.Server.Query().All(l.ctx)
  32. serverSet := make(map[uint64]*ent.Server, len(servers))
  33. for _, s := range servers {
  34. serverSet[s.ID] = s
  35. }
  36. var predicates []predicate.Wx
  37. if !isAdmin {
  38. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  39. } else {
  40. if req.OrganizationId != nil {
  41. predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
  42. }
  43. if req.OrganizationName != nil {
  44. departmentList, _ := l.svcCtx.CoreRpc.GetDepartmentList(l.ctx, &core.DepartmentListReq{Name: req.OrganizationName})
  45. organizationIds := make([]uint64, 0)
  46. for _, department := range departmentList.Data {
  47. organizationIds = append(organizationIds, *department.Id)
  48. }
  49. predicates = append(predicates, wx.OrganizationIDIn(organizationIds...))
  50. }
  51. }
  52. if req.ServerId != nil {
  53. predicates = append(predicates, wx.ServerIDEQ(*req.ServerId))
  54. }
  55. if req.Port != nil {
  56. predicates = append(predicates, wx.PortContains(*req.Port))
  57. }
  58. if req.ProcessId != nil {
  59. predicates = append(predicates, wx.ProcessIDContains(*req.ProcessId))
  60. }
  61. if req.Callback != nil {
  62. predicates = append(predicates, wx.CallbackContains(*req.Callback))
  63. }
  64. data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Order(ent.Desc(wx.FieldOrganizationID)).WithAgent().WithServer().Page(l.ctx, req.Page, req.PageSize)
  65. if err != nil {
  66. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  67. }
  68. resp := &types.WxListResp{}
  69. resp.Msg = errormsg.Success
  70. resp.Data.Total = data.PageDetails.Total
  71. for _, v := range data.List {
  72. // 创建 hookClient 客户端
  73. serverInfo := serverSet[v.ServerID]
  74. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, v.Port)
  75. var loginStatus uint8 = 0
  76. if serverInfo.Status == 1 {
  77. // 获取登录状态
  78. loginInfo, err := hookClient.IsLoginStatus()
  79. if err != nil {
  80. l.Error("退出登录失败", err)
  81. } else {
  82. if loginInfo.Onlinestatus == "3" {
  83. loginStatus = 1
  84. }
  85. }
  86. }
  87. processID := v.ProcessID
  88. wxid := v.Wxid
  89. account := v.Account
  90. nickname := v.Nickname
  91. tel := v.Tel
  92. headBig := v.HeadBig
  93. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  94. if err != nil {
  95. l.Error("获取部门信息失败", err)
  96. }
  97. if loginStatus == 1 {
  98. // 如果处于登录状态,获取登录信息
  99. wxInfo, _ := hookClient.GetSelfLoginInfo()
  100. if err != nil {
  101. l.Error("获取登录信息失败", err)
  102. } else {
  103. processID = wxInfo.ProcessID
  104. wxid = wxInfo.Wxid
  105. account = wxInfo.Account
  106. nickname = wxInfo.Nickname
  107. tel = wxInfo.Tel
  108. headBig = wxInfo.HeadBig
  109. _ = l.svcCtx.DB.Wx.UpdateOneID(v.ID).
  110. SetNotNilStatus(&loginStatus).
  111. SetNotNilProcessID(&wxInfo.ProcessID).
  112. SetNotNilWxid(&wxInfo.Wxid).
  113. SetNotNilAccount(&wxInfo.Account).
  114. SetNotNilNickname(&wxInfo.Nickname).
  115. SetNotNilTel(&wxInfo.Tel).
  116. SetNotNilHeadBig(&wxInfo.HeadBig).
  117. Exec(l.ctx)
  118. }
  119. } else {
  120. if loginStatus != v.Status {
  121. _ = l.svcCtx.DB.Wx.UpdateOneID(v.ID).
  122. SetNotNilStatus(&loginStatus).
  123. Exec(l.ctx)
  124. }
  125. }
  126. var agent types.AgentInfo
  127. if v.Edges.Agent != nil {
  128. agent = types.AgentInfo{
  129. BaseIDInfo: types.BaseIDInfo{
  130. Id: &v.AgentID,
  131. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  132. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  133. },
  134. Name: &v.Edges.Agent.Name,
  135. Role: &v.Edges.Agent.Role,
  136. Status: &v.Edges.Agent.Status,
  137. Background: &v.Edges.Agent.Background,
  138. Examples: &v.Edges.Agent.Examples,
  139. }
  140. }
  141. resp.Data.Data = append(resp.Data.Data,
  142. types.WxInfo{
  143. BaseIDInfo: types.BaseIDInfo{
  144. Id: &v.ID,
  145. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  146. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  147. },
  148. Status: &loginStatus,
  149. ServerId: &v.ServerID,
  150. ServerName: &serverInfo.Name,
  151. Port: &v.Port,
  152. ProcessId: &processID,
  153. Callback: &v.Callback,
  154. Wxid: &wxid,
  155. Account: &account,
  156. Nickname: &nickname,
  157. Tel: &tel,
  158. HeadBig: &headBig,
  159. OrganizationId: &v.OrganizationID,
  160. OrganizationName: departmentInfo.Name,
  161. AgentId: &v.AgentID,
  162. AgentInfo: &agent,
  163. ApiBase: &v.APIBase,
  164. ApiKey: &v.APIKey,
  165. })
  166. }
  167. return resp, nil
  168. }