This commit is contained in:
eatMoreApple 2021-04-28 23:30:52 +08:00
parent b967eda0d0
commit c20044dbb1
4 changed files with 381 additions and 359 deletions

View File

@ -1,4 +1,6 @@
# openwechat # openwechat
[![Go Doc](https://pkg.go.dev/badge/github.com/eatMoreApple/openwechat)](https://godoc.org/github.com/eatMoreApple/openwechat)
> golang版个人微信号API, 类似开发公众号一样,开发个人微信号 > golang版个人微信号API, 类似开发公众号一样,开发个人微信号

440
bot.go
View File

@ -1,300 +1,316 @@
package openwechat package openwechat
import ( import (
"errors" "errors"
"log" "log"
"net/url" "net/url"
) )
type Bot struct { type Bot struct {
ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像 ScanCallBack func(body []byte) // 扫码回调,可获取扫码用户的头像
LoginCallBack func(body []byte) // 登陆回调 LoginCallBack func(body []byte) // 登陆回调
UUIDCallback func(uuid string) // 获取UUID的回调函数 UUIDCallback func(uuid string) // 获取UUID的回调函数
MessageHandler func(msg *Message) // 获取消息成功的handle MessageHandler func(msg *Message) // 获取消息成功的handle
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
isHot bool isHot bool
err error err error
exit chan bool exit chan bool
Caller *Caller Caller *Caller
self *Self self *Self
storage *Storage storage *Storage
hotReloadStorage HotReloadStorage hotReloadStorage HotReloadStorage
} }
// 判断当前用户是否正常在线 // 判断当前用户是否正常在线
func (b *Bot) Alive() bool { func (b *Bot) Alive() bool {
if b.self == nil { if b.self == nil {
return false return false
} }
select { select {
case <-b.exit: case <-b.exit:
return false return false
default: default:
return true return true
} }
} }
// 获取当前的用户 // 获取当前的用户
// self, err := bot.GetCurrentUser()
// if err != nil {
// return
// }
// fmt.Println(self.NickName)
func (b *Bot) GetCurrentUser() (*Self, error) { func (b *Bot) GetCurrentUser() (*Self, error) {
if b.self == nil { if b.self == nil {
return nil, errors.New("user not login") return nil, errors.New("user not login")
} }
return b.self, nil return b.self, nil
} }
// 热登录,可实现重复登录,
// retry设置为true可在热登录失效后进行普通登录行为
// storage := NewJsonFileHotReloadStorage("storage.json")
// err := bot.HotLogin(storage, true)
// fmt.Println(err)
func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error { func (b *Bot) HotLogin(storage HotReloadStorage, retry ...bool) error {
b.isHot = true b.isHot = true
b.hotReloadStorage = storage b.hotReloadStorage = storage
var err error var err error
// 如果load出错了,就执行正常登陆逻辑 // 如果load出错了,就执行正常登陆逻辑
// 第一次没有数据load都会出错的 // 第一次没有数据load都会出错的
if err = storage.Load(); err != nil { if err = storage.Load(); err != nil {
return b.Login() return b.Login()
} }
if err = b.hotLoginInit(); err != nil { if err = b.hotLoginInit(); err != nil {
return err return err
} }
// 如果webInit出错,则说明可能身份信息已经失效 // 如果webInit出错,则说明可能身份信息已经失效
// 如果retry为True的话,则进行正常登陆 // 如果retry为True的话,则进行正常登陆
if err = b.webInit(); err != nil { if err = b.webInit(); err != nil {
if len(retry) > 0 { if len(retry) > 0 {
if retry[0] { if retry[0] {
return b.Login() return b.Login()
} }
} }
} }
return err return err
} }
// 热登陆初始化 // 热登陆初始化
func (b *Bot) hotLoginInit() error { func (b *Bot) hotLoginInit() error {
cookies := b.hotReloadStorage.GetCookie() cookies := b.hotReloadStorage.GetCookie()
for u, ck := range cookies { for u, ck := range cookies {
path, err := url.Parse(u) path, err := url.Parse(u)
if err != nil { if err != nil {
return err return err
} }
b.Caller.Client.Jar.SetCookies(path, ck) b.Caller.Client.Jar.SetCookies(path, ck)
} }
b.storage.LoginInfo = b.hotReloadStorage.GetLoginInfo() b.storage.LoginInfo = b.hotReloadStorage.GetLoginInfo()
b.storage.Request = b.hotReloadStorage.GetBaseRequest() b.storage.Request = b.hotReloadStorage.GetBaseRequest()
return nil return nil
} }
// 用户登录 // 用户登录
// 该方法会一直阻塞,直到用户扫码登录,或者二维码过期 // 该方法会一直阻塞,直到用户扫码登录,或者二维码过期
func (b *Bot) Login() error { func (b *Bot) Login() error {
uuid, err := b.Caller.GetLoginUUID() uuid, err := b.Caller.GetLoginUUID()
if err != nil { if err != nil {
return err return err
} }
if b.UUIDCallback != nil { if b.UUIDCallback != nil {
b.UUIDCallback(uuid) b.UUIDCallback(uuid)
} }
for { for {
resp, err := b.Caller.CheckLogin(uuid) resp, err := b.Caller.CheckLogin(uuid)
if err != nil { if err != nil {
return err return err
} }
switch resp.Code { switch resp.Code {
case statusSuccess: case statusSuccess:
return b.handleLogin(resp.Raw) return b.handleLogin(resp.Raw)
case statusScanned: case statusScanned:
if b.ScanCallBack != nil { if b.ScanCallBack != nil {
b.ScanCallBack(resp.Raw) b.ScanCallBack(resp.Raw)
} }
case statusTimeout: case statusTimeout:
return errors.New("login time out") return errors.New("login time out")
case statusWait: case statusWait:
continue continue
} }
} }
} }
// 用户退出 // 用户退出
func (b *Bot) Logout() error { func (b *Bot) Logout() error {
if b.Alive() { if b.Alive() {
info := b.storage.LoginInfo info := b.storage.LoginInfo
if err := b.Caller.Logout(info); err != nil { if err := b.Caller.Logout(info); err != nil {
return err return err
} }
b.stopAsyncCALL(errors.New("logout")) b.stopAsyncCALL(errors.New("logout"))
return nil return nil
} }
return errors.New("user not login") return errors.New("user not login")
} }
// 登录逻辑 // 登录逻辑
func (b *Bot) handleLogin(data []byte) error { func (b *Bot) handleLogin(data []byte) error {
// 判断是否有登录回调,如果有执行它 // 判断是否有登录回调,如果有执行它
if b.LoginCallBack != nil { if b.LoginCallBack != nil {
b.LoginCallBack(data) b.LoginCallBack(data)
} }
// 获取登录的一些基本的信息 // 获取登录的一些基本的信息
info, err := b.Caller.GetLoginInfo(data) info, err := b.Caller.GetLoginInfo(data)
if err != nil { if err != nil {
return err return err
} }
// 将LoginInfo存到storage里面 // 将LoginInfo存到storage里面
b.storage.LoginInfo = info b.storage.LoginInfo = info
// 构建BaseRequest // 构建BaseRequest
request := &BaseRequest{ request := &BaseRequest{
Uin: info.WxUin, Uin: info.WxUin,
Sid: info.WxSid, Sid: info.WxSid,
Skey: info.SKey, Skey: info.SKey,
DeviceID: GetRandomDeviceId(), DeviceID: GetRandomDeviceId(),
} }
// 将BaseRequest存到storage里面方便后续调用 // 将BaseRequest存到storage里面方便后续调用
b.storage.Request = request b.storage.Request = request
// 如果是热登陆,则将当前的重要信息写入hotReloadStorage // 如果是热登陆,则将当前的重要信息写入hotReloadStorage
if b.isHot { if b.isHot {
cookies := b.Caller.Client.GetCookieMap() cookies := b.Caller.Client.GetCookieMap()
if err := b.hotReloadStorage.Dump(cookies, request, info); err != nil { if err := b.hotReloadStorage.Dump(cookies, request, info); err != nil {
return err return err
} }
} }
return b.webInit() return b.webInit()
} }
func (b *Bot) webInit() error { func (b *Bot) webInit() error {
req := b.storage.Request req := b.storage.Request
info := b.storage.LoginInfo info := b.storage.LoginInfo
// 获取初始化的用户信息和一些必要的参数 // 获取初始化的用户信息和一些必要的参数
resp, err := b.Caller.WebInit(req) resp, err := b.Caller.WebInit(req)
if err != nil { if err != nil {
return err return err
} }
// 设置当前的用户 // 设置当前的用户
b.self = &Self{Bot: b, User: &resp.User} b.self = &Self{Bot: b, User: &resp.User}
b.self.Self = b.self b.self.Self = b.self
b.storage.Response = resp b.storage.Response = resp
// 通知手机客户端已经登录 // 通知手机客户端已经登录
if err = b.Caller.WebWxStatusNotify(req, resp, info); err != nil { if err = b.Caller.WebWxStatusNotify(req, resp, info); err != nil {
return err return err
} }
// 开启协程,轮训获取是否有新的消息返回 // 开启协程,轮训获取是否有新的消息返回
go func() { go func() {
if b.GetMessageErrorHandler == nil { if b.GetMessageErrorHandler == nil {
b.GetMessageErrorHandler = b.stopAsyncCALL b.GetMessageErrorHandler = b.stopAsyncCALL
} }
if err := b.asyncCall(); err != nil { if err := b.asyncCall(); err != nil {
b.GetMessageErrorHandler(err) b.GetMessageErrorHandler(err)
} }
}() }()
return nil return nil
} }
// 轮训请求 // 轮训请求
// 根据状态码判断是否有新的请求 // 根据状态码判断是否有新的请求
func (b *Bot) asyncCall() error { func (b *Bot) asyncCall() error {
var ( var (
err error err error
resp *SyncCheckResponse resp *SyncCheckResponse
) )
for b.Alive() { for b.Alive() {
// 长轮训检查是否有消息返回 // 长轮训检查是否有消息返回
resp, err = b.Caller.SyncCheck(b.storage.LoginInfo, b.storage.Response) resp, err = b.Caller.SyncCheck(b.storage.LoginInfo, b.storage.Response)
if err != nil { if err != nil {
return err return err
} }
// 如果不是正常的状态码返回,发生了错误,直接退出 // 如果不是正常的状态码返回,发生了错误,直接退出
if !resp.Success() { if !resp.Success() {
return resp return resp
} }
// 如果Selector不为0则获取消息 // 如果Selector不为0则获取消息
if !resp.NorMal() { if !resp.NorMal() {
if err = b.getNewWechatMessage(); err != nil { if err = b.getNewWechatMessage(); err != nil {
return err return err
} }
} }
} }
return err return err
} }
// 当获取消息发生错误时, 默认的错误处理行为 // 当获取消息发生错误时, 默认的错误处理行为
func (b *Bot) stopAsyncCALL(err error) { func (b *Bot) stopAsyncCALL(err error) {
b.exit <- true b.exit <- true
b.err = err b.err = err
b.self = nil b.self = nil
log.Printf("exit with : %s", err.Error()) log.Printf("exit with : %s", err.Error())
} }
// 获取新的消息 // 获取新的消息
func (b *Bot) getNewWechatMessage() error { func (b *Bot) getNewWechatMessage() error {
resp, err := b.Caller.WebWxSync(b.storage.Request, b.storage.Response, b.storage.LoginInfo) resp, err := b.Caller.WebWxSync(b.storage.Request, b.storage.Response, b.storage.LoginInfo)
if err != nil { if err != nil {
return err return err
} }
// 更新SyncKey并且重新存入storage // 更新SyncKey并且重新存入storage
b.storage.Response.SyncKey = resp.SyncKey b.storage.Response.SyncKey = resp.SyncKey
// 遍历所有的新的消息,依次处理 // 遍历所有的新的消息,依次处理
for _, message := range resp.AddMsgList { for _, message := range resp.AddMsgList {
// 根据不同的消息类型来进行处理,方便后续统一调用 // 根据不同的消息类型来进行处理,方便后续统一调用
message.init(b) message.init(b)
// 调用自定义的处理方法 // 调用自定义的处理方法
if handler := b.MessageHandler; handler != nil { if handler := b.MessageHandler; handler != nil {
handler(message) handler(message)
} }
} }
return nil return nil
} }
// 当消息同步发生了错误或者用户主动在手机上退出,该方法会立即返回,否则会一直阻塞 // 当消息同步发生了错误或者用户主动在手机上退出,该方法会立即返回,否则会一直阻塞
func (b *Bot) Block() error { func (b *Bot) Block() error {
if b.self == nil { if b.self == nil {
return errors.New("`Block` must be called after user login") return errors.New("`Block` must be called after user login")
} }
if _, closed := <-b.exit; !closed { if _, closed := <-b.exit; !closed {
return errors.New("can not call `Block` after user logout") return errors.New("can not call `Block` after user logout")
} }
close(b.exit) close(b.exit)
return nil return nil
} }
// 获取当前Bot崩溃的原因 // 获取当前Bot崩溃的原因
func (b *Bot) CrashReason() error { func (b *Bot) CrashReason() error {
return b.err return b.err
} }
// setter for Bot.MessageHandler // setter for Bot.MessageHandler
func (b *Bot) MessageOnSuccess(h func(msg *Message)) { func (b *Bot) MessageOnSuccess(h func(msg *Message)) {
b.MessageHandler = h b.MessageHandler = h
} }
// setter for Bot.GetMessageErrorHandler // setter for Bot.GetMessageErrorHandler
func (b *Bot) MessageOnError(h func(err error)) { func (b *Bot) MessageOnError(h func(err error)) {
b.GetMessageErrorHandler = h b.GetMessageErrorHandler = h
} }
// Bot的构造方法需要自己传入Caller
func NewBot(caller *Caller) *Bot { func NewBot(caller *Caller) *Bot {
return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)} return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)}
} }
// 默认的Bot的构造方法,
// mode不传入默认为openwechat.Normal,详情见mode
// bot := openwechat.DefaultBot(openwechat.Desktop)
func DefaultBot(modes ...mode) *Bot { func DefaultBot(modes ...mode) *Bot {
var m mode var m mode
if len(modes) == 0 { if len(modes) == 0 {
m = Normal m = Normal
} else { } else {
m = modes[0] m = modes[0]
} }
urlManager := GetUrlManagerByMode(m) urlManager := GetUrlManagerByMode(m)
return NewBot(DefaultCaller(urlManager)) return NewBot(DefaultCaller(urlManager))
} }
// 通过uuid获取登录二维码的url
func GetQrcodeUrl(uuid string) string { func GetQrcodeUrl(uuid string) string {
return qrcodeUrl + uuid return qrcodeUrl + uuid
} }
// 打印登录二维码
func PrintlnQrcodeUrl(uuid string) { func PrintlnQrcodeUrl(uuid string) {
println("访问下面网址扫描二维码登录") println("访问下面网址扫描二维码登录")
println(GetQrcodeUrl(uuid)) println(GetQrcodeUrl(uuid))
} }

