Files
wk-backend/internal/ckwk/types.go
zhilv 2a6732ffe7 fix: 修复6个bug并接入CodeStable工作流
Bug修复:
- GetWorkList 使用了错误的 RecordType (RecordStudy→RecordWork)
- AllRecord handler 返回错误的分页信息 (page硬编码1, pageSize用RecordsCount)
- CourseParse creditNode nil panic (加nil检查)
- WebSocket CheckOrigin 安全漏洞 (release模式限制为同源)
- math/rand 可预测 (替换为 crypto/rand)
- GetDiscussList 未实现 (补全实现, 移除重复路由)

其他:
- 接入 CodeStable 工作流体系 (codestable/ 骨架 + AGENTS.md)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-25 19:55:57 +08:00

94 lines
2.3 KiB
Go

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"`
}
type QAList struct {
}
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)))
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 nil, err
}
var result AllRecordResp[T]
if err := json.Unmarshal(resp.Bytes(), &result); err != nil {
log.Error("JSON解析失败", zap.Error(err))
return nil, err
}
if !result.Status {
return &result, fmt.Errorf("接口报错: %s", result.Msg)
}
currentPage := result.PageInfo.Page
totalPages := result.PageInfo.PageCount
for currentPage < totalPages {
nextPage := fmt.Sprint(currentPage + 1)
nextResult, err := GetRecords[T](wk, rType, courseID, nextPage)
if err != nil {
return nil, err
}
result.List = append(result.List, nextResult.List...)
log.Debug("Page", zap.Int("currentPage", currentPage), zap.Int("Page", nextResult.PageInfo.Page))
currentPage = nextResult.PageInfo.Page
}
return &result, nil
}