123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- package wechat
- import (
- "context"
- "encoding/json"
- "github.com/imroc/req/v3"
- "github.com/zeromicro/go-zero/core/logx"
- "strconv"
- "time"
- "wechat-api/ent"
- "wechat-api/ent/wx"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- )
- func SyncAllWx(svcCtx *svc.ServiceContext) {
- // 获取微信列表
- 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=0")
- if err != nil {
- logx.Error("syncWx: ", err)
- return
- }
- if !res.IsSuccessState() {
- logx.Error("GetWeChats failed with status code: ", res.StatusCode)
- return
- }
- // 遍历微信列表
- for _, account := range result.Data {
- if account.Wechatid == "" {
- continue
- }
- wxinfo, err := svcCtx.DB.Wx.Query().
- Where(
- wx.And(
- wx.Or(
- wx.WxidEQ(account.Wechatid),
- wx.PortEQ(account.Deviceid),
- ),
- wx.CtypeEQ(1),
- ),
- ).
- Only(context.TODO())
- if err != nil && !ent.IsNotFound(err) {
- logx.Error("syncWx: ", err)
- return
- }
- var status uint8
- if account.Isonline == 0 {
- status = 1
- } else {
- status = 0
- }
- if wxinfo != nil {
- err = 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(context.TODO())
- if err != nil {
- logx.Error("syncWx: ", err)
- return
- }
- } else {
- logx.Debug("wxinfo is nil")
- _, err := 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(context.TODO())
- if err != nil {
- logx.Error("syncWx: ", err)
- return
- }
- }
- data := map[string]interface{}{
- "MsgType": "TriggerFriendPushTask",
- "Content": map[string]interface{}{
- "WeChatId": account.Wechatid,
- },
- }
- jsonStr, err := json.Marshal(data)
- err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
- if err != nil {
- logx.Error("syncWx: ", err)
- return
- }
- data_chatroom := map[string]interface{}{
- "MsgType": "TriggerChatroomPushTask",
- "Content": map[string]interface{}{
- "WeChatId": account.Wechatid,
- "Flag": 1,
- },
- }
- jsonStrChatroom, err := json.Marshal(data_chatroom)
- err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStrChatroom))
- }
- }
|