qw_refresh_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package WorkPhone
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/imroc/req/v3"
  6. "strconv"
  7. "time"
  8. "wechat-api/ent"
  9. "wechat-api/ent/wx"
  10. "wechat-api/internal/utils/dberrorhandler"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "github.com/zeromicro/go-zero/core/logx"
  14. )
  15. type QwRefreshLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewQwRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QwRefreshLogic {
  21. return &QwRefreshLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx}
  25. }
  26. func (l *QwRefreshLogic) QwRefresh() (resp *types.BaseMsgResp, err error) {
  27. var result types.WorkPhoneGetWeChatsResp
  28. client := req.C().DevMode()
  29. client.SetCommonRetryCount(2).
  30. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  31. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  32. res, err := client.R().SetSuccessResult(&result).Post("http://wecom.gkscrm.com:15086/pc/GetWeChatsReq?id=19")
  33. if err != nil {
  34. return nil, err
  35. }
  36. if !res.IsSuccessState() {
  37. err = fmt.Errorf("GetWeChats failed with status code %d", res.StatusCode)
  38. return nil, err
  39. }
  40. for _, account := range result.Data {
  41. if account.Wechatid == "" {
  42. continue
  43. }
  44. wxinfo, err := l.svcCtx.DB.Wx.Query().
  45. Where(
  46. wx.WxidEQ(account.Wechatid),
  47. wx.Ctype(3),
  48. ).
  49. Only(l.ctx)
  50. if err != nil && !ent.IsNotFound(err) {
  51. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  52. }
  53. var status uint8
  54. if account.Isonline == 0 {
  55. status = 1
  56. } else {
  57. status = 0
  58. }
  59. if wxinfo != nil {
  60. err = l.svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
  61. SetServerID(0).
  62. SetPort(account.Deviceid).
  63. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  64. SetAccount(account.Wechatno).
  65. SetNickname(account.Wechatnick).
  66. SetHeadBig(account.Avatar).
  67. SetStatus(status).
  68. Exec(l.ctx)
  69. if err != nil {
  70. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  71. }
  72. } else {
  73. l.Debug("wxinfo is nil")
  74. _, err := l.svcCtx.DB.Wx.Create().
  75. SetServerID(0).
  76. SetPort(account.Deviceid).
  77. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  78. SetWxid(account.Wechatid).
  79. SetAccount(account.Wechatno).
  80. SetHeadBig(account.Avatar).
  81. SetNickname(account.Wechatnick).
  82. SetStatus(status).
  83. SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
  84. Save(l.ctx)
  85. l.Error("wxinfo save", err)
  86. if err != nil {
  87. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  88. }
  89. }
  90. }
  91. l.svcCtx.Rds.Del(l.ctx, "crontask_wx_server_info")
  92. return resp, nil
  93. }