后台独立登录页面,并加入权限控制。

This commit is contained in:
2026-08-11 18:04:02 +08:00
parent 808f2e7523
commit a1d57d6b44
12 changed files with 638 additions and 178 deletions
+30 -15
View File
@@ -17,16 +17,19 @@ func SetupRoutes(r *gin.Engine, cfg interface{}) {
// 后台管理(放在前面避免冲突)
r.Static("/admin/static", "./static/admin")
r.GET("/admin", func(c *gin.Context) {
c.File("./static/admin/index.html")
})
// 独立登录页
r.GET("/admin/login", handlers.AdminLoginView)
// 后台首页:服务端按登录角色用模板渲染菜单与页面框架
r.GET("/admin", handlers.AdminView)
// 前端页面路由
r.GET("/", handlers.IndexView)
r.GET("/post/:slug", handlers.PostView)
r.GET("/page/:slug", handlers.PageView)
r.GET("/search", handlers.SearchView)
// 前端页面路由(可选认证:侧边栏根据登录态显示系统菜单)
r.GET("/", middleware.OptionalAuth(), handlers.IndexView)
r.GET("/post/:slug", middleware.OptionalAuth(), handlers.PostView)
r.GET("/page/:slug", middleware.OptionalAuth(), handlers.PageView)
r.GET("/search", middleware.OptionalAuth(), handlers.SearchView)
r.POST("/comment", handlers.SubmitComment)
// 前台退出登录:清除 Cookie 后回首页
r.GET("/logout", handlers.WebLogout)
// API 路由组
api := r.Group("/api")
@@ -47,6 +50,7 @@ func SetupRoutes(r *gin.Engine, cfg interface{}) {
// 用户认证
api.POST("/auth/login", handlers.Login)
api.POST("/auth/logout", handlers.Logout)
// 需要登录的 API
auth := api.Group("/")
@@ -55,6 +59,23 @@ func SetupRoutes(r *gin.Engine, cfg interface{}) {
auth.GET("/auth/me", handlers.GetCurrentUser)
auth.PUT("/auth/me", handlers.UpdateUser)
auth.PUT("/auth/password", handlers.ChangePassword)
// 文章管理(所有登录用户可新建文章,修改/删除自己的文章)
auth.POST("/posts", handlers.CreatePost)
auth.PUT("/posts/:id", handlers.UpdatePost)
auth.DELETE("/posts/:id", handlers.DeletePost)
// Markdown 预览(文章编辑器用)
auth.POST("/markdown/preview", handlers.PreviewMarkdown)
// 文件上传(文章编辑器封面图等)
auth.POST("/upload", handlers.UploadImage)
// 评论管理(登录用户管理自己文章的评论)
auth.GET("/my/comments", handlers.GetComments)
auth.PUT("/my/comments/:id/approve", handlers.ApproveComment)
auth.PUT("/my/comments/:id/spam", handlers.MarkSpamComment)
auth.DELETE("/my/comments/:id", handlers.DeleteComment)
}
// 主题管理(可选认证)
@@ -70,13 +91,7 @@ func SetupRoutes(r *gin.Engine, cfg interface{}) {
admin := api.Group("/admin")
admin.Use(middleware.JWTAuth(), middleware.AdminRequired())
{
// 文章管理
admin.POST("/posts", handlers.CreatePost)
admin.PUT("/posts/:id", handlers.UpdatePost)
admin.DELETE("/posts/:id", handlers.DeletePost)
// Markdown 预览(文章编辑器用)
admin.POST("/markdown/preview", handlers.PreviewMarkdown)
// Markdown 预览(文章编辑器用)已移至登录组
// 分类管理
admin.POST("/categories", handlers.CreateCategory)