wx_refresh_logic.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package WorkPhone
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/imroc/req/v3"
  6. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  7. "strconv"
  8. "time"
  9. "wechat-api/ent"
  10. "wechat-api/ent/wx"
  11. "wechat-api/internal/utils/dberrorhandler"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "github.com/zeromicro/go-zero/core/logx"
  15. )
  16. type WxRefreshLogic struct {
  17. logx.Logger
  18. ctx context.Context
  19. svcCtx *svc.ServiceContext
  20. }
  21. func NewWxRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxRefreshLogic {
  22. return &WxRefreshLogic{
  23. Logger: logx.WithContext(ctx),
  24. ctx: ctx,
  25. svcCtx: svcCtx}
  26. }
  27. func (l *WxRefreshLogic) WxRefresh() (resp *types.BaseMsgResp, err error) {
  28. var result types.WorkPhoneGetWeChatsResp
  29. client := req.C().DevMode()
  30. client.SetCommonRetryCount(2).
  31. SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
  32. SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
  33. res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=13")
  34. if err != nil {
  35. return
  36. }
  37. if !res.IsSuccessState() {
  38. err = fmt.Errorf("GetWeChats failed with status code %d", res.StatusCode)
  39. return
  40. }
  41. for _, account := range result.Data {
  42. wxinfo, err := l.svcCtx.DB.Wx.Query().
  43. Where(
  44. wx.WxidEQ(account.Wechatid),
  45. ).
  46. Only(l.ctx)
  47. if err != nil && !ent.IsNotFound(err) {
  48. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  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 = l.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(l.ctx)
  66. if err != nil {
  67. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  68. }
  69. } else {
  70. l.Debug("wxinfo is nil")
  71. _, err := l.svcCtx.DB.Wx.Create().
  72. SetServerID(0).
  73. SetPort(account.Deviceid).
  74. SetProcessID(strconv.FormatInt(account.Cid, 10)).
  75. SetWxid(account.Wechatid).
  76. SetAccount(account.Wechatno).
  77. SetHeadBig(account.Avatar).
  78. SetNickname(account.Wechatnick).
  79. SetStatus(status).
  80. SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
  81. Save(l.ctx)
  82. l.Error("wxinfo save", err)
  83. if err != nil {
  84. return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
  85. }
  86. }
  87. }
  88. return &types.BaseMsgResp{Msg: errormsg.Success}, nil
  89. return
  90. }