|
@@ -0,0 +1,104 @@
|
|
|
+package crontask
|
|
|
+
|
|
|
+import (
|
|
|
+ "encoding/json"
|
|
|
+ "github.com/imroc/req/v3"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
+ "wechat-api/ent"
|
|
|
+ "wechat-api/ent/wx"
|
|
|
+ "wechat-api/internal/types"
|
|
|
+)
|
|
|
+
|
|
|
+func (l *CronTask) syncWx() {
|
|
|
+ // 获取微信列表
|
|
|
+ var result types.WorkPhoneGetWeChatsResp
|
|
|
+ client := req.C().DevMode()
|
|
|
+ client.SetCommonRetryCount(2).
|
|
|
+ SetCommonRetryBackoffInterval(1*time.Second, 5*time.Second).
|
|
|
+ SetCommonRetryFixedInterval(2 * time.Second).SetTimeout(30 * time.Second)
|
|
|
+ res, err := client.R().SetSuccessResult(&result).Post("http://chat.gkscrm.com:13086/pc/GetWeChatsReq?id=13")
|
|
|
+ if err != nil {
|
|
|
+ l.Error("syncWx: ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if !res.IsSuccessState() {
|
|
|
+ l.Error("GetWeChats failed with status code: ", res.StatusCode)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 遍历微信列表
|
|
|
+ for _, account := range result.Data {
|
|
|
+ if account.Wechatid == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ wxinfo, err := l.svcCtx.DB.Wx.Query().
|
|
|
+ Where(
|
|
|
+ wx.And(
|
|
|
+ wx.Or(
|
|
|
+ wx.WxidEQ(account.Wechatid),
|
|
|
+ wx.PortEQ(account.Deviceid),
|
|
|
+ ),
|
|
|
+ wx.CtypeEQ(1),
|
|
|
+ ),
|
|
|
+ ).
|
|
|
+ Only(l.ctx)
|
|
|
+
|
|
|
+ if err != nil && !ent.IsNotFound(err) {
|
|
|
+ l.Error("syncWx: ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var status uint8
|
|
|
+ if account.Isonline == 0 {
|
|
|
+ status = 1
|
|
|
+ } else {
|
|
|
+ status = 0
|
|
|
+ }
|
|
|
+ if wxinfo != nil {
|
|
|
+ err = l.svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
|
|
|
+ SetServerID(0).
|
|
|
+ SetPort(account.Deviceid).
|
|
|
+ SetProcessID(strconv.FormatInt(account.Cid, 10)).
|
|
|
+ SetAccount(account.Wechatno).
|
|
|
+ SetNickname(account.Wechatnick).
|
|
|
+ SetHeadBig(account.Avatar).
|
|
|
+ SetStatus(status).
|
|
|
+ Exec(l.ctx)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ l.Error("syncWx: ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ l.Debug("wxinfo is nil")
|
|
|
+ _, err := l.svcCtx.DB.Wx.Create().
|
|
|
+ SetServerID(0).
|
|
|
+ SetPort(account.Deviceid).
|
|
|
+ SetProcessID(strconv.FormatInt(account.Cid, 10)).
|
|
|
+ SetWxid(account.Wechatid).
|
|
|
+ SetAccount(account.Wechatno).
|
|
|
+ SetHeadBig(account.Avatar).
|
|
|
+ SetNickname(account.Wechatnick).
|
|
|
+ SetStatus(status).
|
|
|
+ SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
|
|
|
+ Save(l.ctx)
|
|
|
+ if err != nil {
|
|
|
+ l.Error("syncWx: ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ data := map[string]interface{}{
|
|
|
+ "MsgType": "TriggerFriendPushTask",
|
|
|
+ "Content": map[string]interface{}{
|
|
|
+ "WeChatId": account.Wechatid,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ jsonStr, err := json.Marshal(data)
|
|
|
+ err = l.svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
|
|
|
+ if err != nil {
|
|
|
+ l.Error("syncWx: ", err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|