|
@@ -9,11 +9,15 @@ import (
|
|
|
"time"
|
|
|
"wechat-api/ent"
|
|
|
"wechat-api/ent/wx"
|
|
|
- "wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
|
)
|
|
|
|
|
|
-func SyncAllWx(svcCtx *svc.ServiceContext) {
|
|
|
+type SyncAllWx struct {
|
|
|
+ Ctx context.Context
|
|
|
+ DB *ent.Client
|
|
|
+}
|
|
|
+
|
|
|
+func (s *SyncAllWx) SyncAll() {
|
|
|
// 获取微信列表
|
|
|
var result types.WorkPhoneGetWeChatsResp
|
|
|
client := req.C().DevMode()
|
|
@@ -35,19 +39,20 @@ func SyncAllWx(svcCtx *svc.ServiceContext) {
|
|
|
if account.Wechatid == nil || *account.Wechatid == "" {
|
|
|
continue
|
|
|
}
|
|
|
- wxinfo, err := svcCtx.DB.Wx.Query().
|
|
|
+ wxinfo, err := s.DB.Wx.Query().
|
|
|
Where(
|
|
|
wx.And(
|
|
|
wx.WxidEQ(*account.Wechatid),
|
|
|
wx.CtypeEQ(1),
|
|
|
),
|
|
|
).
|
|
|
- Only(context.TODO())
|
|
|
+ Only(s.Ctx)
|
|
|
|
|
|
if err != nil && !ent.IsNotFound(err) {
|
|
|
logx.Error("syncWx: ", err)
|
|
|
return
|
|
|
}
|
|
|
+
|
|
|
var status uint8
|
|
|
logx.Info(fmt.Printf("*account.Isonline: %d\n", *account.Isonline))
|
|
|
if *account.Isonline == 0 {
|
|
@@ -57,9 +62,20 @@ func SyncAllWx(svcCtx *svc.ServiceContext) {
|
|
|
}
|
|
|
|
|
|
if wxinfo != nil {
|
|
|
+ tx, err := s.DB.Tx(s.Ctx)
|
|
|
+ if err != nil {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ defer tx.Rollback()
|
|
|
+ change := WxSafeChange{Ctx: s.Ctx, Tx: tx, WxId: wxinfo.Wxid, OrganizationId: wxinfo.OrganizationID}
|
|
|
+ err = change.KeepAllSafe()
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ continue
|
|
|
+ }
|
|
|
logx.Info(fmt.Printf("wxinfo is not nil, account: %s\n", *account.Wechatid))
|
|
|
logx.Info(fmt.Printf("wxinfo is not nil, account: %s\n", *account.Wechatnick))
|
|
|
- err = svcCtx.DB.Wx.UpdateOneID(wxinfo.ID).
|
|
|
+ err = tx.Wx.UpdateOneID(wxinfo.ID).
|
|
|
SetServerID(0).
|
|
|
SetPort(*account.Deviceid).
|
|
|
SetProcessID(strconv.FormatInt(account.Cid, 10)).
|
|
@@ -67,14 +83,20 @@ func SyncAllWx(svcCtx *svc.ServiceContext) {
|
|
|
SetNotNilNickname(account.Wechatnick).
|
|
|
SetNotNilHeadBig(account.Avatar).
|
|
|
SetStatus(status).
|
|
|
- Exec(context.TODO())
|
|
|
+ Exec(s.Ctx)
|
|
|
+ if err != nil {
|
|
|
+ _ = tx.Rollback()
|
|
|
+ logx.Error("syncWx: ", err)
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ err = tx.Commit()
|
|
|
if err != nil {
|
|
|
logx.Error("syncWx: ", err)
|
|
|
continue
|
|
|
}
|
|
|
} else {
|
|
|
logx.Error(fmt.Printf("wxinfo is nil, account: %+v\n", account))
|
|
|
- _, err := svcCtx.DB.Wx.Create().
|
|
|
+ _, err := s.DB.Wx.Create().
|
|
|
SetServerID(0).
|
|
|
SetPort(*account.Deviceid).
|
|
|
SetProcessID(strconv.FormatInt(account.Cid, 10)).
|
|
@@ -84,38 +106,11 @@ func SyncAllWx(svcCtx *svc.ServiceContext) {
|
|
|
SetNotNilNickname(account.Wechatnick).
|
|
|
SetStatus(status).
|
|
|
SetAllowList([]string{}).SetBlockList([]string{}).SetGroupAllowList([]string{}).SetGroupBlockList([]string{}).
|
|
|
- Save(context.TODO())
|
|
|
+ Save(s.Ctx)
|
|
|
if err != nil {
|
|
|
logx.Error("syncWx: ", err)
|
|
|
continue
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- //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)
|
|
|
- // continue
|
|
|
- //}
|
|
|
- //
|
|
|
- //dataChatroom := map[string]interface{}{
|
|
|
- // "MsgType": "TriggerChatroomPushTask",
|
|
|
- // "Content": map[string]interface{}{
|
|
|
- // "WeChatId": *account.Wechatid,
|
|
|
- // "Flag": 1,
|
|
|
- // },
|
|
|
- //}
|
|
|
- //jsonStrChatroom, err := json.Marshal(dataChatroom)
|
|
|
- //err = svcCtx.WechatWs["default"].SendMsg([]byte(jsonStrChatroom))
|
|
|
- //if err != nil {
|
|
|
- // logx.Error("syncWx: ", err)
|
|
|
- // continue
|
|
|
- //}
|
|
|
}
|
|
|
}
|