138
global.go
View File

@ -1,100 +1,102 @@
package openwechat package openwechat
import ( import (
"errors" "errors"
"regexp" "regexp"
) )
var ( var (
uuidRegexp = regexp.MustCompile(`uuid = "(.*?)";`) uuidRegexp = regexp.MustCompile(`uuid = "(.*?)";`)
statusCodeRegexp = regexp.MustCompile(`window.code=(\d+);`) statusCodeRegexp = regexp.MustCompile(`window.code=(\d+);`)
syncCheckRegexp = regexp.MustCompile(`window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}`) syncCheckRegexp = regexp.MustCompile(`window.synccheck=\{retcode:"(\d+)",selector:"(\d+)"\}`)
redirectUriRegexp = regexp.MustCompile(`window.redirect_uri="(.*?)"`) redirectUriRegexp = regexp.MustCompile(`window.redirect_uri="(.*?)"`)
) )
const ( const (
appId = "wx782c26e4c19acffb" appId = "wx782c26e4c19acffb"
jsLoginUrl = "https://login.wx.qq.com/jslogin" jsLoginUrl = "https://login.wx.qq.com/jslogin"
qrcodeUrl = "https://login.weixin.qq.com/qrcode/" qrcodeUrl = "https://login.weixin.qq.com/qrcode/"
loginUrl = "https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login" loginUrl = "https://login.wx.qq.com/cgi-bin/mmwebwx-bin/login"
// Normal urls // Normal urls
baseNormalUrl = "https://wx2.qq.com" baseNormalUrl = "https://wx2.qq.com"
webWxNewLoginPageNormalUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage" webWxNewLoginPageNormalUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage"
webWxInitNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit" webWxInitNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxinit"
webWxStatusNotifyNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify" webWxStatusNotifyNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify"
webWxSyncNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync" webWxSyncNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsync"
webWxSendMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg" webWxSendMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg"
webWxGetContactNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact" webWxGetContactNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact"
webWxSendMsgImgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg" webWxSendMsgImgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg"
webWxSendAppMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendappmsg" webWxSendAppMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxsendappmsg"
webWxBatchGetContactNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact" webWxBatchGetContactNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact"
webWxOplogNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxoplog" webWxOplogNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxoplog"
webWxVerifyUserNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxverifyuser" webWxVerifyUserNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxverifyuser"
syncCheckNormalUrl = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck" syncCheckNormalUrl = "https://webpush.wx2.qq.com/cgi-bin/mmwebwx-bin/synccheck"
webWxUpLoadMediaNormalUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia" webWxUpLoadMediaNormalUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia"
webWxGetMsgImgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg" webWxGetMsgImgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg"
webWxGetVoiceNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice" webWxGetVoiceNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice"
webWxGetVideoNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo" webWxGetVideoNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo"
webWxLogoutNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout" webWxLogoutNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout"
webWxGetMediaNormalUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia" webWxGetMediaNormalUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia"
webWxUpdateChatRoomNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxupdatechatroom" webWxUpdateChatRoomNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxupdatechatroom"
webWxRevokeMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg" webWxRevokeMsgNormalUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg"
// Desktop urls // Desktop urls
baseDesktopUrl = "https://wx.qq.com" baseDesktopUrl = "https://wx.qq.com"
webWxNewLoginPageDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?mod=desktop" webWxNewLoginPageDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxnewloginpage?mod=desktop"
webWxInitDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit" webWxInitDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxinit"
webWxStatusNotifyDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify" webWxStatusNotifyDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxstatusnotify"
webWxSyncDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync" webWxSyncDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsync"
webWxSendMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg" webWxSendMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsg"
webWxGetContactDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact" webWxGetContactDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetcontact"
webWxSendMsgImgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg" webWxSendMsgImgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendmsgimg"
webWxSendAppMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendappmsg" webWxSendAppMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxsendappmsg"
webWxBatchGetContactDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact" webWxBatchGetContactDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxbatchgetcontact"
webWxOplogDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxoplog" webWxOplogDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxoplog"
webWxVerifyUserDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxverifyuser" webWxVerifyUserDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxverifyuser"
syncCheckDesktopUrl = "https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck" syncCheckDesktopUrl = "https://webpush.wx.qq.com/cgi-bin/mmwebwx-bin/synccheck"
webWxUpLoadMediaDesktopUrl = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia" webWxUpLoadMediaDesktopUrl = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia"
webWxGetMsgImgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg" webWxGetMsgImgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg"
webWxGetVoiceDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice" webWxGetVoiceDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice"
webWxGetVideoDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo" webWxGetVideoDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo"
webWxLogoutDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout" webWxLogoutDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxlogout"
webWxGetMediaDesktopUrl = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia" webWxGetMediaDesktopUrl = "https://file.wx.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia"
webWxUpdateChatRoomDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxupdatechatroom" webWxUpdateChatRoomDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxupdatechatroom"
webWxRevokeMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg" webWxRevokeMsgDesktopUrl = "https://wx.qq.com/cgi-bin/mmwebwx-bin/webwxrevokemsg"
jsonContentType = "application/json; charset=utf-8" jsonContentType = "application/json; charset=utf-8"
uosPatchClientVersion = "2.0.0" uosPatchClientVersion = "2.0.0"
uosPatchExtspam = "Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQnYUKYotKkvLGpshucCaeWZMOylnc6o2AgDX9grhQQx7fm2DJRTyuNhUlwmEoWhjoG3F0ySAWUsEbH3bJMsEBwoB//0qmFJob74ffdaslqL+IrSy7LJ76/G5TkvNC+J0VQkpH1u3iJJs0uUYyLDzdBIQ6Ogd8LDQ3VKnJLm4g/uDLe+G7zzzkOPzCjXL+70naaQ9medzqmh+/SmaQ6uFWLDQLcRln++wBwoEibNpG4uOJvqXy+ql50DjlNchSuqLmeadFoo9/mDT0q3G7o/80P15ostktjb7h9bfNc+nZVSnUEJXbCjTeqS5UYuxn+HTS5nZsPVxJA2O5GdKCYK4x8lTTKShRstqPfbQpplfllx2fwXcSljuYi3YipPyS3GCAqf5A7aYYwJ7AvGqUiR2SsVQ9Nbp8MGHET1GxhifC692APj6SJxZD3i1drSYZPMMsS9rKAJTGz2FEupohtpf2tgXm6c16nDk/cw+C7K7me5j5PLHv55DFCS84b06AytZPdkFZLj7FHOkcFGJXitHkX5cgww7vuf6F3p0yM/W73SoXTx6GX4G6Hg2rYx3O/9VU2Uq8lvURB4qIbD9XQpzmyiFMaytMnqxcZJcoXCtfkTJ6pI7a92JpRUvdSitg967VUDUAQnCXCM/m0snRkR9LtoXAO1FUGpwlp1EfIdCZFPKNnXMeqev0j9W9ZrkEs9ZWcUEexSj5z+dKYQBhIICviYUQHVqBTZSNy22PlUIeDeIs11j7q4t8rD8LPvzAKWVqXE+5lS1JPZkjg4y5hfX1Dod3t96clFfwsvDP6xBSe1NBcoKbkyGxYK0UvPGtKQEE0Se2zAymYDv41klYE9s+rxp8e94/H8XhrL9oGm8KWb2RmYnAE7ry9gd6e8ZuBRIsISlJAE/e8y8xFmP031S6Lnaet6YXPsFpuFsdQs535IjcFd75hh6DNMBYhSfjv456cvhsb99+fRw/KVZLC3yzNSCbLSyo9d9BI45Plma6V8akURQA/qsaAzU0VyTIqZJkPDTzhuCl92vD2AD/QOhx6iwRSVPAxcRFZcWjgc2wCKh+uCYkTVbNQpB9B90YlNmI3fWTuUOUjwOzQRxJZj11NsimjOJ50qQwTTFj6qQvQ1a/I+MkTx5UO+yNHl718JWcR3AXGmv/aa9rD1eNP8ioTGlOZwPgmr2sor2iBpKTOrB83QgZXP+xRYkb4zVC+LoAXEoIa1+zArywlgREer7DLePukkU6wHTkuSaF+ge5Of1bXuU4i938WJHj0t3D8uQxkJvoFi/EYN/7u2P1zGRLV4dHVUsZMGCCtnO6BBigFMAA=" uosPatchExtspam = "Gp8ICJkIEpkICggwMDAwMDAwMRAGGoAI1GiJSIpeO1RZTq9QBKsRbPJdi84ropi16EYI10WB6g74sGmRwSNXjPQnYUKYotKkvLGpshucCaeWZMOylnc6o2AgDX9grhQQx7fm2DJRTyuNhUlwmEoWhjoG3F0ySAWUsEbH3bJMsEBwoB//0qmFJob74ffdaslqL+IrSy7LJ76/G5TkvNC+J0VQkpH1u3iJJs0uUYyLDzdBIQ6Ogd8LDQ3VKnJLm4g/uDLe+G7zzzkOPzCjXL+70naaQ9medzqmh+/SmaQ6uFWLDQLcRln++wBwoEibNpG4uOJvqXy+ql50DjlNchSuqLmeadFoo9/mDT0q3G7o/80P15ostktjb7h9bfNc+nZVSnUEJXbCjTeqS5UYuxn+HTS5nZsPVxJA2O5GdKCYK4x8lTTKShRstqPfbQpplfllx2fwXcSljuYi3YipPyS3GCAqf5A7aYYwJ7AvGqUiR2SsVQ9Nbp8MGHET1GxhifC692APj6SJxZD3i1drSYZPMMsS9rKAJTGz2FEupohtpf2tgXm6c16nDk/cw+C7K7me5j5PLHv55DFCS84b06AytZPdkFZLj7FHOkcFGJXitHkX5cgww7vuf6F3p0yM/W73SoXTx6GX4G6Hg2rYx3O/9VU2Uq8lvURB4qIbD9XQpzmyiFMaytMnqxcZJcoXCtfkTJ6pI7a92JpRUvdSitg967VUDUAQnCXCM/m0snRkR9LtoXAO1FUGpwlp1EfIdCZFPKNnXMeqev0j9W9ZrkEs9ZWcUEexSj5z+dKYQBhIICviYUQHVqBTZSNy22PlUIeDeIs11j7q4t8rD8LPvzAKWVqXE+5lS1JPZkjg4y5hfX1Dod3t96clFfwsvDP6xBSe1NBcoKbkyGxYK0UvPGtKQEE0Se2zAymYDv41klYE9s+rxp8e94/H8XhrL9oGm8KWb2RmYnAE7ry9gd6e8ZuBRIsISlJAE/e8y8xFmP031S6Lnaet6YXPsFpuFsdQs535IjcFd75hh6DNMBYhSfjv456cvhsb99+fRw/KVZLC3yzNSCbLSyo9d9BI45Plma6V8akURQA/qsaAzU0VyTIqZJkPDTzhuCl92vD2AD/QOhx6iwRSVPAxcRFZcWjgc2wCKh+uCYkTVbNQpB9B90YlNmI3fWTuUOUjwOzQRxJZj11NsimjOJ50qQwTTFj6qQvQ1a/I+MkTx5UO+yNHl718JWcR3AXGmv/aa9rD1eNP8ioTGlOZwPgmr2sor2iBpKTOrB83QgZXP+xRYkb4zVC+LoAXEoIa1+zArywlgREer7DLePukkU6wHTkuSaF+ge5Of1bXuU4i938WJHj0t3D8uQxkJvoFi/EYN/7u2P1zGRLV4dHVUsZMGCCtnO6BBigFMAA="
) )
// 消息类型 // 消息类型
const ( const (
TextMessage = 1 TextMessage = 1
ImageMessage = 3 ImageMessage = 3
AppMessage = 6 AppMessage = 6
) )
// 登录状态 // 登录状态
const ( const (
statusSuccess = "200" statusSuccess = "200"
statusScanned = "201" statusScanned = "201"
statusTimeout = "400" statusTimeout = "400"
statusWait = "408" statusWait = "408"
) )
// errors // errors
var ( var (
noSuchUserFoundError = errors.New("no such user found") noSuchUserFoundError = errors.New("no such user found")
) )
// ALL跟search函数搭配
// friends.Search(openwechat.ALL, )
const ALL = 0 const ALL = 0
// sex // 性别
const ( const (
MALE = 1 MALE = 1
FEMALE = 2 FEMALE = 2
) )

