get_wx_list_logic.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package Wx
  2. import (
  3. "context"
  4. "fmt"
  5. reqv3 "github.com/imroc/req/v3"
  6. "github.com/suyuan32/simple-admin-common/utils/pointy"
  7. "github.com/suyuan32/simple-admin-core/rpc/types/core"
  8. "strings"
  9. "wechat-api/ent"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/ent/usagetotal"
  12. "wechat-api/ent/wx"
  13. "wechat-api/hook"
  14. "wechat-api/internal/svc"
  15. "wechat-api/internal/types"
  16. "wechat-api/internal/utils/dberrorhandler"
  17. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  18. "github.com/zeromicro/go-zero/core/logx"
  19. )
  20. type GetWxListLogic struct {
  21. ctx context.Context
  22. svcCtx *svc.ServiceContext
  23. logx.Logger
  24. }
  25. func NewGetWxListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWxListLogic {
  26. return &GetWxListLogic{
  27. ctx: ctx,
  28. svcCtx: svcCtx,
  29. Logger: logx.WithContext(ctx),
  30. }
  31. }
  32. func (l *GetWxListLogic) GetWxList(req *types.WxListReq) (*types.WxListResp, error) {
  33. organizationId := l.ctx.Value("organizationId").(uint64)
  34. isAdmin := l.ctx.Value("isAdmin").(bool)
  35. servers, err := l.svcCtx.DB.Server.Query().All(l.ctx)
  36. serverSet := make(map[uint64]*ent.Server, len(servers))
  37. serverSet[0] = &ent.Server{
  38. ID: 0,
  39. Name: "工作手机",
  40. Status: 1,
  41. }
  42. for _, s := range servers {
  43. serverSet[s.ID] = s
  44. }
  45. var predicates []predicate.Wx
  46. if req.Ctype != nil && *req.Ctype > 0 {
  47. predicates = append(predicates, wx.Ctype(*req.Ctype))
  48. }
  49. if !isAdmin {
  50. predicates = append(predicates, wx.OrganizationIDEQ(organizationId))
  51. } else {
  52. if req.OrganizationId != nil {
  53. predicates = append(predicates, wx.OrganizationIDEQ(*req.OrganizationId))
  54. }
  55. if req.OrganizationName != nil {
  56. departmentList, _ := l.svcCtx.CoreRpc.GetDepartmentList(l.ctx, &core.DepartmentListReq{Name: req.OrganizationName})
  57. organizationIds := make([]uint64, 0)
  58. for _, department := range departmentList.Data {
  59. organizationIds = append(organizationIds, *department.Id)
  60. }
  61. predicates = append(predicates, wx.OrganizationIDIn(organizationIds...))
  62. }
  63. }
  64. if req.ServerId != nil {
  65. predicates = append(predicates, wx.ServerIDEQ(*req.ServerId))
  66. }
  67. if req.Port != nil {
  68. predicates = append(predicates, wx.PortContains(*req.Port))
  69. }
  70. if req.ProcessId != nil {
  71. predicates = append(predicates, wx.ProcessIDContains(*req.ProcessId))
  72. }
  73. if req.Callback != nil {
  74. predicates = append(predicates, wx.CallbackContains(*req.Callback))
  75. }
  76. data, err := l.svcCtx.DB.Wx.Query().Where(predicates...).Order(ent.Desc(wx.FieldOrganizationID)).WithAgent().WithServer().Page(l.ctx, req.Page, req.PageSize)
  77. if err != nil {
  78. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  79. }
  80. resp := &types.WxListResp{}
  81. resp.Msg = errormsg.Success
  82. resp.Data.Total = data.PageDetails.Total
  83. if req.Ctype != nil && *req.Ctype == 3 {
  84. for _, v := range data.List {
  85. // 创建 hookClient 客户端
  86. serverInfo := serverSet[v.ServerID]
  87. serverName := serverInfo.Name
  88. processID := v.ProcessID
  89. wxid := v.Wxid
  90. account := v.Account
  91. nickname := v.Nickname
  92. tel := v.Tel
  93. headBig := v.HeadBig
  94. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  95. if err != nil {
  96. l.Error("获取部门信息失败", err)
  97. }
  98. totalTokens := uint64(0)
  99. usageTotalInfo, _ := l.svcCtx.DB.UsageTotal.Query().
  100. Where(
  101. usagetotal.BotID(wxid),
  102. ).
  103. Only(l.ctx)
  104. if usageTotalInfo != nil {
  105. totalTokens = usageTotalInfo.TotalTokens
  106. }
  107. var agent types.AgentInfo
  108. if v.Edges.Agent != nil {
  109. agent = types.AgentInfo{
  110. BaseIDInfo: types.BaseIDInfo{
  111. Id: &v.AgentID,
  112. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  113. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  114. },
  115. Name: &v.Edges.Agent.Name,
  116. Role: &v.Edges.Agent.Role,
  117. Status: &v.Edges.Agent.Status,
  118. Background: &v.Edges.Agent.Background,
  119. Examples: &v.Edges.Agent.Examples,
  120. }
  121. }
  122. resp.Data.Data = append(resp.Data.Data,
  123. types.WxInfo{
  124. BaseIDInfo: types.BaseIDInfo{
  125. Id: &v.ID,
  126. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  127. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  128. },
  129. Status: &v.Status,
  130. ServerId: &v.ServerID,
  131. ServerName: &serverName,
  132. Port: &v.Port,
  133. ProcessId: &processID,
  134. Callback: &v.Callback,
  135. Wxid: &wxid,
  136. Account: &account,
  137. Nickname: &nickname,
  138. Tel: &tel,
  139. HeadBig: &headBig,
  140. OrganizationId: &v.OrganizationID,
  141. OrganizationName: departmentInfo.Name,
  142. AgentId: &v.AgentID,
  143. AgentInfo: &agent,
  144. ApiBase: &v.APIBase,
  145. ApiKey: &v.APIKey,
  146. TotalTokens: &totalTokens,
  147. Ctype: &v.Ctype,
  148. })
  149. }
  150. } else {
  151. var result types.WorkPhoneGetWeChatsResp
  152. res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=0")
  153. if err != nil {
  154. return nil, err
  155. }
  156. if !res.IsSuccessState() {
  157. err = fmt.Errorf("GetWeChats failed with status code %d", res.StatusCode)
  158. return nil, err
  159. }
  160. workphoneWxList := make(map[string]types.WorkPhoneWeChat, len(result.Data))
  161. for _, v := range result.Data {
  162. if v.Wechatid == nil || *v.Wechatid == "" {
  163. continue
  164. }
  165. workphoneWxList[*v.Wechatid] = v
  166. }
  167. for _, v := range data.List {
  168. // 创建 hookClient 客户端
  169. serverInfo := serverSet[v.ServerID]
  170. serverName := serverInfo.Name
  171. if strings.HasPrefix(strings.ToUpper(v.Port), "PC-") {
  172. serverName = "本地版"
  173. }
  174. var loginStatus uint8 = 0
  175. hookClient := hook.NewHook(serverInfo.PrivateIP, serverInfo.AdminPort, v.Port)
  176. if v.ServerID > 0 {
  177. if serverInfo.Status == 1 {
  178. // 获取登录状态
  179. loginInfo, err := hookClient.IsLoginStatus()
  180. if err != nil {
  181. l.Error("退出登录失败", err)
  182. } else {
  183. if loginInfo.Onlinestatus == "3" {
  184. loginStatus = 1
  185. }
  186. }
  187. }
  188. } else {
  189. workphoneWx, exists := workphoneWxList[v.Wxid]
  190. if exists && *workphoneWx.Isonline == 0 {
  191. loginStatus = 1
  192. } else {
  193. loginStatus = 0
  194. }
  195. }
  196. processID := v.ProcessID
  197. wxid := v.Wxid
  198. account := v.Account
  199. nickname := v.Nickname
  200. tel := v.Tel
  201. headBig := v.HeadBig
  202. departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID})
  203. if err != nil {
  204. l.Error("获取部门信息失败", err)
  205. }
  206. if v.ServerID > 0 {
  207. if loginStatus == 1 {
  208. // 如果处于登录状态,获取登录信息
  209. wxInfo, _ := hookClient.GetSelfLoginInfo()
  210. if err != nil {
  211. l.Error("获取登录信息失败", err)
  212. } else {
  213. if wxid != wxInfo.Wxid {
  214. l.svcCtx.Rds.HDel(l.ctx, "wx_info", wxid)
  215. l.svcCtx.Rds.HDel(l.ctx, "wx_info", wxInfo.Wxid)
  216. l.svcCtx.Rds.HDel(l.ctx, "crontask_wx_server_info", wxid)
  217. l.svcCtx.Rds.HDel(l.ctx, "crontask_wx_server_info", wxInfo.Wxid)
  218. }
  219. processID = wxInfo.ProcessID
  220. wxid = wxInfo.Wxid
  221. account = wxInfo.Account
  222. nickname = wxInfo.Nickname
  223. tel = wxInfo.Tel
  224. headBig = wxInfo.HeadBig
  225. _ = l.svcCtx.DB.Wx.UpdateOneID(v.ID).
  226. SetNotNilStatus(&loginStatus).
  227. SetNotNilProcessID(&wxInfo.ProcessID).
  228. SetNotNilWxid(&wxInfo.Wxid).
  229. SetNotNilAccount(&wxInfo.Account).
  230. SetNotNilNickname(&wxInfo.Nickname).
  231. SetNotNilTel(&wxInfo.Tel).
  232. SetNotNilHeadBig(&wxInfo.HeadBig).
  233. Exec(l.ctx)
  234. }
  235. } else {
  236. if loginStatus != v.Status {
  237. _ = l.svcCtx.DB.Wx.UpdateOneID(v.ID).
  238. SetNotNilStatus(&loginStatus).
  239. Exec(l.ctx)
  240. }
  241. }
  242. }
  243. totalTokens := uint64(0)
  244. totalCredits := float64(0)
  245. usageTotalInfo, _ := l.svcCtx.DB.UsageTotal.Query().
  246. Where(
  247. usagetotal.BotID(wxid),
  248. ).
  249. Only(l.ctx)
  250. if usageTotalInfo != nil {
  251. totalTokens = usageTotalInfo.TotalTokens
  252. totalCredits = usageTotalInfo.Credits
  253. }
  254. var agent types.AgentInfo
  255. if v.Edges.Agent != nil {
  256. agent = types.AgentInfo{
  257. BaseIDInfo: types.BaseIDInfo{
  258. Id: &v.AgentID,
  259. CreatedAt: pointy.GetPointer(v.Edges.Agent.CreatedAt.UnixMilli()),
  260. UpdatedAt: pointy.GetPointer(v.Edges.Agent.UpdatedAt.UnixMilli()),
  261. },
  262. Name: &v.Edges.Agent.Name,
  263. Role: &v.Edges.Agent.Role,
  264. Status: &v.Edges.Agent.Status,
  265. Background: &v.Edges.Agent.Background,
  266. Examples: &v.Edges.Agent.Examples,
  267. }
  268. }
  269. resp.Data.Data = append(resp.Data.Data,
  270. types.WxInfo{
  271. BaseIDInfo: types.BaseIDInfo{
  272. Id: &v.ID,
  273. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  274. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  275. },
  276. Status: &loginStatus,
  277. ServerId: &v.ServerID,
  278. ServerName: &serverName,
  279. Port: &v.Port,
  280. ProcessId: &processID,
  281. Callback: &v.Callback,
  282. Wxid: &wxid,
  283. Account: &account,
  284. Nickname: &nickname,
  285. Tel: &tel,
  286. HeadBig: &headBig,
  287. OrganizationId: &v.OrganizationID,
  288. OrganizationName: departmentInfo.Name,
  289. AgentId: &v.AgentID,
  290. AgentInfo: &agent,
  291. ApiBase: &v.APIBase,
  292. ApiKey: &v.APIKey,
  293. TotalTokens: &totalTokens,
  294. Ctype: &v.Ctype,
  295. Credits: &totalCredits,
  296. })
  297. }
  298. }
  299. return resp, nil
  300. }