package ckwk import ( "ckwk/pkg/log" "encoding/json" "fmt" "time" "go.uber.org/zap" ) type CourseKind string type RecordType string type StudyStatus int const ( Run CourseKind = "run" // 我的课程 Finish CourseKind = "finish" // 已结束 Sign CourseKind = "sign" // 报名中 RecordStudy RecordType = "" // 学习记录 RecordWork RecordType = "/work" // 作业记录 RecordExam RecordType = "/exam" // 考试记录 RecordDiscuss RecordType = "/discuss" // 讨论记录 StudyStart StudyStatus = 1 // 开始学习 Study StudyStatus = 2 // 学习中 StudyOver StudyStatus = 3 // 学习结束 ) // User: 用户 type User struct { Name string `json:"name"` ID string `json:"id"` Dept string `json:"dept"` Class string `json:"class"` Gender string `json:"gender"` } // Course: 课程 type Course struct { Name string `json:"name"` ID int `json:"id"` Teacher string `json:"teacher"` Progress string `json:"progress"` StartTime string `json:"start_time"` StopTime string `json:"stop_time"` Credit float32 `json:"credit"` Type string `json:"type"` } // GetRecords 分页获取记录(迭代实现) func GetRecords[T any](wk *WK, rType RecordType, courseID, page string) (*AllRecordResp[T], error) { log.Debug("获取记录信息", zap.String("host", wk.Host), zap.String("type", string(rType))) result, err := fetchRecordPage[T](wk, rType, courseID, page) if err != nil { return nil, err } if !result.Status { return &result, fmt.Errorf("接口报错: %s", result.Msg) } for result.PageInfo.Page < result.PageInfo.PageCount { nextPage := fmt.Sprint(result.PageInfo.Page + 1) nextResult, err := fetchRecordPage[T](wk, rType, courseID, nextPage) if err != nil { return nil, err } result.List = append(result.List, nextResult.List...) log.Debug("Page", zap.Int("currentPage", result.PageInfo.Page), zap.Int("nextPage", nextResult.PageInfo.Page)) result.PageInfo = nextResult.PageInfo } return &result, nil } // fetchRecordPage 获取单页记录 func fetchRecordPage[T any](wk *WK, rType RecordType, courseID, page string) (AllRecordResp[T], error) { var result AllRecordResp[T] resp, err := wk.newRequest(). SetQueryParams(map[string]string{ "courseId": courseID, "page": page, "_": fmt.Sprint(time.Now().UnixMilli()), }). Get(fmt.Sprintf("https://%s/user/study_record%s.json", wk.Host, rType)) if err != nil { return result, err } if err := json.Unmarshal(resp.Bytes(), &result); err != nil { log.Error("JSON解析失败", zap.Error(err)) return result, err } return result, nil }