Skip to content

Commit

Permalink
add: GetValidateCode
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail committed Sep 27, 2024
1 parent 5d834c9 commit a792187
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
22 changes: 22 additions & 0 deletions jwch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwch
import (
"bytes"
"crypto/tls"
"encoding/json"
"net/http"
"strings"

Expand Down Expand Up @@ -88,3 +89,24 @@ func (s *Student) PostWithIdentifier(url string, formData map[string]string) (*h

return htmlquery.Parse(strings.NewReader(strings.TrimSpace(string(resp.Body()))))
}

// 获取验证码
func GetValidateCode(image string) (string, error) {
// 请求西二服务器,自动识别验证码
code := verifyCodeResponse{}

s := NewStudent()
resp, err := s.NewRequest().SetFormData(map[string]string{
"validateCode": image,
}).Post("https://statistics.fzuhelper.w2fzu.com/api/login/validateCode?validateCode")
if err != nil {
return "", errno.HTTPQueryError.WithMessage("automatic code identification failed")
}

err = json.Unmarshal(resp.Body(), &code)

if err != nil {
return "", errno.HTTPQueryError.WithErr(err)
}
return code.Message, nil
}
18 changes: 16 additions & 2 deletions jwch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@ func login() error {
return nil
}

func Test_GetValidateCode(t *testing.T) {
// 获取验证码图片
s := NewStudent()
resp, err := s.NewRequest().Get("https://jwcjwxt1.fzu.edu.cn/plus/verifycode.asp")
if err != nil {
t.Error(err)
}
code, err := GetValidateCode(utils.Base64EncodeHTTPImage(resp.Body()))
if err != nil {
t.Error(err)
}
fmt.Println(code)
}

func Test_GetIdentifierAndCookies(t *testing.T) {
Identifier, cookies := stu.GetIdentifierAndCookies()
fmt.Println(Identifier)
Expand Down Expand Up @@ -124,9 +138,9 @@ func Test_GetQiShanEmptyRoom(t *testing.T) {

rooms, err := stu.GetQiShanEmptyRoom(EmptyRoomReq{
Campus: "旗山校区",
Time: "2024-09-19",
Time: "2024-09-26",
Start: "1",
End: "2",
End: "8",
})
if err != nil {
t.Error(err)
Expand Down

0 comments on commit a792187

Please sign in to comment.