Compare commits
10 Commits
ee2e890c39
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 4062d16fa8 | |||
| 4a3c510a00 | |||
| 4229a59d89 | |||
| a81fbb724e | |||
| ae36d59b68 | |||
| b7b0c32e82 | |||
| 32dae8712c | |||
| b84e6449dc | |||
| 1a3bd1a6b3 | |||
| 37c78d9a4e |
@@ -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, "管理员文章列表")
|
||||
}
|
||||
|
||||
13
controller/admin/baseController.go
Normal file
13
controller/admin/baseController.go
Normal 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, "失败")
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
package admin
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type IndexController struct {
|
||||
}
|
||||
|
||||
func (con IndexController) Index(c *gin.Context) {
|
||||
c.String(200, "管理员用户列表")
|
||||
username, _ := c.Get("user")
|
||||
fmt.Println(username)
|
||||
c.String(200, "管理员用户列表 %v", username)
|
||||
}
|
||||
|
||||
func (con IndexController) Plist(c *gin.Context) {
|
||||
|
||||
17
controller/admin/navController.go
Normal file
17
controller/admin/navController.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -1,14 +1,121 @@
|
||||
package admin
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"awesomeProject/model"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type UserController struct {
|
||||
BaseController
|
||||
}
|
||||
|
||||
func (con UserController) Index(c *gin.Context) {
|
||||
c.String(200, "管理员用户列表")
|
||||
//查询数据库
|
||||
userList := []model.User{}
|
||||
|
||||
model.DB.Find(&userList)
|
||||
c.JSON(200, userList)
|
||||
//con.Success(c)
|
||||
}
|
||||
|
||||
func (con UserController) Add(c *gin.Context) {
|
||||
user := model.User{
|
||||
Name: "test",
|
||||
Age: 18,
|
||||
Email: "test@example.com",
|
||||
Password: "password",
|
||||
}
|
||||
model.DB.Create(&user)
|
||||
c.JSON(200, user)
|
||||
fmt.Println(user)
|
||||
c.String(200, "用户添加成功")
|
||||
}
|
||||
|
||||
func (con UserController) Edit(c *gin.Context) {
|
||||
user := model.User{Id: 1}
|
||||
model.DB.Find(&user)
|
||||
user.Age = 19
|
||||
user.Password = "6666"
|
||||
user.Name = "哈哈哈"
|
||||
model.DB.Save(&user)
|
||||
c.JSON(200, user)
|
||||
}
|
||||
|
||||
func (con UserController) Delete(c *gin.Context) {
|
||||
user := model.User{Id: 1}
|
||||
model.DB.Delete(&user)
|
||||
c.String(200, "用户删除成功")
|
||||
}
|
||||
|
||||
func (con UserController) Show(c *gin.Context) {
|
||||
c.String(200, "管理员用户详情")
|
||||
}
|
||||
|
||||
func (con UserController) DoUpload(c *gin.Context) {
|
||||
// 获取表单中的用户名
|
||||
username := c.PostForm("username")
|
||||
|
||||
// 获取上传的文件
|
||||
file, err := c.FormFile("face")
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "获取文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 创建上传目录
|
||||
uploadDir := "./upload"
|
||||
if err := os.MkdirAll(uploadDir, os.ModePerm); err != nil {
|
||||
c.JSON(500, gin.H{"error": "创建目录失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 构建目标文件路径并保存文件
|
||||
dst := filepath.Join(uploadDir, file.Filename)
|
||||
if err := c.SaveUploadedFile(file, dst); err != nil {
|
||||
c.JSON(500, gin.H{"error": "保存文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
// 返回成功响应
|
||||
c.JSON(200, gin.H{
|
||||
"success": true,
|
||||
"username": username,
|
||||
"dst": dst,
|
||||
})
|
||||
}
|
||||
|
||||
func (con UserController) DoEdit(c *gin.Context) {
|
||||
username := c.PostForm("username")
|
||||
file1, err := c.FormFile("face1")
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "获取文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
uploadDir := "./upload"
|
||||
dst := filepath.Join(uploadDir, file1.Filename)
|
||||
if err := c.SaveUploadedFile(file1, dst); err != nil {
|
||||
c.JSON(500, gin.H{"error": "保存文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
file2, err := c.FormFile("face2")
|
||||
if err != nil {
|
||||
c.JSON(400, gin.H{"error": "获取文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
dst1 := filepath.Join(uploadDir, file2.Filename)
|
||||
if err := c.SaveUploadedFile(file2, dst1); err != nil {
|
||||
c.JSON(500, gin.H{"error": "保存文件失败: " + err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, gin.H{
|
||||
"success": true,
|
||||
"username": username,
|
||||
"dst": dst,
|
||||
"dst1": dst1,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package nannanwu
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
import (
|
||||
"awesomeProject/model"
|
||||
"fmt"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type DefaultController struct {
|
||||
//继承BaseController
|
||||
@@ -8,13 +14,47 @@ type DefaultController struct {
|
||||
}
|
||||
|
||||
func (con DefaultController) Index(c *gin.Context) {
|
||||
c.String(200, "首页")
|
||||
//设置session
|
||||
session := sessions.Default(c)
|
||||
session.Set("username", "张三sessions")
|
||||
err := session.Save()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(model.UnixToTime(1629788418))
|
||||
//设置cookie
|
||||
c.SetCookie("username", "张三", 3600, "/", "", false, false)
|
||||
c.HTML(200, "pages/index", gin.H{
|
||||
"title": "首页",
|
||||
"t": 1629788418,
|
||||
})
|
||||
}
|
||||
func (con DefaultController) Article(c *gin.Context) {
|
||||
c.String(200, "文章详情")
|
||||
//获取session
|
||||
session := sessions.Default(c)
|
||||
username := session.Get("username")
|
||||
c.String(200, "session=%v", username)
|
||||
////获取cookie
|
||||
//username, err := c.Cookie("username")
|
||||
//if err != nil {
|
||||
// c.String(200, "获取cookie失败")
|
||||
// return
|
||||
//}
|
||||
//c.String(200, "cookie="+username)
|
||||
|
||||
}
|
||||
|
||||
// 使用继承的方式调用BaseController的success方法
|
||||
func (con DefaultController) DeleteCookie(c *gin.Context) {
|
||||
c.SetCookie("username", "张三", -1, "/", "", false, false)
|
||||
_, err := c.Cookie("username")
|
||||
if err != nil {
|
||||
c.String(200, "删除cookie成功")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Success 使用继承的方式调用BaseController的success方法
|
||||
func (con DefaultController) Success(c *gin.Context) {
|
||||
BaseController{}.success(c)
|
||||
}
|
||||
|
||||
19
go.mod
19
go.mod
@@ -2,9 +2,15 @@ module awesomeProject
|
||||
|
||||
go 1.25
|
||||
|
||||
require github.com/gin-gonic/gin v1.11.0
|
||||
require (
|
||||
github.com/gin-contrib/sessions v1.0.4
|
||||
github.com/gin-gonic/gin v1.11.0
|
||||
gorm.io/driver/mysql v1.6.0
|
||||
gorm.io/gorm v1.31.1
|
||||
)
|
||||
|
||||
require (
|
||||
filippo.io/edwards25519 v1.1.0 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.14.2 // indirect
|
||||
github.com/bytedance/sonic/loader v0.4.0 // indirect
|
||||
@@ -14,8 +20,14 @@ require (
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.28.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.8.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/gorilla/context v1.1.2 // indirect
|
||||
github.com/gorilla/securecookie v1.1.2 // indirect
|
||||
github.com/gorilla/sessions v1.4.0 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jinzhu/now v1.1.5 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
@@ -30,11 +42,8 @@ require (
|
||||
go.uber.org/mock v0.6.0 // indirect
|
||||
golang.org/x/arch v0.23.0 // indirect
|
||||
golang.org/x/crypto v0.45.0 // indirect
|
||||
golang.org/x/mod v0.30.0 // indirect
|
||||
golang.org/x/net v0.47.0 // indirect
|
||||
golang.org/x/sync v0.18.0 // indirect
|
||||
golang.org/x/sys v0.38.0 // indirect
|
||||
golang.org/x/text v0.31.0 // indirect
|
||||
golang.org/x/tools v0.39.0 // indirect
|
||||
golang.org/x/text v0.34.0 // indirect
|
||||
google.golang.org/protobuf v1.36.10 // indirect
|
||||
)
|
||||
|
||||
12
main.go
12
main.go
@@ -1,8 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"awesomeProject/model"
|
||||
"awesomeProject/router"
|
||||
"html/template"
|
||||
|
||||
"github.com/gin-contrib/sessions"
|
||||
"github.com/gin-contrib/sessions/cookie"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
@@ -11,12 +15,20 @@ func main() {
|
||||
// 创建默认的gin引擎
|
||||
r := gin.Default()
|
||||
|
||||
//自定义模板函数
|
||||
r.SetFuncMap(template.FuncMap{
|
||||
"UnixToTime": model.UnixToTime,
|
||||
})
|
||||
|
||||
// 加载模板文件,支持多层目录结构
|
||||
r.LoadHTMLGlob("templates/**/*")
|
||||
|
||||
// 配置静态文件目录,将./static目录映射到URL路径/static
|
||||
r.Static("/static", "./static")
|
||||
|
||||
//配置session中间件
|
||||
r.Use(sessions.Sessions("my1session", cookie.NewStore([]byte("secre111111"))))
|
||||
|
||||
router.AdminRouterInit(r)
|
||||
|
||||
router.ApiRouterInit(r)
|
||||
|
||||
24
middleware/init.go
Normal file
24
middleware/init.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func InitMiddleware(c *gin.Context) {
|
||||
//判断是否登录
|
||||
fmt.Println(time.Now())
|
||||
fmt.Println(c.Request.URL.Path)
|
||||
c.Set("user", "张三")
|
||||
|
||||
//定义goroutine统计
|
||||
cCp := c.Copy()
|
||||
go func() {
|
||||
time.Sleep(1 * time.Second)
|
||||
fmt.Println("统计信息" + cCp.Request.URL.Path)
|
||||
}()
|
||||
|
||||
c.Next()
|
||||
}
|
||||
13
model/article.go
Normal file
13
model/article.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
type Article struct {
|
||||
Id int
|
||||
Title string
|
||||
CateId int
|
||||
State int
|
||||
ArticleCate ArticleCate `gorm:"foreignKey:CateId;references:State"`
|
||||
}
|
||||
|
||||
func (Article) TableName() string {
|
||||
return "article"
|
||||
}
|
||||
11
model/article_cate.go
Normal file
11
model/article_cate.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package model
|
||||
|
||||
type ArticleCate struct {
|
||||
Id int
|
||||
Title string
|
||||
State int
|
||||
}
|
||||
|
||||
func (ArticleCate) TableName() string {
|
||||
return "article_cate"
|
||||
}
|
||||
30
model/core.go
Normal file
30
model/core.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
var err error
|
||||
|
||||
func init() {
|
||||
// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
|
||||
dsn := "db:g1VYgyrRzfd06M@tcp(127.0.0.1:3306)/db?charset=utf8mb4&parseTime=True&loc=Local"
|
||||
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
// 自动创建所有不存在的表
|
||||
err = DB.AutoMigrate(&User{}, &Article{}, &Nav{}, &ArticleCate{})
|
||||
if err != nil {
|
||||
fmt.Println("数据表迁移失败:", err)
|
||||
} else {
|
||||
fmt.Println("数据表迁移成功")
|
||||
}
|
||||
}
|
||||
13
model/nav.go
Normal file
13
model/nav.go
Normal file
@@ -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"
|
||||
}
|
||||
41
model/time.go
Normal file
41
model/time.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// TimeUtil 时间工具结构体
|
||||
type TimeUtil struct{}
|
||||
|
||||
// TimestampToDate 将时间戳转换为日期字符串
|
||||
// timestamp: 秒级或毫秒级时间戳
|
||||
// layout: 日期格式,如 "2006-01-02 15:04:05"
|
||||
func (t TimeUtil) TimestampToDate(timestamp int64, layout string) string {
|
||||
// 判断是秒级还是毫秒级时间戳
|
||||
if timestamp > 1e10 {
|
||||
timestamp = timestamp / 1000
|
||||
}
|
||||
return time.Unix(timestamp, 0).Format(layout)
|
||||
}
|
||||
|
||||
// TimestampToDateDefault 使用默认格式转换时间戳
|
||||
// 默认格式: 2006-01-02 15:04:05
|
||||
func (t TimeUtil) TimestampToDateDefault(timestamp int64) string {
|
||||
return t.TimestampToDate(timestamp, "2006-01-02 15:04:05")
|
||||
}
|
||||
|
||||
// TimestampToDateOnly 只返回日期部分
|
||||
// 格式: 2006-01-02
|
||||
func (t TimeUtil) TimestampToDateOnly(timestamp int64) string {
|
||||
return t.TimestampToDate(timestamp, "2006-01-02")
|
||||
}
|
||||
|
||||
// NowTimestamp 获取当前时间戳(秒级)
|
||||
func (t TimeUtil) NowTimestamp() int64 {
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
// NowTimestampMilli 获取当前时间戳(毫秒级)
|
||||
func (t TimeUtil) NowTimestampMilli() int64 {
|
||||
return time.Now().UnixMilli()
|
||||
}
|
||||
13
model/tools.go
Normal file
13
model/tools.go
Normal file
@@ -0,0 +1,13 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// 传入时间戳,返回时间格式字符串
|
||||
func UnixToTime(timestamp int) string {
|
||||
fmt.Println(timestamp)
|
||||
t := time.Unix(int64(timestamp), 0)
|
||||
return t.Format("2006-01-02 15:04:05")
|
||||
}
|
||||
14
model/user.go
Normal file
14
model/user.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
type User struct {
|
||||
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 {
|
||||
return "user"
|
||||
}
|
||||
@@ -2,23 +2,34 @@ package router
|
||||
|
||||
import (
|
||||
"awesomeProject/controller/admin"
|
||||
"awesomeProject/middleware"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func AdminRouterInit(r *gin.Engine) {
|
||||
adminRouter := r.Group("/admin")
|
||||
adminRouter := r.Group("/admin", middleware.InitMiddleware)
|
||||
{
|
||||
adminRouter.GET("", admin.IndexController{}.Index)
|
||||
|
||||
adminRouter.GET("/user/add", admin.UserController{}.Add)
|
||||
|
||||
adminRouter.GET("/user/edit", admin.UserController{}.Edit)
|
||||
|
||||
adminRouter.GET("/user/delete", admin.UserController{}.Delete)
|
||||
|
||||
adminRouter.GET("/plist", admin.IndexController{}.Plist)
|
||||
|
||||
adminRouter.GET("/login", admin.IndexController{}.Login)
|
||||
|
||||
adminRouter.GET("/article", admin.ArticleController{}.Article)
|
||||
adminRouter.GET("/article", admin.ArticleController{}.Index)
|
||||
|
||||
adminRouter.GET("/user", admin.UserController{}.Index)
|
||||
|
||||
adminRouter.POST("/user/doUpload", admin.UserController{}.DoUpload)
|
||||
|
||||
adminRouter.POST("/user/doEdit", admin.UserController{}.DoEdit)
|
||||
|
||||
adminRouter.GET("/user/:id", admin.UserController{}.Show)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,9 +8,11 @@ import (
|
||||
|
||||
func DefaultRouterInit(r *gin.Engine) {
|
||||
defaultRouter := r.Group("/")
|
||||
|
||||
{
|
||||
defaultRouter.GET("", nannanwu.DefaultController{}.Index)
|
||||
defaultRouter.GET("/article", nannanwu.DefaultController{}.Article)
|
||||
defaultRouter.GET("/deletecookie", nannanwu.DefaultController{}.DeleteCookie)
|
||||
defaultRouter.GET("/success", nannanwu.DefaultController{}.Success)
|
||||
defaultRouter.GET("/error", nannanwu.DefaultController{}.Error)
|
||||
}
|
||||
|
||||
21
templates/admin/useradd.html
Normal file
21
templates/admin/useradd.html
Normal file
@@ -0,0 +1,21 @@
|
||||
{{ define "admin/useradd" }}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>演示文件上传</h1>
|
||||
<form action="/admin/user/doUpload" method="post" enctype="multipart/form-data">
|
||||
<label for="username">用户名:</label>
|
||||
<input type="text" id="username" name="username" placeholder="用户名">
|
||||
<br>
|
||||
<label for="face">头 像:</label>
|
||||
<input type="file" id="face" name="face">
|
||||
<br>
|
||||
<input type="submit" value="上传">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
24
templates/admin/useredit.html
Normal file
24
templates/admin/useredit.html
Normal file
@@ -0,0 +1,24 @@
|
||||
{{ define "admin/useredit" }}
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Title</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>演示多文件上传</h1>
|
||||
<form action="/admin/user/doEdit" method="post" enctype="multipart/form-data">
|
||||
<label for="username">用户名:</label>
|
||||
<input type="text" id="username" name="username" placeholder="用户名">
|
||||
<br>
|
||||
<label for="face">头 像1:</label>
|
||||
<input type="file" id="face" name="face1">
|
||||
<br>
|
||||
<label for="face2">头 像2:</label>
|
||||
<input type="file" id="face2" name="face2">
|
||||
<br>
|
||||
<input type="submit" value="上传">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
{{ end }}
|
||||
@@ -1,4 +1,3 @@
|
||||
{{/* 定义index页面模板 */}}
|
||||
{{define "pages/index"}}
|
||||
<!doctype html>
|
||||
<html lang="zh">
|
||||
@@ -6,63 +5,53 @@
|
||||
<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 */}}
|
||||
<h2>{{.title}}</h2>
|
||||
<h2>{{UnixToTime .t}}</h2>
|
||||
|
||||
{{$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"}}
|
||||
{{template "layout/footer"}}
|
||||
</body>
|
||||
</html>
|
||||
{{end}}
|
||||
Reference in New Issue
Block a user