get_wx_list_logic.go 6.4 KB

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