فهرست منبع

个微手动刷新也走 ws

boweniac 2 هفته پیش
والد
کامیت
30e421d3ed

+ 92 - 97
crontask/sync_wx.go

@@ -1,104 +1,99 @@
 package crontask
 
 import (
-	"encoding/json"
-	"github.com/imroc/req/v3"
-	"strconv"
-	"time"
-	"wechat-api/ent"
-	"wechat-api/ent/wx"
-	"wechat-api/internal/types"
+	"wechat-api/internal/service/wx"
 )
 
 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=0")
-	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
-		}
-	}
+	wx.SyncAllWx(l.svcCtx)
+	//// 获取微信列表
+	//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 {
+	//	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
+	//	}
+	//}
 }

+ 2 - 85
internal/logic/WorkPhone/wx_refresh_logic.go

@@ -2,15 +2,8 @@ package WorkPhone
 
 import (
 	"context"
-	"fmt"
-	"github.com/imroc/req/v3"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
-	"strconv"
-	"time"
-	"wechat-api/ent"
-	"wechat-api/ent/wx"
-	"wechat-api/internal/utils/dberrorhandler"
-
+	"wechat-api/internal/service/wx"
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 
@@ -31,83 +24,7 @@ func NewWxRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxRefre
 }
 
 func (l *WxRefreshLogic) WxRefresh() (resp *types.BaseMsgResp, err error) {
-	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 {
-		return
-	}
-	if !res.IsSuccessState() {
-		err = fmt.Errorf("GetWeChats failed with status code %d", 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) {
-			return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
-		}
-		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 {
-				return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
-			}
-		} 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)
-			l.Error("wxinfo save", err)
-			if err != nil {
-				return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
-			}
-		}
-	}
-
-	l.svcCtx.Rds.Del(l.ctx, "crontask_wx_server_info")
+	wx.SyncAllWx(l.svcCtx)
 
 	return &types.BaseMsgResp{Msg: errormsg.Success}, nil
-
-	return
 }

+ 114 - 0
internal/logic/WorkPhone/wx_refresh_logic_backup.go

@@ -0,0 +1,114 @@
+package WorkPhone
+
+//
+//import (
+//	"context"
+//	"fmt"
+//	"github.com/imroc/req/v3"
+//	"github.com/suyuan32/simple-admin-common/msg/errormsg"
+//	"strconv"
+//	"time"
+//	"wechat-api/ent"
+//	"wechat-api/ent/wx"
+//	"wechat-api/internal/utils/dberrorhandler"
+//
+//	"wechat-api/internal/svc"
+//	"wechat-api/internal/types"
+//
+//	"github.com/zeromicro/go-zero/core/logx"
+//)
+//
+//type WxRefreshLogic struct {
+//	logx.Logger
+//	ctx    context.Context
+//	svcCtx *svc.ServiceContext
+//}
+//
+//func NewWxRefreshLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WxRefreshLogic {
+//	return &WxRefreshLogic{
+//		Logger: logx.WithContext(ctx),
+//		ctx:    ctx,
+//		svcCtx: svcCtx}
+//}
+//
+//func (l *WxRefreshLogic) WxRefresh() (resp *types.BaseMsgResp, err error) {
+//	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 {
+//		return
+//	}
+//	if !res.IsSuccessState() {
+//		err = fmt.Errorf("GetWeChats failed with status code %d", 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) {
+//			return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
+//		}
+//		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 {
+//				return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
+//			}
+//		} 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)
+//			l.Error("wxinfo save", err)
+//			if err != nil {
+//				return nil, dberrorhandler.DefaultEntError(l.Logger, err, account)
+//			}
+//		}
+//	}
+//
+//	l.svcCtx.Rds.Del(l.ctx, "crontask_wx_server_info")
+//
+//	return &types.BaseMsgResp{Msg: errormsg.Success}, nil
+//
+//	return
+//}

+ 56 - 44
internal/logic/Wxhook/get_friends_and_groups_logic.go

