diff --git a/calendar.go b/calendar.go index 2ed1b9e..eceab62 100644 --- a/calendar.go +++ b/calendar.go @@ -10,7 +10,7 @@ import ( ) func (s *Student) GetSchoolCalendar() (*SchoolCalendar, error) { - resp, err := s.GetWithSession(constants.SchoolCalendarURL) + resp, err := s.GetWithIdentifier(constants.SchoolCalendarURL) if err != nil { return nil, err diff --git a/course.go b/course.go index 31c7cef..4073eac 100644 --- a/course.go +++ b/course.go @@ -15,7 +15,7 @@ import ( // 获取我的学期 func (s *Student) GetTerms() (*Term, error) { - resp, err := s.GetWithSession(constants.CourseURL) + resp, err := s.GetWithIdentifier(constants.CourseURL) if err != nil { return nil, err diff --git a/jwch.go b/jwch.go index c5a2fa1..ca17029 100644 --- a/jwch.go +++ b/jwch.go @@ -7,7 +7,6 @@ import ( "strings" "github.com/west2-online/jwch/errno" - "github.com/west2-online/jwch/utils" "github.com/antchfx/htmlquery" "github.com/go-resty/resty/v2" @@ -32,41 +31,26 @@ func (s *Student) WithUser(id, password string) *Student { return s } -func (s *Student) WithSession(session string) *Student { - s.LoginData.Session = session - return s -} - -// SaveLoginData save session and cookie to localfile -func (s *Student) SaveLoginData(filePath string) error { - return utils.SaveData(filePath, []byte(utils.PrintStruct(s.LoginData))) +func (s *Student) SetIdentifier(identifier string) { + s.Identifier = identifier } -func (s *Student) GetLoginDataJSON() string { - return utils.PrintStruct(s.LoginData) +func (s *Student) SetCookies(cookies []*http.Cookie) { + s.cookies = cookies + s.client.SetCookies(cookies) } func (s *Student) ClearLoginData() { - s.LoginData = LoginData{} - s.client.Cookies = s.LoginData.Cookies -} - -func (s *Student) SetLoginData(data LoginData) { - s.LoginData = data - s.client = s.client.SetCookies(s.LoginData.Cookies) -} - -func (s *Student) AppendCookies(cookies []*http.Cookie) { - s.LoginData.Cookies = append(s.LoginData.Cookies, cookies...) + s.cookies = []*http.Cookie{} + s.client.Cookies = []*http.Cookie{} } func (s *Student) NewRequest() *resty.Request { return s.client.R() } -// GetWithSession returns parse tree for the resp of the request. -func (s *Student) GetWithSession(url string) (*html.Node, error) { - resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).Get(url) +func (s *Student) GetWithIdentifier(url string) (*html.Node, error) { + resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.Identifier).Get(url) if err != nil { return nil, errno.HTTPQueryError.WithErr(err) @@ -80,25 +64,9 @@ func (s *Student) GetWithSession(url string) (*html.Node, error) { return htmlquery.Parse(bytes.NewReader(resp.Body())) } -// GetWithSessionRaw returns the raw data of response -func (s *Student) GetWithSessionRaw(url string) (*resty.Response, error) { - resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).Get(url) - - if err != nil { - return nil, errno.HTTPQueryError.WithErr(err) - } - - // 会话过期 TODO: 判断条件有点简陋 - if strings.Contains(string(resp.Body()), "重新登录") { - return nil, errno.SessionExpiredError - } - - return resp, nil -} - // PostWithSession returns parse tree for the resp of the request. -func (s *Student) PostWithSession(url string, formdata map[string]string) (*html.Node, error) { - resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.LoginData.Session).SetFormData(formdata).Post(url) +func (s *Student) PostWithSession(url string, formData map[string]string) (*html.Node, error) { + resp, err := s.NewRequest().SetHeader("Referer", "https://jwcjwxt1.fzu.edu.cn/").SetQueryParam("id", s.Identifier).SetFormData(formData).Post(url) s.NewRequest().EnableTrace() diff --git a/jwch_test.go b/jwch_test.go index 592b4e6..c9ebe66 100644 --- a/jwch_test.go +++ b/jwch_test.go @@ -43,32 +43,6 @@ func Test_Login(t *testing.T) { } } -func Test_LoginFromLocal(t *testing.T) { - var res LoginData - err := utils.JSONUnmarshalFromFile(localfile, &res) - if err != nil { - t.Error(err) - } - stu.SetLoginData(res) - - err = stu.CheckSession() - - if err != nil { - t.Log("session expire, relogin") - err = stu.Login() - if err != nil { - t.Error(err) - } - err = stu.CheckSession() - - if err != nil { - t.Error(err) - } - - stu.SaveLoginData(localfile) - } -} - func Test_GetCourse(t *testing.T) { if !islogin { err := login() diff --git a/mark.go b/mark.go index 012de76..16cdb50 100644 --- a/mark.go +++ b/mark.go @@ -13,7 +13,7 @@ import ( // 获取成绩,由于教务处缺陷,这里会返回全部的成绩 func (s *Student) GetMarks() (resp []*Mark, err error) { - res, err := s.GetWithSession(constants.MarksQueryURL) + res, err := s.GetWithIdentifier(constants.MarksQueryURL) if err != nil { return nil, err @@ -62,7 +62,7 @@ func (s *Student) GetMarks() (resp []*Mark, err error) { // 获取CET成绩 func (s *Student) GetCET() error { - resp, err := s.GetWithSession(constants.CETQueryURL) + resp, err := s.GetWithIdentifier(constants.CETQueryURL) if err != nil { return err diff --git a/model.go b/model.go index 7f3fbf4..cf04a59 100644 --- a/model.go +++ b/model.go @@ -1,23 +1,19 @@ package jwch import ( - "net/http" - "github.com/go-resty/resty/v2" + "net/http" ) -// 本地数据 -type LoginData struct { - Cookies []*http.Cookie `json:"cookies"` - Session string `json:"session"` -} - // 学生对象 type Student struct { - ID string `json:"id"` // 学号 - Password string `json:"password"` // 密码 - LoginData LoginData `json:"login_data"` // 登录凭证 - client *resty.Client // Request对象 + ID string `json:"id"` // 学号 + Password string `json:"password"` // 密码 + cookies []*http.Cookie //cookies中将包含session_id和其他数据 + //如果我们使用client进行登陆的话,此时该字段失效,因为client会在登录时自动保存登陆凭证(session) + //所以该字段用于其他服务调用时传递登陆凭证 + Identifier string //位于url上id=....的一个标识符,主要用于组成url + client *resty.Client // Request对象 } // 学生信息详情 diff --git a/room.go b/room.go index 1f2843c..6ca62df 100644 --- a/room.go +++ b/room.go @@ -89,7 +89,7 @@ func (s *Student) GetQiShanEmptyRoom(req EmptyRoomReq) ([]string, error) { // 获取VIEWSTATE和EVENTVALIDATION func (s *Student) getEmptyRoomState() (map[string]string, error) { - resp, err := s.GetWithSession(constants.ClassroomQueryURL) + resp, err := s.GetWithIdentifier(constants.ClassroomQueryURL) if err != nil { return nil, err } diff --git a/user.go b/user.go index 44b8faf..f5519b9 100644 --- a/user.go +++ b/user.go @@ -72,7 +72,6 @@ func (s *Student) Login() error { return errno.LoginCheckFailedError } - // 获取session的id和num id := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error())[1] num := regexp.MustCompile(`num=(.*?)&`).FindStringSubmatch(err.Error())[1] @@ -98,7 +97,7 @@ func (s *Student) Login() error { return errno.SSOLoginFailedError } - // 获取session + // 获取cookies resp, err = s.NewRequest().SetHeaders(map[string]string{ "Referer": "https://jwcjwxt1.fzu.edu.cn/", "Origin": "https://jwcjwxt2.fzu.edu.cn/", @@ -111,20 +110,20 @@ func (s *Student) Login() error { }).Get("https://jwcjwxt2.fzu.edu.cn:81/loginchk_xs.aspx") // 保存这部分Cookie,这部分Cookie是用来后续鉴权的[ASP.NET_SessionId] - s.AppendCookies(resp.RawResponse.Cookies()) + s.SetCookies(resp.RawResponse.Cookies()) // 这里是err == nil 因为禁止了重定向,正常登录是会出现异常的 if err == nil { return errno.GetSessionFailedError } - session := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error()) + data := regexp.MustCompile(`id=(.*?)&`).FindStringSubmatch(err.Error()) - if len(session) < 1 { + if len(data) < 1 { return errno.GetSessionFailedError } - s.WithSession(session[1]) + s.SetIdentifier(data[1]) return nil } @@ -135,7 +134,7 @@ func (s *Student) CheckSession() error { // 旧版处理过程: 查询Body中是否含有[当前用户]这四个字 // 检查过期 - resp, err := s.GetWithSession(constants.UserInfoURL) + resp, err := s.GetWithIdentifier(constants.UserInfoURL) if err != nil { return err } @@ -156,7 +155,7 @@ func (s *Student) CheckSession() error { // 获取学生个人信息 func (s *Student) GetInfo() (resp *StudentDetail, err error) { - res, err := s.GetWithSession(constants.UserInfoURL) + res, err := s.GetWithIdentifier(constants.UserInfoURL) if err != nil { return nil, err