**统计功能**: - 新增UserAnswerRecord模型记录用户答题历史 - 实现GetStatistics接口,统计题库总数、已答题数、正确率 - 在提交答案时自动记录答题历史 - 前端连接真实统计接口,显示实时数据 **认证系统优化**: - 新增Auth中间件,实现基于Token的身份验证 - 登录和注册时自动生成并保存Token到数据库 - 所有需要登录的接口都通过Auth中间件保护 - 统一处理未授权请求,返回401状态码 **错题练习功能**: - 新增GetRandomWrongQuestion接口,随机获取错题 - 支持错题练习模式(/question?mode=wrong) - 优化错题本页面UI,移除已掌握功能 - 新增"开始错题练习"按钮,直接进入练习模式 **数据库迁移**: - 新增user_answer_records表,记录用户答题历史 - User表新增token字段,存储用户登录凭证 **技术改进**: - 统一错误处理,区分401未授权和404未找到 - 优化答题流程,记录历史和错题分离处理 - 移除异步记录错题,改为同步处理保证数据一致性 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
30 lines
907 B
Go
30 lines
907 B
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// UserAnswerRecord 用户答题记录
|
|
type UserAnswerRecord struct {
|
|
gorm.Model
|
|
UserID uint `gorm:"index;not null" json:"user_id"` // 用户ID
|
|
QuestionID uint `gorm:"index;not null" json:"question_id"` // 题目ID
|
|
IsCorrect bool `gorm:"not null" json:"is_correct"` // 是否答对
|
|
AnsweredAt time.Time `gorm:"not null" json:"answered_at"` // 答题时间
|
|
}
|
|
|
|
// TableName 指定表名
|
|
func (UserAnswerRecord) TableName() string {
|
|
return "user_answer_records"
|
|
}
|
|
|
|
// UserStatistics 用户统计数据
|
|
type UserStatistics struct {
|
|
TotalQuestions int `json:"total_questions"` // 题库总数
|
|
AnsweredQuestions int `json:"answered_questions"` // 已答题数
|
|
CorrectAnswers int `json:"correct_answers"` // 答对题数
|
|
Accuracy float64 `json:"accuracy"` // 正确率
|
|
}
|