删除冗余的代码

This commit is contained in:
eatMoreApple 2021-04-30 14:50:39 +08:00
parent 66acd48e3a
commit 24a43987e3
4 changed files with 218 additions and 218 deletions

16
bot.go
View File

@ -1,6 +1,7 @@
package openwechat
import (
"context"
"errors"
"log"
"net/url"
@ -14,7 +15,8 @@ type Bot struct {
GetMessageErrorHandler func(err error) // 获取消息发生错误的handle
isHot bool
err error
exit chan bool
context context.Context
cancel context.CancelFunc
Caller *Caller
self *Self
storage *Storage
@ -27,7 +29,7 @@ func (b *Bot) Alive() bool {
return false
}
select {
case <-b.exit:
case <-b.context.Done():
return false
default:
return true
@ -236,7 +238,7 @@ func (b *Bot) asyncCall() error {
// 当获取消息发生错误时, 默认的错误处理行为
func (b *Bot) stopAsyncCALL(err error) {
b.exit <- true
b.cancel()
b.err = err
b.self = nil
log.Printf("exit with : %s", err.Error())
@ -267,10 +269,7 @@ func (b *Bot) Block() error {
if b.self == nil {
return errors.New("`Block` must be called after user login")
}
if _, notClose := <-b.exit; !notClose {
return errors.New("can not call `Block` after user logout")
}
close(b.exit)
<-b.context.Done()
return nil
}
@ -291,7 +290,8 @@ func (b *Bot) MessageOnError(h func(err error)) {
// Bot的构造方法需要自己传入Caller
func NewBot(caller *Caller) *Bot {
return &Bot{Caller: caller, storage: &Storage{}, exit: make(chan bool)}
ctx, cancel := context.WithCancel(context.Background())
return &Bot{Caller: caller, storage: &Storage{}, context: ctx, cancel: cancel}
}
// 默认的Bot的构造方法,

View File

@ -230,8 +230,6 @@ func (c *Client) sendMessage(request *BaseRequest, url string, msg *SendMessage)
body, _ := ToBuffer(content)
req, _ := http.NewRequest(http.MethodPost, url, body)
req.Header.Add("Content-Type", jsonContentType)
fmt.Println(6666)
fmt.Println(body.String())
return c.Do(req)
}

View File

@ -124,3 +124,5 @@ var imageType = map[string]bool{
}
var videoType = "mp4"
const appMessageAppId = "wxeb7ec651dd0aefa9"

View File

@ -495,7 +495,7 @@ func (f appmsg) XmlByte() ([]byte, error) {
}
func NewFileAppMessage(stat os.FileInfo, attachId string) *appmsg {
m := &appmsg{AppId: "wxeb7ec651dd0aefa9", Title: stat.Name()}
m := &appmsg{AppId: appMessageAppId, Title: stat.Name()}
m.AppAttach.AttachId = attachId
m.AppAttach.TotalLen = stat.Size()
m.Type = 6