Compare commits

..

1 Commits

Author SHA1 Message Date
958bb6e88e 2026年2月20日 2026-02-20 11:12:41 +08:00
23 changed files with 63 additions and 859 deletions

231
FUN.md
View File

@@ -1,231 +0,0 @@
# 🎉 程序员快乐指南
> 写代码就像谈恋爱,你永远不知道下一个 Bug 会在哪里出现。
---
## 🐛 Bug 的六种境界
| 境界 | 描述 | 程序员状态 |
|------|------|-----------|
| 第一层 | 代码能跑,但不知道为啥 | 😅 懵懂 |
| 第二层 | 代码不能跑,但知道为啥 | 🤔 清醒 |
| 第三层 | 代码能跑,也知道为啥 | 😊 自信 |
| 第四层 | 代码不能跑,也不知道为啥 | 😭 崩溃 |
| 第五层 | 代码昨天能跑,今天不能跑 | 🤯 怀疑人生 |
| 第六层 | 代码没动,自己好了 | 👻 见鬼了 |
---
## 💬 程序员经典语录
- "这不是 Bug这是特性"
- "在我电脑上好好的啊..."
- "重启试试?"
- "肯定是缓存问题"
- "下个版本再修"
---
## 🎯 编程语言选择指南
```
想快速上手?→ Python
想找工作? → Java
想装X → Haskell
想折磨自己? → C++
想全都要? → 做梦
```
---
## 🍕 程序员食物链
```
架构师
|
技术负责人
|
高级工程师
|
中级工程师
|
初级工程师
|
实习生
|
写代码的猫🐱
|
Stack Overflow
```
---
## ⏰ 程序员时间估算
| 你说的时间 | 实际时间 | 客户理解 |
|-----------|---------|---------|
| 5 分钟 | 2 小时 | 明天 |
| 1 小时 | 1 天 | 下周 |
| 1 天 | 1 周 | 下个月 |
| 1 周 | 1 月 | 明年 |
| 1 月 | 项目取消 | 从未存在 |
---
## 🔧 调试艺术
```go
// 初级程序员
fmt.Println("到这里了1")
fmt.Println("到这里了2")
fmt.Println("到这里了3")
// 中级程序员
log.Printf("debug: var=%v", variable)
// 高级程序员
// 盯着代码看 5 分钟,删掉一行,好了
// 大神
// 瞟一眼,"哦,少了个分号"
```
---
## 🎮 程序员日常 RPG
```
【任务】修复生产环境 Bug
【难度】⭐⭐⭐⭐⭐
【奖励】加班 + 老板的感谢 + 下次还找你
【任务】重构祖传代码
【难度】⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
【奖励】精神崩溃 + 想辞职
【任务】写文档
【难度】⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐⭐
【奖励】无(没人会看)
```
---
## 🌈 代码注释的艺术
```go
// 初级
// 这里是加法
a + b
// 中级
// 计算两个数的和
a + b
// 高级
// 别动,动了就崩
a + b
// 大神
// TODO: 这里有问题,但我懒得修
a + b
// 佛系
// 一切随缘
a + b
```
---
## 🎵 程序员之歌
```
🎶 在那山的那边海的那边
有一群程序员
他们老实又胹腆
他们聪明又没钱
他们一天到晚坐在那里熬夜写代码
饿了就咬一口方便面~
哦 可爱的程序员
哦 可怜的程序员
只要一改需求他们就要重新搞一遍
但是期限只剩下两天~
```
---
## 🏆 程序员成就系统
- [x] 写出没有 Bug 的代码(运行一次)
- [x] 在 Stack Overflow 找到答案
- [x] 复制粘贴代码并运行成功
- [x] 解释自己三个月前写的代码
- [ ] 在截止日前完成任务
- [ ] 产品经理不改需求
- [ ] 写出完美的注释
- [ ] 让客户满意
- [ ] 准时下班
---
## 🧙‍♂️ 程序员咒语
```
rm -rf /
😱 千万别试!
sudo !!
上条命令加权限,救急神器
ctrl + c, ctrl + v
程序员的核心技能
ctrl + z
后悔药,但现实中没有
```
---
## 💡 人生哲理
> "如果代码能运行,就不要动它。"
>
> "注释里的 TODO永远是 TODO。"
>
> "最好的代码是没有代码。"
>
> "过早优化是万恶之源。"
>
> "程序员最讨厌的两件事:
> 1. 别人不写注释
> 2. 别人让自己写注释"
---
## 🎬 程序员电影分级
- **G级**: 《社交网络》- 创业真美好
- **PG级**: 《硅谷》- 搞笑又心酸
- **R级**: 看自己的代码审查记录
- **X级**: 看生产环境的错误日志
---
*最后,记住程序员的终极奥义:*
```
if (code.Works()) {
dont.Touch()
}
```
**Happy Coding!** 🚀
---
*本文档仅供娱乐,如有雷同,说明你也是程序员。*

