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

This reverts commit 1a3bd1a6b3.
This commit is contained in:
2026-02-21 12:50:53 +08:00
parent b84e6449dc
commit 32dae8712c
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"
type UserController struct {
BaseController
}
func (con UserController) Index(c *gin.Context) {
c.String(200, "管理员用户列表")
con.Success(c)
}
func (con UserController) Show(c *gin.Context) {

View File

@@ -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, "文章详情")

View File

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