From 4062d16fa8e1c92b74dbcea6948b9309dcafc336 Mon Sep 17 00:00:00 2001 From: nannanwu Date: Wed, 25 Mar 2026 18:49:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(admin):=20=E9=87=8D=E6=9E=84=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=91=98=E6=96=87=E7=AB=A0=E8=B7=AF=E7=94=B1=E5=B9=B6?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=AF=BC=E8=88=AA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将管理员文章路由从 Article 方法改为 Index 方法 - 在 ArticleController 中实现 Index 方法,支持预加载文章分类数据并返回 JSON - 添加 ArticleCate 模型及对应数据表映射 - 扩展 Article 模型,关联 ArticleCate,实现文章与分类的关联查询 - 新增 Nav 模型及 NavController,提供导航数据接口 - 修改用户模型添加 JSON 标签,规范字段序列化 - 扩展数据库自动迁移,新增 Article、Nav 和 ArticleCate 表的自动创建功能 --- controller/admin/articleController.go | 13 ++++++++++++- controller/admin/navController.go | 17 +++++++++++++++++ model/article.go | 9 +++++---- model/article_cate.go | 11 +++++++++++ model/core.go | 4 ++-- model/nav.go | 13 +++++++++++++ model/user.go | 12 ++++++------ router/adminRouter.go | 2 +- 8 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 controller/admin/navController.go create mode 100644 model/article_cate.go create mode 100644 model/nav.go diff --git a/controller/admin/articleController.go b/controller/admin/articleController.go index 86ffc50..57131d4 100644 --- a/controller/admin/articleController.go +++ b/controller/admin/articleController.go @@ -1,10 +1,21 @@ package admin -import "github.com/gin-gonic/gin" +import ( + "awesomeProject/model" + + "github.com/gin-gonic/gin" +) type ArticleController struct { } +func (con ArticleController) Index(c *gin.Context) { + articleList := []model.Article{} + model.DB.Preload("ArticleCate").Find(&articleList) + c.JSON(200, articleList) + +} + func (con ArticleController) Article(c *gin.Context) { c.String(200, "管理员文章列表") } diff --git a/controller/admin/navController.go b/controller/admin/navController.go new file mode 100644 index 0000000..bd09d04 --- /dev/null +++ b/controller/admin/navController.go @@ -0,0 +1,17 @@ +package admin + +import ( + "awesomeProject/model" + + "github.com/gin-gonic/gin" +) + +type NavController struct { + BaseController +} + +func (con NavController) Index(c *gin.Context) { + navList := []model.Nav{} + model.DB.Find(&navList) + c.JSON(200, navList) +} diff --git a/model/article.go b/model/article.go index 6b03fe9..bb2739d 100644 --- a/model/article.go +++ b/model/article.go @@ -1,10 +1,11 @@ package model type Article struct { - Id int - Title string - CateId int - State int + Id int + Title string + CateId int + State int + ArticleCate ArticleCate `gorm:"foreignKey:CateId;references:State"` } func (Article) TableName() string { diff --git a/model/article_cate.go b/model/article_cate.go new file mode 100644 index 0000000..5f3b5da --- /dev/null +++ b/model/article_cate.go @@ -0,0 +1,11 @@ +package model + +type ArticleCate struct { + Id int + Title string + State int +} + +func (ArticleCate) TableName() string { + return "article_cate" +} diff --git a/model/core.go b/model/core.go index 2a3375f..2ef15b4 100644 --- a/model/core.go +++ b/model/core.go @@ -20,8 +20,8 @@ func init() { return } - // 自动创建数据表 - err = DB.AutoMigrate(&User{}) + // 自动创建所有不存在的表 + err = DB.AutoMigrate(&User{}, &Article{}, &Nav{}, &ArticleCate{}) if err != nil { fmt.Println("数据表迁移失败:", err) } else { diff --git a/model/nav.go b/model/nav.go new file mode 100644 index 0000000..2df688c --- /dev/null +++ b/model/nav.go @@ -0,0 +1,13 @@ +package model + +type Nav struct { + Id int `json:"id"` + Title string `json:"title"` + Url string `json:"url"` + Status int `json:"status"` + Sort int `json:"sort"` +} + +func (Nav) TableName() string { + return "nav" +} diff --git a/model/user.go b/model/user.go index 29bf3f0..6a9d82a 100644 --- a/model/user.go +++ b/model/user.go @@ -1,12 +1,12 @@ package model type User struct { - Id int - Name string - Age int - Email string - Password string - AddTime int + Id int `json:"id"` + Name string `json:"name"` + Age int `json:"age"` + Email string `json:"email"` + Password string `json:"password"` + AddTime int `json:"add_time"` } func (User) TableName() string { diff --git a/router/adminRouter.go b/router/adminRouter.go index af6f252..5e27324 100644 --- a/router/adminRouter.go +++ b/router/adminRouter.go @@ -22,7 +22,7 @@ func AdminRouterInit(r *gin.Engine) { adminRouter.GET("/login", admin.IndexController{}.Login) - adminRouter.GET("/article", admin.ArticleController{}.Article) + adminRouter.GET("/article", admin.ArticleController{}.Index) adminRouter.GET("/user", admin.UserController{}.Index)