Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: unified cookie error #17

Merged
merged 10 commits into from
Dec 7, 2024
9 changes: 4 additions & 5 deletions errno/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,10 @@ var (
AuthorizationFailedError = NewErrNo(AuthorizationFailedErrCode, "authorization failed")

// User
AccountConflictError = NewErrNo(AuthorizationFailedErrCode, "account conflict")
IdentifierExpiredError = NewErrNo(AuthorizationFailedErrCode, "session expired")
LoginCheckFailedError = NewErrNo(AuthorizationFailedErrCode, "login check failed")
SSOLoginFailedError = NewErrNo(AuthorizationFailedErrCode, "sso login failed")
GetIdentifierFailedError = NewErrNo(AuthorizationFailedErrCode, "get session failed")
AccountConflictError = NewErrNo(AuthorizationFailedErrCode, "account conflict")
CookieError = NewErrNo(AuthorizationFailedErrCode, "id error or session expired")
LoginCheckFailedError = NewErrNo(AuthorizationFailedErrCode, "login check failed")
SSOLoginFailedError = NewErrNo(AuthorizationFailedErrCode, "sso login failed")

// HTTP
HTTPQueryError = NewErrNo(HTTPQueryErrorCode, "HTTP query failed")
Expand Down
15 changes: 8 additions & 7 deletions jwch.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,14 @@ func (s *Student) NewRequest() *resty.Request {

func (s *Student) GetWithIdentifier(url string) (*html.Node, error) {
resp, err := s.NewRequest().SetHeader("Referer", constants.JwchReferer).SetQueryParam("id", s.Identifier).Get(url)
// 会话过期:会直接重定向,但我们禁用了重定向,所以会有error
if err != nil {
return nil, errno.IdentifierExpiredError.WithErr(err)
return nil, errno.CookieError
}

// 会话过期 TODO: 判断条件有点简陋
// 还有一种情况是 id 或 cookie 缺失或者解析错误 TODO: 判断条件有点简陋
if strings.Contains(string(resp.Body()), "重新登录") {
return nil, errno.IdentifierExpiredError
return nil, errno.CookieError
}

return htmlquery.Parse(bytes.NewReader(resp.Body()))
Expand All @@ -95,14 +96,14 @@ func (s *Student) PostWithIdentifier(url string, formData map[string]string) (*h
resp, err := s.NewRequest().SetHeader("Referer", constants.JwchReferer).SetQueryParam("id", s.Identifier).SetFormData(formData).Post(url)

s.NewRequest().EnableTrace()

// 会话过期:会直接重定向,但我们禁用了重定向,所以会有error
if err != nil {
return nil, errno.IdentifierExpiredError.WithErr(err)
return nil, errno.CookieError.WithErr(err)
}

// Identifier缺失 TODO: 判断条件有点简陋
// id 或 cookie 缺失或者解析错误 TODO: 判断条件有点简陋
if strings.Contains(string(resp.Body()), "处理URL失败") {
return nil, errno.IdentifierExpiredError
return nil, errno.CookieError
}

return htmlquery.Parse(strings.NewReader(strings.TrimSpace(string(resp.Body()))))
Expand Down
6 changes: 3 additions & 3 deletions user.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func (s *Student) Login() error {

// 这里是err == nil 因为禁止了重定向,正常登录是会出现异常的
if err == nil {
return errno.GetIdentifierFailedError
return errno.CookieError
}

data := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error())

if len(data) < 1 {
return errno.GetIdentifierFailedError
return errno.CookieError
}

s.SetIdentifier(data[1])
Expand Down Expand Up @@ -167,7 +167,7 @@ func (s *Student) CheckSession() error {
res := htmlquery.FindOne(resp, `//*[@id="ContentPlaceHolder1_LB_xh"]`)

if res == nil {
return errno.IdentifierExpiredError.WithErr(err)
return errno.CookieError
}

if htmlquery.OutputHTML(res, false) != s.ID {
Expand Down
Loading