sync_wx.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package wechat
  2. import (
  3. "context"
  4. "fmt"
  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/types"
  12. )
  13. type SyncWx struct {
  14. Ctx context.Context
  15. DB *ent.Client
  16. }
  17. func (s *SyncWx) SyncAll() {
  18. // 获取微信列表
  19. var result types.WorkPhoneGetWeChatsResp
  20. client := req.C().DevMode()
  21. client.SetCommonRetryCount(2).
  22. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  23. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  24. res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=0")
  25. if err != nil {
  26. logx.Error("syncWx: ", err)
  27. return
  28. }
  29. if !res.IsSuccessState() {
  30. logx.Error("GetWeChats failed with status code: ", res.StatusCode)
  31. return
  32. }
  33. // 遍历微信列表
  34. for _, account := range result.Data {
  35. if account.Wechatid == nil || *account.Wechatid == "" {
  36. continue
  37. }
  38. wxinfo, err := s.DB.Wx.Query().
  39. Where(
  40. wx.And(
  41. wx.WxidEQ(*account.Wechatid),
  42. wx.CtypeEQ(1),
  43. ),
  44. ).
  45. Only(s.Ctx)
  46. if err != nil && !ent.IsNotFound(err) {
  47. logx.Error("syncWx: ", err)
  48. return
  49. }
  50. var status uint8
  51. logx.Info(fmt.Printf("*account.Isonline: %d\n", *account.Isonline))
  52. if *account.Isonline == 0 {
  53. status = 1
  54. } else {
  55. status = 0
  56. }
  57. if wxinfo != nil {
  58. tx, err := s.DB.Tx(s.Ctx)
  59. if err != nil {
  60. continue
  61. }
  62. defer tx.Rollback()
  63. change := WxSafeChange{Ctx: s.Ctx, Tx: tx, WxId: wxinfo.Wxid, OrganizationId: wxinfo.OrganizationID}
  64. err = change.KeepAllSafe()
  65. if err != nil {
  66. _ = tx.Rollback()
  67. continue
  68. }
  69. logx.Info(fmt.Printf("wxinfo is not nil, account: %s\n", *account.Wechatid))
  70. logx.Info(fmt.Printf("wxinfo is not nil, account: %s\n", *account.Wechatnick))
  71. err = tx.Wx.UpdateOneID(wxinfo.ID).
  72. SetServerID(0).
  73. SetPort(*account.Deviceid).
  74. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  75. SetNotNilAccount(account.Wechatno).
  76. SetNotNilNickname(account.Wechatnick).
  77. SetNotNilHeadBig(account.Avatar).
  78. SetStatus(status).
  79. Exec(s.Ctx)
  80. if err != nil {
  81. _ = tx.Rollback()
  82. logx.Error("syncWx: ", err)
  83. continue
  84. }
  85. err = tx.Commit()
  86. if err != nil {
  87. logx.Error("syncWx: ", err)
  88. continue
  89. }
  90. } else {
  91. logx.Error(fmt.Printf("wxinfo is nil, account: %+v\n", account))
  92. _, err := s.DB.Wx.Create().
  93. SetServerID(0).
  94. SetPort(*account.Deviceid).
  95. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  96. SetWxid(*account.Wechatid).
  97. SetNotNilAccount(account.Wechatno).
  98. SetNotNilHeadBig(account.Avatar).
  99. SetNotNilNickname(account.Wechatnick).
  100. SetStatus(status).
  101. SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
  102. Save(s.Ctx)
  103. if err != nil {
  104. logx.Error("syncWx: ", err)
  105. continue
  106. }
  107. }
  108. }
  109. }