first commit
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
package wxtask
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
"xiawan/wx/clientsdk"
|
||||
"xiawan/wx/clientsdk/baseinfo"
|
||||
"xiawan/wx/protobuf/wechat"
|
||||
"xiawan/wx/srv/srvconfig"
|
||||
"xiawan/wx/srv/wxface"
|
||||
)
|
||||
|
||||
// WXGroupTask 群任务
|
||||
type WXGroupTask struct {
|
||||
wxConn wxface.IWXConnect
|
||||
groupForQrcodeList []*wechat.ModContact
|
||||
groupForAddressBookList []*wechat.ModContact
|
||||
waitTimes uint32 // 毫秒
|
||||
inQrcodeTask bool // 下载群二维码
|
||||
inAddressBookTask bool // 保存群到通讯录任务状态
|
||||
}
|
||||
|
||||
// NewWXGroupTask 新建一个群任务
|
||||
func NewWXGroupTask(wxConn wxface.IWXConnect) *WXGroupTask {
|
||||
return &WXGroupTask{
|
||||
wxConn: wxConn,
|
||||
groupForQrcodeList: make([]*wechat.ModContact, 0),
|
||||
groupForAddressBookList: make([]*wechat.ModContact, 0),
|
||||
inQrcodeTask: false,
|
||||
inAddressBookTask: false,
|
||||
waitTimes: srvconfig.TaskExecWaitTimes,
|
||||
}
|
||||
}
|
||||
|
||||
// 获取下一个待下载的获取二维码的群
|
||||
func (gpt *WXGroupTask) getNextGroupForQrcodeContact() *wechat.ModContact {
|
||||
if len(gpt.groupForQrcodeList) <= 0 {
|
||||
return nil
|
||||
}
|
||||
retContact := gpt.groupForQrcodeList[0]
|
||||
gpt.groupForQrcodeList = gpt.groupForQrcodeList[1:]
|
||||
return retContact
|
||||
}
|
||||
|
||||
// 获取下一个待保存到通讯录到群
|
||||
func (gpt *WXGroupTask) getNextGroupListForAddressBookContact(count uint32) []*wechat.ModContact {
|
||||
size := uint32(len(gpt.groupForAddressBookList))
|
||||
if size <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
retList := make([]*wechat.ModContact, 0)
|
||||
if size > count {
|
||||
retList = append(retList, gpt.groupForAddressBookList[:count]...)
|
||||
gpt.groupForAddressBookList = gpt.groupForAddressBookList[count:]
|
||||
} else {
|
||||
retList = append(retList, gpt.groupForAddressBookList[0:]...)
|
||||
gpt.groupForAddressBookList = []*wechat.ModContact{}
|
||||
}
|
||||
return retList
|
||||
}
|
||||
|
||||
// GetLessCount 获取剩余群聊数量
|
||||
func (gpt *WXGroupTask) GetLessCount() uint32 {
|
||||
return uint32(len(gpt.groupForAddressBookList))
|
||||
}
|
||||
|
||||
// StartDownQrcode 开启下载群二维码任务
|
||||
// lessMembers: 最少成员数量
|
||||
func (gpt *WXGroupTask) StartDownQrcode(lessMembers uint32) {
|
||||
currentWXAccount := gpt.wxConn.GetWXAccount()
|
||||
currentWXReqInvoker := gpt.wxConn.GetWXReqInvoker()
|
||||
// 如果正在执行下载任务,则返回
|
||||
if gpt.inQrcodeTask {
|
||||
return
|
||||
}
|
||||
// 如果上一次的还没
|
||||
if len(gpt.groupForQrcodeList) <= 0 {
|
||||
gpt.groupForQrcodeList = currentWXAccount.GetWXGroupList()
|
||||
}
|
||||
// 先构建当前目录
|
||||
gpt.inQrcodeTask = true
|
||||
go func() {
|
||||
for {
|
||||
// 先判断是否还处于链接当中
|
||||
if !gpt.wxConn.IsConnected() {
|
||||
gpt.inQrcodeTask = false
|
||||
return
|
||||
}
|
||||
|
||||
// 获取下一个群聊
|
||||
tmpModContact := gpt.getNextGroupForQrcodeContact()
|
||||
if tmpModContact == nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 群成员数量要大于20, 超过200人的群聊必须要邀请才能入群
|
||||
memberCount := tmpModContact.NewChatroomData.GetMemberCount()
|
||||
if memberCount < lessMembers || memberCount >= 200 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 进群验证
|
||||
if tmpModContact.GetChatroomAccessType() != 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 获取群二维码请求
|
||||
groupUserName := tmpModContact.GetUserName().GetStr()
|
||||
err := currentWXReqInvoker.SendGetQRCodeRequest(groupUserName)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
// 暂停 2000 毫秒
|
||||
time.Sleep(2000 * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// StartSaveToAddressBook 开启保存到通讯录
|
||||
// bSave: true:保存, false:取消保存
|
||||
func (gpt *WXGroupTask) StartSaveToAddressBook(bSave bool) bool {
|
||||
tmpOptionName := "保存"
|
||||
if !bSave {
|
||||
tmpOptionName = "取消保存"
|
||||
}
|
||||
currentWXAccount := gpt.wxConn.GetWXAccount()
|
||||
currentWXReqInvoker := gpt.wxConn.GetWXReqInvoker()
|
||||
currentWXFileHelperMgr := gpt.wxConn.GetWXFileHelperMgr()
|
||||
// 如果正在执行下载任务,则返回
|
||||
if gpt.inAddressBookTask {
|
||||
tipText := "正在" + tmpOptionName + "中请稍后, 剩余(" + strconv.Itoa(len(gpt.groupForAddressBookList)) + ")个群"
|
||||
currentWXFileHelperMgr.AddNewTipMsg(tipText)
|
||||
return false
|
||||
}
|
||||
|
||||
// 如果上一次的还没
|
||||
if len(gpt.groupForAddressBookList) <= 0 {
|
||||
gpt.groupForAddressBookList = currentWXAccount.GetWXGroupList()
|
||||
}
|
||||
gpt.inAddressBookTask = true
|
||||
go func() {
|
||||
for {
|
||||
// 先判断是否还处于链接当中
|
||||
if !gpt.wxConn.IsConnected() {
|
||||
gpt.inAddressBookTask = false
|
||||
return
|
||||
}
|
||||
|
||||
// 获取接下来的10个群聊
|
||||
tmpModContactList := gpt.getNextGroupListForAddressBookContact(10)
|
||||
count := len(tmpModContactList)
|
||||
if count <= 0 {
|
||||
gpt.inAddressBookTask = false
|
||||
tipText := "执行 " + tmpOptionName + "群聊任务 完成"
|
||||
currentWXFileHelperMgr.AddNewTipMsg(tipText)
|
||||
return
|
||||
}
|
||||
|
||||
// 创建Item列表
|
||||
items := make([]*baseinfo.ModifyItem, 0)
|
||||
for index := 0; index < count; index++ {
|
||||
tmpModContact := tmpModContactList[index]
|
||||
// 如果状态一致则不用操作
|
||||
bitVal := tmpModContact.GetBitVal()
|
||||
hasSaved := false
|
||||
if bitVal&baseinfo.MMBitValGroupSaveInAddressBook == 1 {
|
||||
hasSaved = true
|
||||
}
|
||||
if (bSave && hasSaved) || (!bSave && !hasSaved) {
|
||||
continue
|
||||
}
|
||||
tmpItem := clientsdk.CreateSaveGroupToAddressBookField(tmpModContact, bSave)
|
||||
items = append(items, tmpItem)
|
||||
}
|
||||
if len(items) <= 0 {
|
||||
continue
|
||||
}
|
||||
err := currentWXReqInvoker.SendOplogRequest(items)
|
||||
if err != nil {
|
||||
gpt.inAddressBookTask = false
|
||||
return
|
||||
}
|
||||
// 暂停 waitTimes 毫秒
|
||||
time.Sleep(time.Duration(gpt.waitTimes) * time.Millisecond)
|
||||
}
|
||||
}()
|
||||
|
||||
tmpTip := "开始" + tmpOptionName + "群聊任务"
|
||||
currentWXFileHelperMgr.AddNewTipMsg(tmpTip)
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user