sync_wx.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package wechat
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/imroc/req/v3"
  6. "github.com/zeromicro/go-zero/core/logx"
  7. "strconv"
  8. "time"
  9. "wechat-api/ent"
  10. "wechat-api/ent/wx"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. )
  14. func SyncAllWx(svcCtx *svc.ServiceContext) {
  15. // 获取微信列表
  16. var result types.WorkPhoneGetWeChatsResp
  17. client := req.C().DevMode()
  18. client.SetCommonRetryCount(2).
  19. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  20. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  21. res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=0")
  22. if err != nil {
  23. logx.Error("syncWx: ", err)
  24. return
  25. }
  26. if !res.IsSuccessState() {
  27. logx.Error("GetWeChats failed with status code: ", res.StatusCode)
  28. return
  29. }
  30. // 遍历微信列表
  31. for _, account := range result.Data {
  32. if account.Wechatid == "" {
  33. continue
  34. }
  35. wxinfo, err := svcCtx.DB.Wx.Query().
  36. Where(
  37. wx.And(
  38. wx.Or(
  39. wx.WxidEQ(account.Wechatid),
  40. wx.PortEQ(account.Deviceid),
  41. ),
  42. wx.CtypeEQ(1),
  43. ),
  44. ).
  45. Only(context.TODO())
  46. if err != nil && !ent.IsNotFound(err) {
  47. logx.Error("syncWx: ", err)
  48. return
  49. }
  50. var status uint8
  51. if account.Isonline == 0 {
  52. status = 1
  53. } else {
  54. status = 0
  55. }
  56. if wxinfo != nil {
  57. err = svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
  58. SetServerID(0).
  59. SetPort(account.Deviceid).
  60. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  61. SetAccount(account.Wechatno).
  62. SetNickname(account.Wechatnick).
  63. SetHeadBig(account.Avatar).
  64. SetStatus(status).
  65. Exec(context.TODO())
  66. if err != nil {
  67. logx.Error("syncWx: ", err)
  68. return
  69. }
  70. } else {
  71. logx.Debug("wxinfo is nil")
  72. _, err := svcCtx.DB.Wx.Create().
  73. SetServerID(0).
  74. SetPort(account.Deviceid).
  75. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  76. SetWxid(account.Wechatid).
  77. SetAccount(account.Wechatno).
  78. SetHeadBig(account.Avatar).
  79. SetNickname(account.Wechatnick).
  80. SetStatus(status).
  81. SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
  82. Save(context.TODO())
  83. if err != nil {
  84. logx.Error("syncWx: ", err)
  85. return
  86. }
  87. }
  88. data := map[string]interface{}{
  89. "MsgType": "TriggerFriendPushTask",
  90. "Content": map[string]interface{}{
  91. "WeChatId": account.Wechatid,
  92. },
  93. }
  94. jsonStr, err := json.Marshal(data)
  95. err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
  96. if err != nil {
  97. logx.Error("syncWx: ", err)
  98. return
  99. }
  100. data_chatroom := map[string]interface{}{
  101. "MsgType": "TriggerChatroomPushTask",
  102. "Content": map[string]interface{}{
  103. "WeChatId": account.Wechatid,
  104. "Flag": 1,
  105. },
  106. }
  107. jsonStrChatroom, err := json.Marshal(data_chatroom)
  108. err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStrChatroom))
  109. }
  110. }