Files
2026-02-17 13:06:23 +08:00

122 lines
3.4 KiB
Go

package wxrouter
import (
"bytes"
"time"
"xiawan/wx/clientsdk"
"xiawan/wx/clientsdk/baseinfo"
"xiawan/wx/clientsdk/baseutils"
"xiawan/wx/db"
"xiawan/wx/db/table"
"xiawan/wx/protobuf/wechat"
"xiawan/wx/srv/wxcore"
"xiawan/wx/srv/wxface"
"github.com/gogo/protobuf/proto"
"github.com/lunny/log"
)
// WXNewInitRouter 获取二维码响应路由
type WXNewInitRouter struct {
wxcore.WXBaseRouter
}
// Handle 处理conn业务的方法
func (hbr *WXNewInitRouter) Handle(wxResp wxface.IWXResponse) error {
currentWXConn := wxResp.GetWXConncet()
currentWXAccount := currentWXConn.GetWXAccount()
currentUserInfo := currentWXAccount.GetUserInfo()
currentWXCache := currentWXConn.GetWXCache()
// currentSyncMgr := currentWXConn.GetWXSyncMgr()
// 同步响应
var initResp wechat.NewInitResponse
err := clientsdk.ParseResponseData(currentUserInfo, wxResp.GetPackHeader(), &initResp)
if err != nil {
// 请求出问题了,判断是否掉线,并重连
time.Sleep(1 * time.Second)
go currentWXConn.CheckOnLineStatusLogin()
return err
}
// 跟新同步Key
syncKey := initResp.GetCurrentSyncKey().GetBuffer()
syncKeyMgr := currentUserInfo.SyncKeyMgr()
syncKeyMgr.SetMaxKey(initResp.GetMaxSyncKey())
syncKeyMgr.SetMaxKey(initResp.GetCurrentSyncKey())
//保存SyncKey
if len(currentUserInfo.SyncKey) <= 0 || !bytes.Equal(currentUserInfo.SyncKey, syncKey) {
currentUserInfo.SyncKey = syncKey
db.UpdateUserInfo(currentUserInfo)
}
if initResp.GetContinueFlag() <= 0 {
if !currentWXCache.IsInitNewSyncFinished() {
// 禁用历史消息推送,不设置初始化完成标志
currentWXCache.SetInitNewSyncFinished(true)
}
}
// 如果没有同步到数据则返回
cmdList := initResp.GetCmdList()
syncCount := initResp.GetCmdCount()
//log.Info(initResp.GetContinueFlag())
//log.Info(initResp.GetContinueFlag(), syncCount)
//redis 发布结构体
messageResp := new(table.SyncMessageResponse)
// 遍历同步的信息和群
itemList := cmdList
for index := uint32(0); index < syncCount; index++ {
item := itemList[index]
itemID := item.GetCmdId()
// 同步到消息
if itemID == baseinfo.CmdIDAddMsg {
print(currentWXCache.IsInitNewSyncFinished(), 111)
if !currentWXCache.IsInitNewSyncFinished() {
// fmt.Println("跳过初始化历史消息 - IsInitNewSyncFinished=false")
continue
}
// fmt.Println("处理初始化消息 - IsInitNewSyncFinished=true")
addMsg := &wechat.AddMsg{}
err := proto.Unmarshal(item.CmdBuf.Data, addMsg)
if err != nil {
baseutils.PrintLog(err.Error())
continue
}
go dealAddMsgRun(currentWXConn, addMsg, "init")
}
// 同步到联系人
if itemID == baseinfo.CmdIDModContact {
contact := new(wechat.ModContact)
err := proto.Unmarshal(item.GetCmdBuf().GetData(), contact)
if err != nil {
continue
}
log.Info("dealModContact")
// dealModContact(currentWXConn, contact)
}
// 删除联系人
if itemID == baseinfo.CmdIDDelContact {
delContact := &wechat.DelContact{}
err := proto.Unmarshal(item.CmdBuf.Data, delContact)
if err != nil {
continue
}
// go dealDelContact(currentWXConn, delContact.GetUserName().GetStr())
}
messageResp.SetMessage(item.GetCmdBuf().GetData(), int32(itemID))
}
//发布同步信息消息
_ = db.PublishSyncMsgWxMessage(currentWXAccount, *messageResp)
// 如果数量超过10条则继续同步
if !currentWXCache.IsInitNewSyncFinished() {
// currentSyncMgr.SendSyncInitRequest()
}
return nil
}