feat(controller): 完善基础控制器及默认页面渲染

- 新增BaseController,包含Success和Error通用响应方法
- 修改DefaultController Index方法,改为返回HTML页面
- 更新index.html模板,优化模板语法和结构,增加条件判断和循环示例
- UserController嵌入BaseController,Index方法统一调用Success响应
- 修正部分注释及代码格式提升可读性
This commit is contained in:
2026-02-20 14:22:47 +08:00
parent ee2e890c39
commit 37c78d9a4e
4 changed files with 34 additions and 32 deletions

View File

@@ -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, "失败")
}

View File

@@ -3,10 +3,11 @@ package admin
import "github.com/gin-gonic/gin" import "github.com/gin-gonic/gin"
type UserController struct { type UserController struct {
BaseController
} }
func (con UserController) Index(c *gin.Context) { func (con UserController) Index(c *gin.Context) {
c.String(200, "管理员用户列表") con.Success(c)
} }
func (con UserController) Show(c *gin.Context) { func (con UserController) Show(c *gin.Context) {

View File

@@ -8,7 +8,7 @@ type DefaultController struct {
} }
func (con DefaultController) Index(c *gin.Context) { func (con DefaultController) Index(c *gin.Context) {
c.String(200, "首页") c.HTML(200, "pages/index", gin.H{})
} }
func (con DefaultController) Article(c *gin.Context) { func (con DefaultController) Article(c *gin.Context) {
c.String(200, "文章详情") c.String(200, "文章详情")

View File

@@ -1,4 +1,3 @@
{{/* 定义index页面模板 */}}
{{define "pages/index"}} {{define "pages/index"}}
<!doctype html> <!doctype html>
<html lang="zh"> <html lang="zh">
@@ -6,63 +5,52 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<title>{{.title}}</title> <title>{{.title}}</title>
<!-- 组件特定样式块 -->
<link rel="stylesheet" href="/static/navbar.css"> <link rel="stylesheet" href="/static/navbar.css">
<link rel="stylesheet" href="/static/footer.css"> <link rel="stylesheet" href="/static/footer.css">
</head> </head>
<body> <body>
{{template "layout/navbar"}} {{template "layout/navbar"}}
{{/* 基本数据输出示例 - 直接渲染标题变量 */}}
<h2>{{.title}}</h2> <h2>{{.title}}</h2>
{{/* 模板变量定义示例 - 将标题赋值给局部变量t */}}
{{$t := .title}} {{$t := .title}}
<h4>{{$t}}</h4> <h4>{{$t}}</h4>
{{/* 条件判断示例 - 根据score值判断是否及格 */}} {{if ge .score 60}}
{{if ge .score 60}} <!-- ge: 大于等于 --> <p>及格</p>
<p>及格</p>
{{else}} {{else}}
<p></p> <p>不及格</p>
{{end}} {{end}}
{{/* 简单数组循环遍历示例 */}} {{range $key, $value := .hobby}}
{{range $key, $value := .hobby}} <!-- 遍历爱好列表 --> <ul>
<ul> <li>{{$key}}--{{$value}}</li>
<li>{{$key}}--{{$value}}</li> <!-- 输出索引和值 --> </ul>
</ul>
{{end}} {{end}}
{{/* 结构体数组循环遍历示例 */}} {{range $key, $value := .newsList}}
{{range $key, $value := .newsList}} <!-- 遍历文章列表 --> <ul>
<ul> <li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li> <!-- 输出索引和文章属性 --> </ul>
</ul>
{{end}} {{end}}
{{/* 空数组处理示例 - 当newsList2为空时显示提示信息 */}}
{{range $key, $value := .newsList2}} {{range $key, $value := .newsList2}}
<ul> <ul>
<li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li> <li>{{$key}}--{{$value.Title}}--{{$value.Content}}</li>
</ul> </ul>
{{else}} {{else}}
<li>没有数据</li> <!-- 当数组为空时执行 --> <li>没有数据</li>
{{end}} {{end}}
{{/* 结构体数据访问示例 - 使用with语句简化结构体属性访问 */}} {{with .news}}
{{with .news}} <!-- 针对news结构体设置上下文 --> {{.Title}}
{{.Title}} <!-- 直接访问Title属性等同于.news.Title --> {{.Content}}
{{.Content}} <!-- 直接访问Content属性等同于.news.Content -->
{{end}} {{end}}
<br> <br>
{{/* 自定义模板函数使用示例 */}} {{.data}}
{{.data}} <!-- 输出原始时间戳 -->
{{template "layout/footer"}}
{{ template "layout/footer"}}
</body> </body>
</html> </html>
{{end}} {{end}}