add user logout support
This commit is contained in:
parent
96d14f39f6
commit
c1c2fd9f72
9
bot.go
9
bot.go
@ -69,6 +69,15 @@ func (b *Bot) Login() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) Logout() error {
|
||||
info := b.storage.GetLoginInfo()
|
||||
if err := b.Caller.Logout(info); err != nil {
|
||||
return err
|
||||
}
|
||||
b.stopAsyncCALL(errors.New("logout"))
|
||||
return nil
|
||||
}
|
||||
|
||||
// 登录逻辑
|
||||
func (b *Bot) login(data []byte) error {
|
||||
// 判断是否有登录回调,如果有执行它
|
||||
|
12
bot_test.go
12
bot_test.go
@ -2,22 +2,14 @@ package openwechat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDefaultBot(t *testing.T) {
|
||||
bot := DefaultBot()
|
||||
messageHandler := func(message *Message) {
|
||||
if message.HasFile() {
|
||||
if message.IsMedia() {
|
||||
resp, err := message.GetFile()
|
||||
if err == nil {
|
||||
data, _ := ioutil.ReadAll(resp.Body)
|
||||
ioutil.WriteFile(message.EncryFileName, data, 0x777)
|
||||
resp.Body.Close()
|
||||
}
|
||||
}
|
||||
if message.Content == "logout" {
|
||||
bot.Logout()
|
||||
}
|
||||
}
|
||||
bot.RegisterMessageHandler(messageHandler)
|
||||
|
@ -221,6 +221,12 @@ func (c *Caller) WebWxSendImageMsg(file *os.File, request BaseRequest, info Logi
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 用户退出
|
||||
func (c *Caller) Logout(info LoginInfo) error {
|
||||
resp := NewReturnResponse(c.Client.Logout(info))
|
||||
return parseBaseResponseError(resp)
|
||||
}
|
||||
|
||||
// 处理响应返回的结果是否正常
|
||||
func parseBaseResponseError(resp *ReturnResponse) error {
|
||||
if resp.Err() != nil {
|
||||
|
10
client.go
10
client.go
@ -390,3 +390,13 @@ func (c *Client) WebWxGetMedia(msg *Message, info LoginInfo) (*http.Response, er
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
||||
func (c *Client) Logout(info LoginInfo) (*http.Response, error) {
|
||||
path, _ := url.Parse(webWxLogoutUrl)
|
||||
params := url.Values{}
|
||||
params.Add("redirect", "1")
|
||||
params.Add("type", "1")
|
||||
params.Add("skey", info.SKey)
|
||||
path.RawQuery = params.Encode()
|
||||
return c.Get(path.String())
|
||||
}
|
||||
|
@ -32,6 +32,7 @@ const (
|
||||
webWxGetMsgImgUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmsgimg"
|
||||
webWxGetVoiceUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvoice"
|
||||
webWxGetVideoUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetvideo"
|
||||
webWxLogoutUrl = "https://wx2.qq.com/cgi-bin/mmwebwx-bin/webwxlogout"
|
||||
webWxGetMediaUrl = "https://file.wx2.qq.com/cgi-bin/mmwebwx-bin/webwxgetmedia"
|
||||
|
||||
jsonContentType = "application/json; charset=utf-8"
|
||||
|
Loading…
x
Reference in New Issue
Block a user