160
url.go
View File

@ -2,79 +2,79 @@ package openwechat
// url信息存储 // url信息存储
type UrlManager struct { type UrlManager struct {
baseUrl string baseUrl string
webWxNewLoginPageUrl string webWxNewLoginPageUrl string
webWxInitUrl string webWxInitUrl string
webWxStatusNotifyUrl string webWxStatusNotifyUrl string
webWxSyncUrl string webWxSyncUrl string
webWxSendMsgUrl string webWxSendMsgUrl string
webWxGetContactUrl string webWxGetContactUrl string
webWxSendMsgImgUrl string webWxSendMsgImgUrl string
webWxSendAppMsgUrl string webWxSendAppMsgUrl string
webWxBatchGetContactUrl string webWxBatchGetContactUrl string
webWxOplogUrl string webWxOplogUrl string
webWxVerifyUserUrl string webWxVerifyUserUrl string
syncCheckUrl string syncCheckUrl string
webWxUpLoadMediaUrl string webWxUpLoadMediaUrl string
webWxGetMsgImgUrl string webWxGetMsgImgUrl string
webWxGetVoiceUrl string webWxGetVoiceUrl string
webWxGetVideoUrl string webWxGetVideoUrl string
webWxLogoutUrl string webWxLogoutUrl string
webWxGetMediaUrl string webWxGetMediaUrl string
webWxUpdateChatRoomUrl string webWxUpdateChatRoomUrl string
webWxRevokeMsg string webWxRevokeMsg string
} }
var ( var (
// uos版 // uos版
desktop = UrlManager{ desktop = UrlManager{
baseUrl: baseDesktopUrl, baseUrl: baseDesktopUrl,
webWxNewLoginPageUrl: webWxNewLoginPageDesktopUrl, webWxNewLoginPageUrl: webWxNewLoginPageDesktopUrl,
webWxInitUrl: webWxInitDesktopUrl, webWxInitUrl: webWxInitDesktopUrl,
webWxStatusNotifyUrl: webWxStatusNotifyDesktopUrl, webWxStatusNotifyUrl: webWxStatusNotifyDesktopUrl,
webWxSyncUrl: webWxSyncDesktopUrl, webWxSyncUrl: webWxSyncDesktopUrl,
webWxSendMsgUrl: webWxSendMsgDesktopUrl, webWxSendMsgUrl: webWxSendMsgDesktopUrl,
webWxGetContactUrl: webWxGetContactDesktopUrl, webWxGetContactUrl: webWxGetContactDesktopUrl,
webWxSendMsgImgUrl: webWxSendMsgImgDesktopUrl, webWxSendMsgImgUrl: webWxSendMsgImgDesktopUrl,
webWxSendAppMsgUrl: webWxSendAppMsgDesktopUrl, webWxSendAppMsgUrl: webWxSendAppMsgDesktopUrl,
webWxBatchGetContactUrl: webWxBatchGetContactDesktopUrl, webWxBatchGetContactUrl: webWxBatchGetContactDesktopUrl,
webWxOplogUrl: webWxOplogDesktopUrl, webWxOplogUrl: webWxOplogDesktopUrl,
webWxVerifyUserUrl: webWxVerifyUserDesktopUrl, webWxVerifyUserUrl: webWxVerifyUserDesktopUrl,
syncCheckUrl: syncCheckDesktopUrl, syncCheckUrl: syncCheckDesktopUrl,
webWxUpLoadMediaUrl: webWxUpLoadMediaDesktopUrl, webWxUpLoadMediaUrl: webWxUpLoadMediaDesktopUrl,
webWxGetMsgImgUrl: webWxGetMsgImgDesktopUrl, webWxGetMsgImgUrl: webWxGetMsgImgDesktopUrl,
webWxGetVoiceUrl: webWxGetVoiceDesktopUrl, webWxGetVoiceUrl: webWxGetVoiceDesktopUrl,
webWxGetVideoUrl: webWxGetVideoDesktopUrl, webWxGetVideoUrl: webWxGetVideoDesktopUrl,
webWxLogoutUrl: webWxLogoutDesktopUrl, webWxLogoutUrl: webWxLogoutDesktopUrl,
webWxGetMediaUrl: webWxGetMediaDesktopUrl, webWxGetMediaUrl: webWxGetMediaDesktopUrl,
webWxUpdateChatRoomUrl: webWxUpdateChatRoomDesktopUrl, webWxUpdateChatRoomUrl: webWxUpdateChatRoomDesktopUrl,
webWxRevokeMsg: webWxRevokeMsgDesktopUrl, webWxRevokeMsg: webWxRevokeMsgDesktopUrl,
} }
// 网页版 // 网页版
normal = UrlManager{ normal = UrlManager{
baseUrl: baseNormalUrl, baseUrl: baseNormalUrl,
webWxNewLoginPageUrl: webWxNewLoginPageNormalUrl, webWxNewLoginPageUrl: webWxNewLoginPageNormalUrl,
webWxInitUrl: webWxInitNormalUrl, webWxInitUrl: webWxInitNormalUrl,
webWxStatusNotifyUrl: webWxStatusNotifyNormalUrl, webWxStatusNotifyUrl: webWxStatusNotifyNormalUrl,
webWxSyncUrl: webWxSyncNormalUrl, webWxSyncUrl: webWxSyncNormalUrl,
webWxSendMsgUrl: webWxSendMsgNormalUrl, webWxSendMsgUrl: webWxSendMsgNormalUrl,
webWxGetContactUrl: webWxGetContactNormalUrl, webWxGetContactUrl: webWxGetContactNormalUrl,
webWxSendMsgImgUrl: webWxSendMsgImgNormalUrl, webWxSendMsgImgUrl: webWxSendMsgImgNormalUrl,
webWxSendAppMsgUrl: webWxSendAppMsgNormalUrl, webWxSendAppMsgUrl: webWxSendAppMsgNormalUrl,
webWxBatchGetContactUrl: webWxBatchGetContactNormalUrl, webWxBatchGetContactUrl: webWxBatchGetContactNormalUrl,
webWxOplogUrl: webWxOplogNormalUrl, webWxOplogUrl: webWxOplogNormalUrl,
webWxVerifyUserUrl: webWxVerifyUserNormalUrl, webWxVerifyUserUrl: webWxVerifyUserNormalUrl,
syncCheckUrl: syncCheckNormalUrl, syncCheckUrl: syncCheckNormalUrl,
webWxUpLoadMediaUrl: webWxUpLoadMediaNormalUrl, webWxUpLoadMediaUrl: webWxUpLoadMediaNormalUrl,
webWxGetMsgImgUrl: webWxGetMsgImgNormalUrl, webWxGetMsgImgUrl: webWxGetMsgImgNormalUrl,
webWxGetVoiceUrl: webWxGetVoiceNormalUrl, webWxGetVoiceUrl: webWxGetVoiceNormalUrl,
webWxGetVideoUrl: webWxGetVideoNormalUrl, webWxGetVideoUrl: webWxGetVideoNormalUrl,
webWxLogoutUrl: webWxLogoutNormalUrl, webWxLogoutUrl: webWxLogoutNormalUrl,
webWxGetMediaUrl: webWxGetMediaNormalUrl, webWxGetMediaUrl: webWxGetMediaNormalUrl,
webWxUpdateChatRoomUrl: webWxUpdateChatRoomNormalUrl, webWxUpdateChatRoomUrl: webWxUpdateChatRoomNormalUrl,
webWxRevokeMsg: webWxRevokeMsgNormalUrl, webWxRevokeMsg: webWxRevokeMsgNormalUrl,
} }
) )
// mode 类型限制 // mode 类型限制
@ -82,17 +82,19 @@ type mode string
// 向外暴露2种模式 // 向外暴露2种模式
const ( const (
Normal mode = "normal" Normal mode = "normal"
Desktop mode = "desktop" Desktop mode = "desktop" // 突破网页版登录限制
) )
// 通过mode获取完善的UrlManager,
// mode有且仅有两种模式: Normal && Desktop
func GetUrlManagerByMode(m mode) UrlManager { func GetUrlManagerByMode(m mode) UrlManager {
switch m { switch m {
case Desktop: case Desktop:
return desktop return desktop
case Normal: case Normal:
return normal return normal
default: default:
panic("unsupport mode got") panic("unsupport mode got")
} }
} }