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

add pageNum as value returned #21

Merged
merged 1 commit into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jwch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,12 @@ func TestGetExamRoomInfo(t *testing.T) {
}

func TestGetNoticesInfo(t *testing.T) {
content, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2})
content, totalPages, err := stu.GetNoticeInfo(&NoticeInfoReq{PageNum: 2})
fmt.Println(totalPages)
if err != nil {
t.Error(err)
}
if content == nil {
if content == nil || totalPages == 0 {
t.Error("content is nil")
}
}
Expand Down
32 changes: 16 additions & 16 deletions notice.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,42 @@ import (
"github.com/west2-online/jwch/constants"
)

func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, err error) {
func (s *Student) GetNoticeInfo(req *NoticeInfoReq) (list []*NoticeInfo, totalPages int, err error) {
// 获取通知公告页面的总页数
res, err := s.PostWithIdentifier(constants.NoticeInfoQueryURL, map[string]string{})
if err != nil {
return nil, err
return nil, 0, err
}
// 首页直接爬取
if req.PageNum == 1 {
list, err = parseNoticeInfo(res)
if err != nil {
return nil, err
}
return list, nil
}
// 分页需要根据页数计算 url
// 获取总页数
lastPageNum, err := getTotalPages(res)
if err != nil {
return nil, err
return nil, 0, err
}
// 判断是否超出总页数
if req.PageNum > lastPageNum {
return nil, fmt.Errorf("超出总页数")
return nil, lastPageNum, fmt.Errorf("超出总页数")
}
// 首页直接爬取
if req.PageNum == 1 {
list, err = parseNoticeInfo(res)
if err != nil {
return nil, lastPageNum, err
}
return list, lastPageNum, nil
}
// 根据总页数计算 url
num := lastPageNum - req.PageNum + 1
url := fmt.Sprintf("https://jwch.fzu.edu.cn/jxtz/%d.htm", num)
doc, err := s.PostWithIdentifier(url, map[string]string{})
if err != nil {
return nil, err
return nil, lastPageNum, err
}
list, err = parseNoticeInfo(doc)
if err != nil {
return nil, err
return nil, lastPageNum, err
}
// 3. 返回结果
return list, nil
return list, lastPageNum, nil
}

// 获取当前页面的所有数据信息
Expand Down
Loading