sync_wx.go 3.3 KB

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