package srv import ( "strconv" "strings" "sync" "xiawan/wx/srv/websrv" "xiawan/wx/clientsdk" "xiawan/wx/clientsdk/baseinfo" "xiawan/wx/clientsdk/proxynet" "xiawan/wx/protobuf/wechat" ) // WXAccount 代表微信帐号 type WXAccount struct { userInfo *baseinfo.UserInfo // 好友列表 FriendMap map[string]*wechat.ModContact // 公众号列表 ghMap map[string]*wechat.ModContact // 群map groupMap map[string]*wechat.ModContact // 标签列表 labelMap map[uint32]*wechat.LabelPair // 配置文件信息 userProfile *wechat.GetProfileResponse // 登录状态 loginState uint32 // 刚刚扫码登陆要初始化 bInitializing bool // 任务信息 taskInfo *websrv.TaskInfo friendMapLock sync.RWMutex //读写连接的读写锁 ghMapLock sync.RWMutex //读写连接的读写锁 groupMapLock sync.RWMutex //读写连接的读写锁 labelMapLock sync.RWMutex //读写连接的读写锁 } // NewWXAccount 生成一个新的账户 func NewWXAccount(taskInfo *websrv.TaskInfo, proxyInfo *proxynet.WXProxyInfo, ClientVersion uint32, dbUserInfo *baseinfo.UserInfo) *WXAccount { if !(int(ClientVersion) > 0) { ClientVersion = baseinfo.ClientVersion } if ClientVersion == 319293451 { wxAccount := &WXAccount{ userInfo: clientsdk.NewUserInfo_Mac(taskInfo.UUID, proxyInfo, ClientVersion, dbUserInfo), FriendMap: make(map[string]*wechat.ModContact), ghMap: make(map[string]*wechat.ModContact), groupMap: make(map[string]*wechat.ModContact), labelMap: make(map[uint32]*wechat.LabelPair), userProfile: nil, loginState: baseinfo.MMLoginStateNoLogin, taskInfo: taskInfo, } return wxAccount } else { wxAccount := &WXAccount{ userInfo: clientsdk.NewUserInfo(taskInfo.UUID, proxyInfo, ClientVersion, dbUserInfo), FriendMap: make(map[string]*wechat.ModContact), ghMap: make(map[string]*wechat.ModContact), groupMap: make(map[string]*wechat.ModContact), labelMap: make(map[uint32]*wechat.LabelPair), userProfile: nil, loginState: baseinfo.MMLoginStateNoLogin, taskInfo: taskInfo, } return wxAccount } } // GetUserInfo 获取UserInfo func (wxAccount *WXAccount) GetUserInfo() *baseinfo.UserInfo { return wxAccount.userInfo } // SetUserInfo 设置用户信息 func (wxAccount *WXAccount) SetUserInfo(info *baseinfo.UserInfo) { // 默认重启状态为 false info.IsServerRestart = false wxAccount.userInfo = info } // GetTaskInfo 获取任务信息 func (wxAccount *WXAccount) GetTaskInfo() *websrv.TaskInfo { return wxAccount.taskInfo } // SetUserProfile 设置用户配置信息 func (wxAccount *WXAccount) SetUserProfile(userProfile *wechat.GetProfileResponse) { wxAccount.userProfile = userProfile } // GetUserProfile 获取帐号信息 func (wxAccount *WXAccount) GetUserProfile() *wechat.GetProfileResponse { return wxAccount.userProfile } // GetLoginState 获取登录状态 func (wxAccount *WXAccount) GetLoginState() uint32 { //return wxAccount.loginState return wxAccount.GetUserInfo().GetLoginState() } // SetLoginState 设置登录状态 func (wxAccount *WXAccount) SetLoginState(loginState uint32) { wxAccount.loginState = loginState wxAccount.userInfo.SetLoginState(loginState) } // AddWXFriendContact 增加微信好友ID func (wxAccount *WXAccount) AddWXFriendContact(modContact *wechat.ModContact) { wxAccount.friendMapLock.Lock() defer wxAccount.friendMapLock.Unlock() // 不存在-添加, 存在-更新 wxAccount.FriendMap[modContact.GetUserName().GetStr()] = modContact } // RemoveWXFriendID 删除信好友ID func (wxAccount *WXAccount) RemoveWXFriendID(friendWXID string) bool { wxAccount.friendMapLock.Lock() defer wxAccount.friendMapLock.Unlock() // 如果不存在则退出 _, ok := wxAccount.FriendMap[friendWXID] if !ok { return false } delete(wxAccount.FriendMap, friendWXID) return true } // GetWXFriendList 获取微信好友ID列表 func (wxAccount *WXAccount) GetWXFriendList() []*wechat.ModContact { wxAccount.friendMapLock.Lock() defer wxAccount.friendMapLock.Unlock() retList := make([]*wechat.ModContact, 0) for _, modContact := range wxAccount.FriendMap { retList = append(retList, modContact) } return retList } // GetWXFriendList 根据 id 获取好友 func (wxAccount *WXAccount) GetWXFriendByWxid(wxid string) wechat.ModContact { wxAccount.friendMapLock.Lock() defer wxAccount.friendMapLock.Unlock() for _, modContact := range wxAccount.FriendMap { if modContact.GetUserName().GetStr() == wxid { return *modContact } } return wechat.ModContact{} } // GetWXFriendList 获取微信好友ID列表 func (wxAccount *WXAccount) getWXFriendListByLabelID(labelID uint32) []string { wxAccount.friendMapLock.Lock() defer wxAccount.friendMapLock.Unlock() retList := make([]string, 0) // 遍历好友 获取对应标签的好友列表 for userName, modContact := range wxAccount.FriendMap { if wxAccount.ContainsLabel(modContact.GetLabelIdlist(), labelID) { retList = append(retList, userName) } } return retList } // AddWXGhContact 增加微信公众号ID func (wxAccount *WXAccount) AddWXGhContact(modContact *wechat.ModContact) { wxAccount.ghMapLock.Lock() defer wxAccount.ghMapLock.Unlock() // 不存在-添加, 存在-更新 wxAccount.ghMap[modContact.GetUserName().GetStr()] = modContact } // RemoveWXGhID 删除微信公众号ID func (wxAccount *WXAccount) RemoveWXGhID(ghWXID string) bool { wxAccount.ghMapLock.Lock() defer wxAccount.ghMapLock.Unlock() // 如果不存在则退出 _, ok := wxAccount.ghMap[ghWXID] if !ok { return false } delete(wxAccount.ghMap, ghWXID) return true } // GetWXGhList 获取微信公众号ID列表 func (wxAccount *WXAccount) GetWXGhList() []*wechat.ModContact { wxAccount.ghMapLock.Lock() defer wxAccount.ghMapLock.Unlock() retList := make([]*wechat.ModContact, 0) for _, modContact := range wxAccount.ghMap { retList = append(retList, modContact) } return retList } // AddWXGroup 增加群聊ID func (wxAccount *WXAccount) AddWXGroup(modContact *wechat.ModContact) { wxAccount.groupMapLock.Lock() defer wxAccount.groupMapLock.Unlock() // 不存在-添加,存在-更新 userName := modContact.GetUserName().GetStr() wxAccount.groupMap[userName] = modContact } // RemoveWXGroup 删除群聊ID func (wxAccount *WXAccount) RemoveWXGroup(groupWxID string) bool { wxAccount.groupMapLock.Lock() defer wxAccount.groupMapLock.Unlock() // 如果不存在则退出 _, ok := wxAccount.groupMap[groupWxID] if !ok { return false } delete(wxAccount.groupMap, groupWxID) return true } // GetWXGroupList 获取微信群聊ID列表 func (wxAccount *WXAccount) GetWXGroupList() []*wechat.ModContact { wxAccount.groupMapLock.Lock() defer wxAccount.groupMapLock.Unlock() retList := make([]*wechat.ModContact, 0) for _, modContact := range wxAccount.groupMap { retList = append(retList, modContact) } return retList } // SetLabelPairList 设置标签列表 func (wxAccount *WXAccount) SetLabelPairList(labelList []*wechat.LabelPair) { wxAccount.labelMapLock.Lock() defer wxAccount.labelMapLock.Unlock() labelCount := len(labelList) wxAccount.labelMap = make(map[uint32]*wechat.LabelPair) for index := 0; index < labelCount; index++ { tmpLabelPair := labelList[index] wxAccount.labelMap[tmpLabelPair.GetLabelId()] = tmpLabelPair } } // 获取对应名称的LabelPair func (wxAccount *WXAccount) getLabelPairByName(labelName string) *wechat.LabelPair { for _, labelPair := range wxAccount.labelMap { if labelPair.GetLabelName() == labelName { return labelPair } } return nil } // GetUserListByLabel 根据Label名称获取好友列表 func (wxAccount *WXAccount) GetUserListByLabel(labelName string) []string { wxAccount.labelMapLock.Lock() defer wxAccount.labelMapLock.Unlock() // 先获取LabelPair tmpLabelPair := wxAccount.getLabelPairByName(labelName) if tmpLabelPair == nil { return []string{} } return wxAccount.getWXFriendListByLabelID(tmpLabelPair.GetLabelId()) } // ContainsLabel 判断好友是否属于某个标签 func (wxAccount *WXAccount) ContainsLabel(strLabelList string, tmpLabel uint32) bool { if len(strLabelList) <= 0 { return false } // 判断是否属于标签 labelList := strings.Split(strLabelList, ",") tmpStrLabel := strconv.Itoa(int(tmpLabel)) tmpCount := len(labelList) for index := 0; index < tmpCount; index++ { if tmpStrLabel == labelList[index] { return true } } return false } // GetBindQueryNewReq GetBindQueryNewReq func (wxAccount *WXAccount) GetBindQueryNewReq() (string, error) { tmpBindQueryNewReq := &baseinfo.BindQueryNewReq{} tmpBindQueryNewReq.BalanceVersion = wxAccount.userInfo.BalanceVersion tmpBindQueryNewReq.BindQueryScen = 1 tmpBindQueryNewReq.BindTypeCond = "all_type" tmpBindQueryNewReq.ISRoot = 0 tmpBindQueryNewReq.City = wxAccount.userProfile.GetUserInfo().GetCity() tmpBindQueryNewReq.Province = wxAccount.userProfile.GetUserInfo().GetProvince() tmpString := "balance_version=" + strconv.Itoa(int(tmpBindQueryNewReq.BalanceVersion)) tmpString = tmpString + "&bind_query_scene=" + strconv.Itoa(int(tmpBindQueryNewReq.BindQueryScen)) tmpString = tmpString + "&bind_type_cond=" + tmpBindQueryNewReq.BindTypeCond tmpString = tmpString + "&city=" + tmpBindQueryNewReq.City tmpString = tmpString + "&is_device_open_touch=" + strconv.Itoa(int(tmpBindQueryNewReq.ISDeviceOpenTouch)) tmpString = tmpString + "&is_root=" + strconv.Itoa(int(tmpBindQueryNewReq.ISRoot)) tmpString = tmpString + "&province=" + tmpBindQueryNewReq.Province wcPaySign, err := clientsdk.TenPaySignDes3(tmpString, "%^&*Tenpay!@#$") if err != nil { return "", err } tmpString = tmpString + "&WCPaySign=" + wcPaySign return tmpString, nil }