本次更新实现了基于用户类型的论述题访问权限控制,并为论述题添加了专门的AI评分功能。 后端更新: - 添加论述题权限验证:根据用户类型(ordinary-person/management-person)控制不同论述题的访问权限 - 新增 GradeEssay 方法:为论述题提供专门的AI评分,不依赖标准答案,基于保密法规进行专业评分 - 优化AI评分提示词:增加法规依据要求,返回参考答案、评分依据等更详细的评分信息 - 添加用户类型管理:新增 UpdateUserType API,支持用户更新个人类型 - 路由调整:将练习题相关API移至需要认证的路由组 前端更新: - 论述题答题界面优化:不显示标准答案,展示AI评分的参考答案和评分依据 - 用户类型选择:登录/注册时支持选择用户类型 - 权限控制适配:根据用户类型显示对应的论述题列表 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
88 lines
3.1 KiB
Go
88 lines
3.1 KiB
Go
package main
|
||
|
||
import (
|
||
"ankao/internal/database"
|
||
"ankao/internal/handlers"
|
||
"ankao/internal/middleware"
|
||
"log"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
)
|
||
|
||
func main() {
|
||
// 初始化数据库连接
|
||
if err := database.InitDB(); err != nil {
|
||
log.Fatal("数据库初始化失败:", err)
|
||
}
|
||
log.Println("数据库连接成功")
|
||
|
||
// 创建Gin路由器
|
||
r := gin.Default()
|
||
|
||
// 应用自定义中间件
|
||
r.Use(middleware.CORS())
|
||
r.Use(middleware.Logger())
|
||
|
||
// API路由组(必须在静态文件服务之前注册)
|
||
api := r.Group("/api")
|
||
{
|
||
// 健康检查
|
||
api.GET("/health", handlers.HealthCheckHandler)
|
||
|
||
// 用户相关API
|
||
api.POST("/login", handlers.Login) // 用户登录
|
||
api.POST("/register", handlers.Register) // 用户注册
|
||
|
||
// 公开的练习题相关API
|
||
api.GET("/practice/types", handlers.GetPracticeQuestionTypes) // 获取题型列表
|
||
|
||
// 需要认证的路由
|
||
auth := api.Group("", middleware.Auth())
|
||
{
|
||
// 用户相关API
|
||
auth.PUT("/user/type", handlers.UpdateUserType) // 更新用户类型
|
||
|
||
// 练习题相关API(需要登录)
|
||
auth.GET("/practice/questions", handlers.GetPracticeQuestions) // 获取练习题目列表
|
||
auth.GET("/practice/questions/random", handlers.GetRandomPracticeQuestion) // 获取随机练习题目
|
||
auth.GET("/practice/questions/:id", handlers.GetPracticeQuestionByID) // 获取指定练习题目
|
||
auth.POST("/practice/explain", handlers.ExplainQuestion) // 生成题目解析(AI)
|
||
|
||
// 练习题提交(需要登录才能记录错题)
|
||
auth.POST("/practice/submit", handlers.SubmitPracticeAnswer) // 提交练习答案
|
||
auth.GET("/practice/statistics", handlers.GetStatistics) // 获取统计数据
|
||
|
||
// 错题本相关API
|
||
auth.GET("/wrong-questions", handlers.GetWrongQuestions) // 获取错题列表
|
||
auth.GET("/wrong-questions/stats", handlers.GetWrongQuestionStats) // 获取错题统计
|
||
auth.GET("/wrong-questions/random", handlers.GetRandomWrongQuestion) // 获取随机错题
|
||
auth.DELETE("/wrong-questions/:id", handlers.DeleteWrongQuestion) // 删除单个错题
|
||
auth.PUT("/wrong-questions/:id/mastered", handlers.MarkWrongQuestionMastered) // 标记已掌握
|
||
auth.DELETE("/wrong-questions", handlers.ClearWrongQuestions) // 清空错题本
|
||
}
|
||
|
||
// 题库管理API(需要管理员权限)
|
||
admin := api.Group("", middleware.Auth(), middleware.AdminAuth())
|
||
{
|
||
admin.POST("/practice/questions", handlers.CreatePracticeQuestion) // 创建题目
|
||
admin.PUT("/practice/questions/:id", handlers.UpdatePracticeQuestion) // 更新题目
|
||
admin.DELETE("/practice/questions/:id", handlers.DeletePracticeQuestion) // 删除题目
|
||
}
|
||
}
|
||
|
||
// 静态文件服务(必须在 API 路由之后)
|
||
// 提供静态资源(CSS、JS、图片等)
|
||
r.Static("/assets", "./web/static")
|
||
|
||
// SPA 支持:所有非 API 路由都返回 index.html,让前端路由处理
|
||
r.NoRoute(func(c *gin.Context) {
|
||
c.File("./web/index.html")
|
||
})
|
||
|
||
// 启动服务器
|
||
port := ":8080"
|
||
if err := r.Run(port); err != nil {
|
||
panic("服务器启动失败: " + err.Error())
|
||
}
|
||
}
|