[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" "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 { type BotPreparer interface {
Prepare(*Bot) Prepare(*Bot)
} }
@ -250,7 +279,7 @@ func (l *LoginChecker) CheckLogin() error {
tip = "0" tip = "0"
} }
switch code { switch code {
case StatusSuccess: case LoginCodeSuccess:
// 判断是否有登录回调,如果有执行它 // 判断是否有登录回调,如果有执行它
redirectURL, err := resp.RedirectURL() redirectURL, err := resp.RedirectURL()
if err != nil { if err != nil {
@ -263,14 +292,14 @@ func (l *LoginChecker) CheckLogin() error {
cb(resp) cb(resp)
} }
return nil return nil
case StatusScanned: case LoginCodeScanned:
// 执行扫码回调 // 执行扫码回调
if cb := l.ScanCallBack; cb != nil { if cb := l.ScanCallBack; cb != nil {
cb(resp) cb(resp)
} }
case StatusTimeout: case LoginCodeTimeout:
return ErrLoginTimeout return ErrLoginTimeout
case StatusWait: case LoginCodeWait:
continue continue
} }
} }

View File

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

View File

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