@@ -2,7 +2,7 @@ package Wxhook
 
 import (
 	"context"
-	"fmt"
+	"encoding/json"
 	"github.com/spf13/cast"
 	"github.com/suyuan32/simple-admin-common/enum/errorcode"
 	"github.com/suyuan32/simple-admin-common/msg/errormsg"
@@ -12,8 +12,6 @@ import (
 	"wechat-api/internal/svc"
 	"wechat-api/internal/types"
 
-	reqv3 "github.com/imroc/req/v3"
-
 	"github.com/zeromicro/go-zero/core/logx"
 )
 
@@ -205,58 +203,72 @@ func (l *GetFriendsAndGroupsLogic) GetFriendsAndGroups(req *types.IDReq) (resp *
 	} else {
 		// 工作手机版本
 		var hookClient *hook.Hook
-		var result GetWechatFriendListResp
-		var wsUrl string
+		//var result GetWechatFriendListResp
+		//var wsUrl string
 
 		if wxInfo.Ctype == uint64(3) {
 			hookClient = hook.NewWecomHook("", "", "")
 			_ = hookClient.TriggerCustomerPushTask(wxInfo.Wxid)
 			_ = hookClient.TriggerConversationPushTask(wxInfo.Wxid)
 		} else {
-			hookClient = hook.NewHook("", "", "")
-			wsUrl = "http://chat.gkscrm.com:13086/pc/GetWechatFriendList?cid=" + wxInfo.ProcessID + "&wechatid=" + wxInfo.Wxid
 
-			res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post(wsUrl)
-			if err != nil {
-				return nil, err
+			data := map[string]interface{}{
+				"MsgType": "TriggerFriendPushTask",
+				"Content": map[string]interface{}{
+					"WeChatId": wxInfo.Wxid,
+				},
 			}
-			if !res.IsSuccessState() {
-				err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", res.StatusCode)
-				l.Error("GetWechatFriendList 请求失败", err)
+			jsonStr, err := json.Marshal(data)
+			err = l.svcCtx.WechatWs["default"].SendMsg([]byte(jsonStr))
+			if err != nil {
+				logx.Error("syncWx: ", err)
 				return nil, err
 			}
 
-			_ = hookClient.TriggerChatroomPush(wxInfo.Wxid)
-
-			for _, friend := range result.Data {
-				friendType := 1
-				if friend.Type == 1 {
-					friendType = 2
-					_ = hookClient.RequestChatRoomInfo(friend.Friendid, wxInfo.Wxid)
-				} else {
-					friendType = 1
-				}
-
-				_, err = l.svcCtx.DB.Contact.Create().
-					SetWxWxid(wxInfo.Wxid).
-					SetType(friendType).
-					SetWxid(friend.Friendid).
-					SetAccount(friend.FriendWechatno).
-					SetNickname(friend.Nickname).
-					SetMarkname(friend.Memo).
-					SetHeadimg(friend.Avatar).
-					SetSex(cast.ToInt(friend.Gender)).
-					SetOrganizationID(organizationId).
-					OnConflict().
-					UpdateNewValues().
-					SetOrganizationID(organizationId).
-					ID(l.ctx)
-
-				if err != nil {
-					l.Error("创建联系人失败", err)
-					continue
-				}
-			}
+			//hookClient = hook.NewHook("", "", "")
+			//wsUrl = "http://chat.gkscrm.com:13086/pc/GetWechatFriendList?cid=" + wxInfo.ProcessID + "&wechatid=" + wxInfo.Wxid
+			//
+			//res, err := reqv3.C().DevMode().R().SetSuccessResult(&result).Post(wsUrl)
+			//if err != nil {
+			//	return nil, err
+			//}
+			//if !res.IsSuccessState() {
+			//	err = fmt.Errorf("GetFriendAndChatRoomList failed with status code %d", res.StatusCode)
+			//	l.Error("GetWechatFriendList 请求失败", err)
+			//	return nil, err
+			//}
+			//
+			//_ = hookClient.TriggerChatroomPush(wxInfo.Wxid)
+			//
+			//for _, friend := range result.Data {
+			//	friendType := 1
+			//	if friend.Type == 1 {
+			//		friendType = 2
+			//		_ = hookClient.RequestChatRoomInfo(friend.Friendid, wxInfo.Wxid)
+			//	} else {
+			//		friendType = 1
+			//	}
+			//
+			//	_, err = l.svcCtx.DB.Contact.Create().
+			//		SetWxWxid(wxInfo.Wxid).
+			//		SetType(friendType).
+			//		SetWxid(friend.Friendid).
+			//		SetAccount(friend.FriendWechatno).
+			//		SetNickname(friend.Nickname).
+			//		SetMarkname(friend.Memo).
+			//		SetHeadimg(friend.Avatar).
+			//		SetSex(cast.ToInt(friend.Gender)).
+			//		SetOrganizationID(organizationId).
+			//		OnConflict().
+			//		UpdateNewValues().
+			//		SetOrganizationID(organizationId).
+			//		ID(l.ctx)
+			//
+			//	if err != nil {
+			//		l.Error("创建联系人失败", err)
+			//		continue
+			//	}
+			//}
 		}
 	}
 

+ 107 - 0
internal/service/wx/sync_wx.go

@@ -0,0 +1,107 @@
+package wx
+
+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
+		}
+	}
+}