get_wx_list_logic.go 5.6 KB

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