[refactor]: 增加 LoginCode 定义 (#204)

This commit is contained in:
多吃点苹果 2023-01-13 19:56:52 +08:00 committed by GitHub
parent 53478bad19
commit 63143f9364
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 17 deletions

View File

@ -4,6 +4,35 @@ import (
"time"
)
// LoginCode 定义登录状态码
type LoginCode string
const (
// LoginCodeSuccess 登录成功
LoginCodeSuccess LoginCode = "200"
// LoginCodeScanned 已扫码
LoginCodeScanned LoginCode = "201"
// LoginCodeTimeout 登录超时
LoginCodeTimeout LoginCode = "400"
// LoginCodeWait 等待扫码
LoginCodeWait LoginCode = "408"
)
func (l LoginCode) String() string {
switch l {
case LoginCodeSuccess:
return "登录成功"
case LoginCodeScanned:
return "已扫码"
case LoginCodeTimeout:
return "登录超时"
case LoginCodeWait:
return "等待扫码"
default:
return "未知状态"
}
}
type BotPreparer interface {
Prepare(*Bot)
}
@ -250,7 +279,7 @@ func (l *LoginChecker) CheckLogin() error {
tip = "0"
}
switch code {
case StatusSuccess:
case LoginCodeSuccess:
// 判断是否有登录回调,如果有执行它
redirectURL, err := resp.RedirectURL()
if err != nil {
@ -263,14 +292,14 @@ func (l *LoginChecker) CheckLogin() error {
cb(resp)
}
return nil
case StatusScanned:
case LoginCodeScanned:
// 执行扫码回调
if cb := l.ScanCallBack; cb != nil {
cb(resp)
}
case StatusTimeout:
case LoginCodeTimeout:
return ErrLoginTimeout
case StatusWait:
case LoginCodeWait:
continue
}
}

View File

@ -98,14 +98,6 @@ const (
AppMsgTypeReaderType AppMessageType = 100001 //自定义的消息
)
// 登录状态
const (
StatusSuccess = "200"
StatusScanned = "201"
StatusTimeout = "400"
StatusWait = "408"
)
// ALL 跟search函数搭配
//
// friends.Search(openwechat.ALL, )

View File

@ -128,8 +128,8 @@ func (c CheckLoginResponse) RedirectURL() (*url.URL, error) {
if err != nil {
return nil, err
}
if code != StatusSuccess {
return nil, fmt.Errorf("expect status code %s, but got %s", StatusSuccess, code)
if code != LoginCodeSuccess {
return nil, fmt.Errorf("expect status code %s, but got %s", LoginCodeSuccess, code)
}
results := redirectUriRegexp.FindSubmatch(c)
if len(results) != 2 {
@ -139,13 +139,13 @@ func (c CheckLoginResponse) RedirectURL() (*url.URL, error) {
}
// Code 获取当前的登录检查状态的代码
func (c CheckLoginResponse) Code() (string, error) {
func (c CheckLoginResponse) Code() (LoginCode, error) {
results := statusCodeRegexp.FindSubmatch(c)
if len(results) != 2 {
return "", errors.New("error status code match")
}
code := string(results[1])
return code, nil
return LoginCode(code), nil
}
// Avatar 获取扫码后的用户头像, base64编码
@ -154,7 +154,7 @@ func (c CheckLoginResponse) Avatar() (string, error) {
if err != nil {
return "", err
}
if code != StatusScanned {
if code != LoginCodeScanned {
return "", nil
}
results := avatarRegexp.FindSubmatch(c)