AnCao/main.go
yanlongqi 3ecc1c6a18 添加用户管理功能
新增功能:
- 用户管理页面:展示所有用户及答题统计
- 用户详情页面:查看单个用户的详细答题数据
- 管理员权限中间件:仅yanlongqi用户可访问
- 后端API接口:用户列表和详情统计

后端更新:
- 新增 admin_handler.go:用户管理相关处理器
- 新增 admin.go 中间件:管理员权限验证
- 新增 user_stats.go 模型:用户统计数据结构
- 更新 main.go:注册用户管理API路由

前端更新:
- 新增 UserManagement 页面:用户列表和统计卡片
- 新增 UserDetail 页面:用户详细信息和题型统计
- 更新 Home 页面:添加用户管理入口(仅管理员可见)
- 更新 App.tsx:添加用户管理路由和权限保护
- 更新 API 接口:添加用户管理相关接口定义

UI优化:
- 用户管理页面标题居中显示,参考错题本设计
- 统计卡片使用16px padding
- 返回按钮使用绝对定位,标题居中
- 字体大小统一为18px,字重700

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-08 06:21:15 +08:00

103 lines
4.0 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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) // 更新用户类型
auth.PUT("/user/profile", handlers.UpdateProfile) // 更新用户信息
auth.PUT("/user/password", handlers.ChangePassword) // 修改密码
// 练习题相关API需要登录
auth.GET("/practice/questions", handlers.GetPracticeQuestions) // 获取练习题目列表
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/recommended", handlers.GetRecommendedWrongQuestions) // 获取推荐错题
auth.GET("/wrong-questions/:id", handlers.GetWrongQuestionDetail) // 获取错题详情
auth.DELETE("/wrong-questions/:id", handlers.DeleteWrongQuestion) // 删除错题
auth.DELETE("/wrong-questions", handlers.ClearWrongQuestions) // 清空错题本
auth.PUT("/wrong-questions/:id/tags", handlers.UpdateWrongQuestionTags) // 更新错题标签
// 标签管理API
auth.GET("/wrong-question-tags", handlers.GetWrongQuestionTags) // 获取标签列表
auth.POST("/wrong-question-tags", handlers.CreateWrongQuestionTag) // 创建标签
auth.PUT("/wrong-question-tags/:id", handlers.UpdateWrongQuestionTag) // 更新标签
auth.DELETE("/wrong-question-tags/:id", handlers.DeleteWrongQuestionTag) // 删除标签
}
// 题库管理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仅yanlongqi用户可访问
userAdmin := api.Group("", middleware.Auth(), middleware.AdminOnly())
{
userAdmin.GET("/admin/users", handlers.GetAllUsersWithStats) // 获取所有用户及统计
userAdmin.GET("/admin/users/:id", handlers.GetUserDetailStats) // 获取用户详细统计
}
}
// 静态文件服务(必须在 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())
}
}