168
README.md
View File

@@ -1,167 +1,3 @@
# GoCode Web 项目 # GoCode
基于 Gin 框架的 Go Web 应用程序。 Go语言学习
## 项目结构
```
.
├── controller/ # 控制器层
│ ├── admin/ # 后台管理控制器
│ │ ├── articleController.go # 文章管理
│ │ ├── indexController.go # 后台首页
│ │ └── userController.go # 用户管理
│ ├── api/ # API 接口控制器
│ │ └── apiController.go # API 接口
│ └── nannanwu/ # 前台控制器
│ ├── baseController.go # 基础控制器
│ └── defaultController.go # 默认控制器
├── router/ # 路由配置
│ ├── adminRouter.go # 后台路由
│ ├── apiRouter.go # API 路由
│ └── defaultRouter.go # 默认路由
├── static/ # 静态资源
│ ├── footer.css # 底部样式
│ ├── navbar.css # 导航栏样式
│ └── style.css # 全局样式
├── templates/ # HTML 模板
│ ├── layout/ # 布局模板
│ │ ├── base.html # 基础布局
│ │ ├── footer.html # 底部模板
│ │ └── navbar.html # 导航栏模板
│ └── pages/ # 页面模板
│ ├── index.html # 首页
│ ├── test.html # 测试页
│ └── user.html # 用户页
├── main.go # 程序入口
├── go.mod # Go 模块配置
└── go.sum # Go 依赖校验
```
## 技术栈
- **框架**: [Gin](https://github.com/gin-gonic/gin) v1.11.0
- **语言**: Go 1.25+
- **模板**: Go HTML Template
## 快速开始
### 1. 安装依赖
```bash
go mod tidy
```
### 2. 运行项目
```bash
go run main.go
```
或编译后运行:
```bash
go build -o app.exe .
./app.exe
```
### 3. 访问应用
- 前台首页: http://localhost:8088/
- 后台管理: http://localhost:8088/admin
- API 接口: http://localhost:8088/api
## 路由说明
### 前台路由 (`/`)
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/` | 首页 |
| GET | `/article` | 文章详情 |
| GET | `/success` | 成功页面 |
| GET | `/error` | 错误页面 |
### 后台路由 (`/admin`)
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/admin` | 后台首页 |
| GET | `/admin/login` | 登录页面 |
| GET | `/admin/plist` | 列表页面 |
| GET | `/admin/article` | 文章列表 |
| GET | `/admin/user` | 用户列表 |
| GET | `/admin/user/:id` | 用户详情 |
### API 路由 (`/api`)
| 方法 | 路径 | 说明 |
|------|------|------|
| GET | `/api` | API 首页 |
| GET | `/api/user` | 用户信息 |
| GET | `/api/plist` | 用户列表 |
## 控制器说明
### BaseController
提供基础的响应方法:
- `Success()` - 返回成功响应
- `Error()` - 返回错误响应
### 继承关系
```
BaseController
└── DefaultController (继承 BaseController)
```
## 模板系统
使用 Go 的 `html/template`,支持模板继承:
- **基础模板**: `layout/base.html`
- **页面模板**: 继承 base 模板,填充 `content` block
示例:
```html
{{ define "layout/base" }}
{{ block "content" . }}{{ end }}
{{ end }}
```
## 静态资源
静态文件位于 `/static` 目录,通过 `/static/` 路径访问:
- CSS: `/static/style.css`
- 导航栏样式: `/static/navbar.css`
- 底部样式: `/static/footer.css`
## 配置说明
- **端口**: 8088 (在 `main.go` 中修改)
- **模板路径**: `templates/**/*`
- **静态资源**: `./static` 映射到 `/static`
## 开发规范
1. **控制器**: 按功能模块分组,放在 `controller/`
2. **路由**: 每个模块独立路由文件,统一在 `router/` 管理
3. **模板**: 布局模板放 `layout/`,页面模板放 `pages/`
4. **静态资源**: CSS 放 `static/`,按功能分文件
## 常见问题
### 编译错误: missing go.sum entry
```bash
go mod tidy
```
### 端口被占用
修改 `main.go` 中的端口号:
```go
r.Run(":8088") // 修改为其他端口
```

View File

@@ -1,21 +1,14 @@
package admin package admin
import ( import (
"awesomeProject/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// ArticleController 文章控制器
type ArticleController struct { type ArticleController struct {
} }
func (con ArticleController) Index(c *gin.Context) { // Article 文章列表方法
articleList := []model.Article{}
model.DB.Preload("ArticleCate").Find(&articleList)
c.JSON(200, articleList)
}
func (con ArticleController) Article(c *gin.Context) { func (con ArticleController) Article(c *gin.Context) {
c.String(200, "管理员文章列表") c.String(200, "管理员文章列表")
} }

View File

@@ -5,9 +5,9 @@ import "github.com/gin-gonic/gin"
type BaseController struct { type BaseController struct {
} }
func (con BaseController) Success(c *gin.Context) { func (con BaseController) succes(c *gin.Context) {
c.String(200, "成功") c.String(200, "成功")
} }
func (con BaseController) Error(c *gin.Context) { func (con BaseController) error(c *gin.Context) {
c.String(200, "失败") c.String(200, "失败")
} }

View File

@@ -1,24 +1,24 @@
package admin package admin
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// IndexController 索引控制器
type IndexController struct { type IndexController struct {
} }
// Index 首页方法
func (con IndexController) Index(c *gin.Context) { func (con IndexController) Index(c *gin.Context) {
username, _ := c.Get("user") c.String(200, "管理员用户列表")
fmt.Println(username)
c.String(200, "管理员用户列表 %v", username)
} }
// Plist 详情页方法
func (con IndexController) Plist(c *gin.Context) { func (con IndexController) Plist(c *gin.Context) {
c.String(200, "管理员用户详情") c.String(200, "管理员用户详情")
} }
// Login 登录页方法
func (con IndexController) Login(c *gin.Context) { func (con IndexController) Login(c *gin.Context) {
c.String(200, "管理员登录页面") c.String(200, "管理员登录页面")
} }

View File

@@ -1,17 +0,0 @@
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)
}

View File

@@ -1,121 +1,15 @@
package admin package admin
import ( import "github.com/gin-gonic/gin"
"awesomeProject/model"
"fmt"
"os"
"path/filepath"
"github.com/gin-gonic/gin"
)
type UserController struct { type UserController struct {
BaseController BaseController
} }
func (con UserController) Index(c *gin.Context) { func (con UserController) Index(c *gin.Context) {
//查询数据库 con.succes(c)
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) { func (con UserController) Show(c *gin.Context) {
c.String(200, "管理员用户详情") 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,
})
}

View File

@@ -1,12 +1,6 @@
package nannanwu package nannanwu
import ( import "github.com/gin-gonic/gin"
"awesomeProject/model"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type DefaultController struct { type DefaultController struct {
//继承BaseController //继承BaseController
@@ -14,47 +8,13 @@ type DefaultController struct {
} }
func (con DefaultController) Index(c *gin.Context) { func (con DefaultController) Index(c *gin.Context) {
//设置session c.String(200, "首页")
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) { func (con DefaultController) Article(c *gin.Context) {
//获取session c.String(200, "文章详情")
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)
} }
func (con DefaultController) DeleteCookie(c *gin.Context) { // 使用继承的方式调用BaseController的success方法
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) { func (con DefaultController) Success(c *gin.Context) {
BaseController{}.success(c) BaseController{}.success(c)
} }

19
go.mod
View File

@@ -2,15 +2,9 @@ module awesomeProject
go 1.25 go 1.25
require ( require github.com/gin-gonic/gin v1.11.0
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 ( require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/bytedance/gopkg v0.1.3 // indirect github.com/bytedance/gopkg v0.1.3 // indirect
github.com/bytedance/sonic v1.14.2 // indirect github.com/bytedance/sonic v1.14.2 // indirect
github.com/bytedance/sonic/loader v0.4.0 // indirect github.com/bytedance/sonic/loader v0.4.0 // indirect
@@ -20,14 +14,8 @@ require (
github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.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-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-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.18.0 // 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/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect
github.com/leodido/go-urn v1.4.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect
@@ -42,8 +30,11 @@ require (
go.uber.org/mock v0.6.0 // indirect go.uber.org/mock v0.6.0 // indirect
golang.org/x/arch v0.23.0 // indirect golang.org/x/arch v0.23.0 // indirect
golang.org/x/crypto v0.45.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/net v0.47.0 // indirect
golang.org/x/sync v0.18.0 // indirect
golang.org/x/sys v0.38.0 // indirect golang.org/x/sys v0.38.0 // indirect
golang.org/x/text v0.34.0 // indirect golang.org/x/text v0.31.0 // indirect
golang.org/x/tools v0.39.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect google.golang.org/protobuf v1.36.10 // indirect
) )

31
main.go
View File

@@ -1,43 +1,26 @@
package main package main
import ( import (
"awesomeProject/model"
"awesomeProject/router" "awesomeProject/router"
"html/template" "fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// main 函数是程序的入口点初始化并启动Web服务器
func main() { func main() {
// 创建默认的gin引擎
r := gin.Default() r := gin.Default()
// 加载模板(支持多层目录)
//自定义模板函数
r.SetFuncMap(template.FuncMap{
"UnixToTime": model.UnixToTime,
})
// 加载模板文件,支持多层目录结构
r.LoadHTMLGlob("templates/**/*") r.LoadHTMLGlob("templates/**/*")
// 配置静态文件目录,将./static目录映射到URL路径/static
r.Static("/static", "./static") r.Static("/static", "./static")
//配置session中间件 // 初始化路由
r.Use(sessions.Sessions("my1session", cookie.NewStore([]byte("secre111111")))) router.DefaultRouterInit(r)
router.AdminRouterInit(r) router.AdminRouterInit(r)
router.ApiRouterInit(r) router.ApiRouterInit(r)
router.DefaultRouterInit(r) fmt.Println("Server running on port 8080")
err := r.Run(":8080")
// 启动HTTP服务器监听在8081端口
err := r.Run(":8088")
if err != nil { if err != nil {
fmt.Printf("Server failed to start: %v\n", err)
return return
} }
} }

View File

@@ -1,24 +0,0 @@
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()
}

View File

@@ -1,13 +0,0 @@
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"
}

View File

@@ -1,11 +0,0 @@
package model
type ArticleCate struct {
Id int
Title string
State int
}
func (ArticleCate) TableName() string {
return "article_cate"
}

View File

@@ -1,30 +0,0 @@
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("数据表迁移成功")
}
}

View File

@@ -1,13 +0,0 @@
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"
}

View File

@@ -1,41 +0,0 @@
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()
}

View File

@@ -1,13 +0,0 @@
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")
}

