feat(admin): 实现用户管理的增删改查功能
- 新增用户添加接口,支持数据库中创建用户 - 实现用户编辑接口,允许更新用户信息 - 添加用户删除接口,支持从数据库删除用户 - 修改路由配置,新增用户编辑和删除的GET请求路径 - 使用gorm完成用户数据的增删改查操作 - 初始化数据库连接,自动迁移User表结构 - 新增User和Article模型定义及对应的表名函数 - 在main.go中添加session中间件支持 - defaultController中实现基于session的用户名保存与读取 - 添加go.mod依赖,包含gorm与gin-contrib/sessions相关包
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"awesomeProject/model"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
@@ -12,11 +14,41 @@ type UserController struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (con UserController) Index(c *gin.Context) {
|
func (con UserController) Index(c *gin.Context) {
|
||||||
con.Success(c)
|
//查询数据库
|
||||||
|
userList := []model.User{}
|
||||||
|
|
||||||
|
model.DB.Find(&userList)
|
||||||
|
c.JSON(200, userList)
|
||||||
|
//con.Success(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (con UserController) Add(c *gin.Context) {
|
func (con UserController) Add(c *gin.Context) {
|
||||||
c.HTML(200, "admin/useradd", gin.H{})
|
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) {
|
||||||
@@ -87,7 +119,3 @@ func (con UserController) DoEdit(c *gin.Context) {
|
|||||||
"dst1": dst1,
|
"dst1": dst1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (con UserController) Edit(c *gin.Context) {
|
|
||||||
c.HTML(200, "admin/useredit", gin.H{})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"awesomeProject/model"
|
"awesomeProject/model"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,6 +14,14 @@ type DefaultController struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (con DefaultController) Index(c *gin.Context) {
|
func (con DefaultController) Index(c *gin.Context) {
|
||||||
|
//设置session
|
||||||
|
session := sessions.Default(c)
|
||||||
|
session.Set("username", "张三sessions")
|
||||||
|
err := session.Save()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(model.UnixToTime(1629788418))
|
fmt.Println(model.UnixToTime(1629788418))
|
||||||
//设置cookie
|
//设置cookie
|
||||||
c.SetCookie("username", "张三", 3600, "/", "", false, false)
|
c.SetCookie("username", "张三", 3600, "/", "", false, false)
|
||||||
@@ -22,13 +31,17 @@ func (con DefaultController) Index(c *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
func (con DefaultController) Article(c *gin.Context) {
|
func (con DefaultController) Article(c *gin.Context) {
|
||||||
//获取cookie
|
//获取session
|
||||||
username, err := c.Cookie("username")
|
session := sessions.Default(c)
|
||||||
if err != nil {
|
username := session.Get("username")
|
||||||
c.String(200, "获取cookie失败")
|
c.String(200, "session=%v", username)
|
||||||
return
|
////获取cookie
|
||||||
}
|
//username, err := c.Cookie("username")
|
||||||
c.String(200, "cookie="+username)
|
//if err != nil {
|
||||||
|
// c.String(200, "获取cookie失败")
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
//c.String(200, "cookie="+username)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
go.mod
19
go.mod
@@ -2,9 +2,15 @@ module awesomeProject
|
|||||||
|
|
||||||
go 1.25
|
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 (
|
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
|
||||||
@@ -14,8 +20,14 @@ 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
|
||||||
@@ -30,11 +42,8 @@ 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.31.0 // indirect
|
golang.org/x/text v0.34.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
|
||||||
)
|
)
|
||||||
|
|||||||
5
main.go
5
main.go
@@ -5,6 +5,8 @@ import (
|
|||||||
"awesomeProject/router"
|
"awesomeProject/router"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
||||||
|
"github.com/gin-contrib/sessions"
|
||||||
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -24,6 +26,9 @@ func main() {
|
|||||||
// 配置静态文件目录,将./static目录映射到URL路径/static
|
// 配置静态文件目录,将./static目录映射到URL路径/static
|
||||||
r.Static("/static", "./static")
|
r.Static("/static", "./static")
|
||||||
|
|
||||||
|
//配置session中间件
|
||||||
|
r.Use(sessions.Sessions("my1session", cookie.NewStore([]byte("secre111111"))))
|
||||||
|
|
||||||
router.AdminRouterInit(r)
|
router.AdminRouterInit(r)
|
||||||
|
|
||||||
router.ApiRouterInit(r)
|
router.ApiRouterInit(r)
|
||||||
|
|||||||
12
model/article.go
Normal file
12
model/article.go
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type Article struct {
|
||||||
|
Id int
|
||||||
|
Title string
|
||||||
|
CateId int
|
||||||
|
State int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (Article) TableName() string {
|
||||||
|
return "article"
|
||||||
|
}
|
||||||
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{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("数据表迁移失败:", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("数据表迁移成功")
|
||||||
|
}
|
||||||
|
}
|
||||||
14
model/user.go
Normal file
14
model/user.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
Id int
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Email string
|
||||||
|
Password string
|
||||||
|
AddTime int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (User) TableName() string {
|
||||||
|
return "user"
|
||||||
|
}
|
||||||
@@ -14,6 +14,10 @@ func AdminRouterInit(r *gin.Engine) {
|
|||||||
|
|
||||||
adminRouter.GET("/user/add", admin.UserController{}.Add)
|
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)
|
||||||
@@ -24,8 +28,6 @@ func AdminRouterInit(r *gin.Engine) {
|
|||||||
|
|
||||||
adminRouter.POST("/user/doUpload", admin.UserController{}.DoUpload)
|
adminRouter.POST("/user/doUpload", admin.UserController{}.DoUpload)
|
||||||
|
|
||||||
adminRouter.GET("/user/edit", admin.UserController{}.Edit)
|
|
||||||
|
|
||||||
adminRouter.POST("/user/doEdit", admin.UserController{}.DoEdit)
|
adminRouter.POST("/user/doEdit", admin.UserController{}.DoEdit)
|
||||||
|
|
||||||
adminRouter.GET("/user/:id", admin.UserController{}.Show)
|
adminRouter.GET("/user/:id", admin.UserController{}.Show)
|
||||||
|
|||||||
Reference in New Issue
Block a user