diff --git a/bot_login.go b/bot_login.go index 9fd1ca7..d56c29b 100644 --- a/bot_login.go +++ b/bot_login.go @@ -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 } } diff --git a/global.go b/global.go index bb68cb4..a5033ff 100644 --- a/global.go +++ b/global.go @@ -98,14 +98,6 @@ const ( AppMsgTypeReaderType AppMessageType = 100001 //自定义的消息 ) -// 登录状态 -const ( - StatusSuccess = "200" - StatusScanned = "201" - StatusTimeout = "400" - StatusWait = "408" -) - // ALL 跟search函数搭配 // // friends.Search(openwechat.ALL, ) diff --git a/items.go b/items.go index 11bd4dd..f9ce0d6 100644 --- a/items.go +++ b/items.go @@ -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)