View File

@@ -1,14 +0,0 @@
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"
}

View File

@@ -2,34 +2,23 @@ package router
import ( import (
"awesomeProject/controller/admin" "awesomeProject/controller/admin"
"awesomeProject/middleware"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func AdminRouterInit(r *gin.Engine) { func AdminRouterInit(r *gin.Engine) {
adminRouter := r.Group("/admin", middleware.InitMiddleware) adminRouter := r.Group("/admin")
{ {
adminRouter.GET("", admin.IndexController{}.Index) 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("/plist", admin.IndexController{}.Plist)
adminRouter.GET("/login", admin.IndexController{}.Login) adminRouter.GET("/login", admin.IndexController{}.Login)
adminRouter.GET("/article", admin.ArticleController{}.Index) adminRouter.GET("/article", admin.ArticleController{}.Article)
adminRouter.GET("/user", admin.UserController{}.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) adminRouter.GET("/user/:id", admin.UserController{}.Show)
} }
} }

View File

@@ -6,13 +6,12 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// DefaultRouterInit 默认路由初始化函数
func DefaultRouterInit(r *gin.Engine) { func DefaultRouterInit(r *gin.Engine) {
defaultRouter := r.Group("/") defaultRouter := r.Group("/")
{ {
defaultRouter.GET("", nannanwu.DefaultController{}.Index) defaultRouter.GET("", nannanwu.HomeController{}.Index)
defaultRouter.GET("/article", nannanwu.DefaultController{}.Article) defaultRouter.GET("/article", nannanwu.DefaultController{}.Article)
defaultRouter.GET("/deletecookie", nannanwu.DefaultController{}.DeleteCookie)
defaultRouter.GET("/success", nannanwu.DefaultController{}.Success) defaultRouter.GET("/success", nannanwu.DefaultController{}.Success)
defaultRouter.GET("/error", nannanwu.DefaultController{}.Error) defaultRouter.GET("/error", nannanwu.DefaultController{}.Error)
} }

View File

@@ -1,21 +0,0 @@
{{ 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 }}

View File

@@ -1,24 +0,0 @@
{{ 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 }}

View File

@@ -1,3 +1,4 @@
{{/* 定义index页面模板 */}}
{{define "pages/index"}} {{define "pages/index"}}
<!doctype html> <!doctype html>
<html lang="zh"> <html lang="zh">
@@ -5,53 +6,63 @@
<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>
<h2>{{UnixToTime .t}}</h2>
{{/* 模板变量定义示例 - 将标题赋值给局部变量t */}}
{{$t := .title}} {{$t := .title}}
<h4>{{$t}}</h4> <h4>{{$t}}</h4>
{{if ge .score 60}} {{/* 条件判断示例 - 根据score值判断是否及格 */}}
{{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 .news}} {{/* 结构体数据访问示例 - 使用with语句简化结构体属性访问 */}}
{{.Title}} {{with .news}} <!-- 针对news结构体设置上下文 -->
{{.Content}} {{.Title}} <!-- 直接访问Title属性等同于.news.Title -->
{{.Content}} <!-- 直接访问Content属性等同于.news.Content -->
{{end}} {{end}}
<br> <br>
{{.data}} {{/* 自定义模板函数使用示例 */}}
{{.data}} <!-- 输出原始时间戳 -->
{{template "layout/footer"}}
{{ template "layout/footer"}}
</body> </body>
</html> </html>
{{end}} {{end}}