Browse Source

增加群组同步

boweniac 1 week ago
parent
commit
adf986d170

+ 67 - 0
internal/service/MessageHandlers/chatroom_push_notice.go

@@ -0,0 +1,67 @@
+package MessageHandlers
+
+import (
+	"context"
+	"encoding/json"
+	"github.com/zeromicro/go-zero/core/logx"
+	"wechat-api/ent/wx"
+	"wechat-api/internal/pkg/wechat_ws"
+	"wechat-api/internal/svc"
+	"wechat-api/workphone"
+)
+
+type ChatroomPushNoticeHandler struct {
+	svcCtx *svc.ServiceContext
+}
+
+func NewChatroomPushNoticeHandler(svcCtx *svc.ServiceContext) *ChatroomPushNoticeHandler {
+	return &ChatroomPushNoticeHandler{
+		svcCtx: svcCtx,
+	}
+}
+
+func (f *ChatroomPushNoticeHandler) Handler(msg *wechat_ws.MsgJsonObject) error {
+	if msg.MsgType == "ChatroomPushNotice" {
+		message := workphone.ChatRoomPushNoticeMessage{}
+		err := json.Unmarshal([]byte(msg.Message), &message)
+		if err != nil {
+			return err
+		}
+		// 拿到租户 id
+		wx_info, err := f.svcCtx.DB.Wx.Query().
+			Where(
+				wx.WxidEQ(message.WeChatId), // Additional filter by organizationId
+			).
+			Only(context.TODO())
+		//hookClient := hook.NewHook("", "", "")
+		for _, friend := range message.ChatRooms {
+			friendType := 1
+			if friend.Type == 1 {
+				friendType = 2
+				//_ = hookClient.RequestChatRoomInfo(friend.FriendId, message.WeChatId)
+			} else {
+				friendType = 1
+			}
+			_, err = f.svcCtx.DB.Contact.Create().
+				SetWxWxid(message.WeChatId).
+				SetType(friendType).
+				SetWxid(friend.UserName).
+				SetAccount(friend.UserName).
+				SetNickname(friend.Notice).
+				SetMarkname(friend.Notice).
+				SetHeadimg(friend.Avatar).
+				//SetSex(cast.ToInt(friend.Gender)).
+				SetOrganizationID(wx_info.OrganizationID).
+				OnConflict().
+				UpdateNewValues().
+				SetOrganizationID(wx_info.OrganizationID).
+				ID(context.TODO())
+
+			if err != nil {
+				logx.Error("Contact.Create: ", wx_info.OrganizationID)
+				return err
+			}
+		}
+	}
+	return nil
+}

+ 9 - 0
internal/service/wechat/sync_wx.go

@@ -103,5 +103,14 @@ func SyncAllWx(svcCtx *svc.ServiceContext) {
 			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))
 	}
 }

+ 1 - 0
wechat.go

@@ -63,6 +63,7 @@ func main() {
 			//	ic = channel.NewWechatChannel(ws, ctx)
 			//}
 			//ws.RegisterMessageHandler(ic.OnMessage)
+			ws.RegisterMessageHandler(MessageHandlers.NewChatroomPushNoticeHandler(ctx).Handler)
 			ws.RegisterMessageHandler(MessageHandlers.NewFriendPushNoticeHandler(ctx).Handler)
 		}
 		logx.Info("注册个微处理通道~")