From 37c78d9a4eb4c3e17780f11edace5a6a0019f7c1 Mon Sep 17 00:00:00 2001 From: nannanwu Date: Fri, 20 Feb 2026 14:22:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(controller):=20=E5=AE=8C=E5=96=84=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E6=8E=A7=E5=88=B6=E5=99=A8=E5=8F=8A=E9=BB=98=E8=AE=A4?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E6=B8=B2=E6=9F=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增BaseController,包含Success和Error通用响应方法 - 修改DefaultController Index方法,改为返回HTML页面 - 更新index.html模板,优化模板语法和结构,增加条件判断和循环示例 - UserController嵌入BaseController,Index方法统一调用Success响应 - 修正部分注释及代码格式提升可读性 --- controller/admin/baseController.go | 13 +++++++ controller/admin/userController.go | 3 +- controller/nannanwu/defaultController.go | 2 +- templates/pages/index.html | 48 +++++++++--------------- 4 files changed, 34 insertions(+), 32 deletions(-) create mode 100644 controller/admin/baseController.go diff --git a/controller/admin/baseController.go b/controller/admin/baseController.go new file mode 100644 index 0000000..bcf53ee --- /dev/null +++ b/controller/admin/baseController.go @@ -0,0 +1,13 @@ +package admin + +import "github.com/gin-gonic/gin" + +type BaseController struct { +} + +func (con BaseController) Success(c *gin.Context) { + c.String(200, "成功") +} +func (con BaseController) Error(c *gin.Context) { + c.String(200, "失败") +} diff --git a/controller/admin/userController.go b/controller/admin/userController.go index 49f35b1..5115312 100644 --- a/controller/admin/userController.go +++ b/controller/admin/userController.go @@ -3,10 +3,11 @@ package admin import "github.com/gin-gonic/gin" type UserController struct { + BaseController } func (con UserController) Index(c *gin.Context) { - c.String(200, "管理员用户列表") + con.Success(c) } func (con UserController) Show(c *gin.Context) { diff --git a/controller/nannanwu/defaultController.go b/controller/nannanwu/defaultController.go index 36b182e..aacbaf6 100644 --- a/controller/nannanwu/defaultController.go +++ b/controller/nannanwu/defaultController.go @@ -8,7 +8,7 @@ type DefaultController struct { } func (con DefaultController) Index(c *gin.Context) { - c.String(200, "首页") + c.HTML(200, "pages/index", gin.H{}) } func (con DefaultController) Article(c *gin.Context) { c.String(200, "文章详情") diff --git a/templates/pages/index.html b/templates/pages/index.html index 224199c..d0ab3e1 100644 --- a/templates/pages/index.html +++ b/templates/pages/index.html @@ -1,4 +1,3 @@ -{{/* 定义index页面模板 */}} {{define "pages/index"}} @@ -6,63 +5,52 @@ {{.title}} - - - {{template "layout/navbar"}} - {{/* 基本数据输出示例 - 直接渲染标题变量 */}} +

{{.title}}

- {{/* 模板变量定义示例 - 将标题赋值给局部变量t */}} {{$t := .title}}

{{$t}}

- {{/* 条件判断示例 - 根据score值判断是否及格 */}} - {{if ge .score 60}} -

及格

+ {{if ge .score 60}} +

及格

{{else}} -

+

不及格

{{end}} - {{/* 简单数组循环遍历示例 */}} - {{range $key, $value := .hobby}} - + {{range $key, $value := .hobby}} + {{end}} - {{/* 结构体数组循环遍历示例 */}} - {{range $key, $value := .newsList}} - + {{range $key, $value := .newsList}} + {{end}} - {{/* 空数组处理示例 - 当newsList2为空时显示提示信息 */}} {{range $key, $value := .newsList2}} {{else}} -
  • 没有数据
  • +
  • 没有数据
  • {{end}} - {{/* 结构体数据访问示例 - 使用with语句简化结构体属性访问 */}} - {{with .news}} - {{.Title}} - {{.Content}} + {{with .news}} + {{.Title}} + {{.Content}} {{end}}
    - {{/* 自定义模板函数使用示例 */}} - {{.data}} + {{.data}} - - {{ template "layout/footer"}} + {{template "layout/footer"}} {{end}} \ No newline at end of file