diff --git a/controllers/admin.go b/controllers/admin/admin.go similarity index 96% rename from controllers/admin.go rename to controllers/admin/admin.go index 815a3cd..20a1c1a 100644 --- a/controllers/admin.go +++ b/controllers/admin/admin.go @@ -1,4 +1,4 @@ -package controllers +package admin import ( "html/template" diff --git a/controllers/themes.go b/controllers/admin/themes.go similarity index 95% rename from controllers/themes.go rename to controllers/admin/themes.go index a6f02f8..0423c2e 100644 --- a/controllers/themes.go +++ b/controllers/admin/themes.go @@ -1,4 +1,4 @@ -package controllers +package admin import ( "html/template" @@ -6,7 +6,7 @@ import ( "go_blog/config" "go_blog/models" - "go_blog/themes" + "go_blog/utils" "github.com/gin-gonic/gin" ) @@ -19,7 +19,7 @@ func ListThemes(c *gin.Context) { c.String(http.StatusInternalServerError, "主题管理器未找到") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "主题管理器类型错误") return @@ -59,7 +59,7 @@ func SwitchTheme(c *gin.Context) { c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"}) return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"}) return diff --git a/controllers/users.go b/controllers/admin/users.go similarity index 88% rename from controllers/users.go rename to controllers/admin/users.go index 9d43457..95dc807 100644 --- a/controllers/users.go +++ b/controllers/admin/users.go @@ -4,13 +4,12 @@ @File : users.py */ -package controllers +package admin import ( "go_blog/models" "go_blog/pkg/util" - "go_blog/serializers" - "go_blog/themes" + "go_blog/utils" "html/template" "net/http" @@ -20,7 +19,7 @@ import ( // 登录 func UsersLoginHandler(ctx *gin.Context) { - var loginUser serializers.Login + var loginUser models.Account if err := ctx.ShouldBind(&loginUser); err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, @@ -45,7 +44,7 @@ func UsersLoginHandler(ctx *gin.Context) { return } - // 改为设置Session(替代JWT) + // 改为设置Session(不使用JWT) session := sessions.Default(ctx) session.Set("user_id", user.ID) // 存储用户ID到Session session.Save() @@ -63,7 +62,7 @@ func UsersLoginHandler(ctx *gin.Context) { // 注册 func UsersRegisterHandler(ctx *gin.Context) { - var registerUser serializers.Login + var registerUser models.Account if err := ctx.ShouldBind(®isterUser); err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, @@ -71,8 +70,8 @@ func UsersRegisterHandler(ctx *gin.Context) { }) // 替换 panic 为错误响应 return } - user := registerUser.GetUser() - status := user.CheckDuplicateUsername() + + status := registerUser.CheckDuplicateUsername() if status == false { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, @@ -80,11 +79,15 @@ func UsersRegisterHandler(ctx *gin.Context) { }) return } - if err := user.SetPassword(user.Password); err != nil { - panic(err) + if err := registerUser.SetPassword(registerUser.Password); err != nil { + ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ + "code": http.StatusBadRequest, + "msg": "密码设置错误: " + err.Error(), + }) + return } - user.IsActive = true - models.DB.Create(&user) + registerUser.IsActive = true + models.DB.Create(®isterUser) ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "注册成功"}) } @@ -115,7 +118,7 @@ func UsersSetPwdHandler(ctx *gin.Context) { }) return } - var user serializers.Account + var user models.Account if err := ctx.ShouldBindJSON(&user); err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, @@ -130,21 +133,21 @@ func UsersSetPwdHandler(ctx *gin.Context) { }) return } - if user.OldPwd == user.NewPwd { + if ctx.GetString("OldPwd") == ctx.GetString("NewPwd") { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, "msg": "两次输入的密码相同", }) return } - if isPwd := currentUser.IsPasswordEqual(user.OldPwd); !isPwd { + if isPwd := currentUser.IsPasswordEqual(user.Password); !isPwd { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, "msg": "原密码错误", }) return } - if err := currentUser.SetPassword(user.NewPwd); err != nil { + if err := currentUser.SetPassword(ctx.GetString("NewPwd")); err != nil { ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{ "code": http.StatusBadRequest, "msg": err.Error(), @@ -157,9 +160,8 @@ func UsersSetPwdHandler(ctx *gin.Context) { "msg": "密码修改成功", }) } - func UsersListHandler(ctx *gin.Context) { - var pager serializers.Pager + var pager utils.Pager pager.InitPager(ctx) var users []models.Account @@ -206,7 +208,7 @@ func ShowRegisterPage(c *gin.Context) { c.String(http.StatusInternalServerError, "Theme manager not found") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "Invalid theme manager type") return diff --git a/controllers/home.go b/controllers/home.go index ee26021..b226117 100644 --- a/controllers/home.go +++ b/controllers/home.go @@ -2,7 +2,7 @@ package controllers import ( "go_blog/models" - "go_blog/themes" // <-- 确保导入 themes 包 + "go_blog/utils" "net/http" "github.com/gin-gonic/gin" @@ -15,7 +15,7 @@ func Home(c *gin.Context) { c.String(http.StatusInternalServerError, "Theme manager not found in context") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "Invalid theme manager type in context") return diff --git a/controllers/showpost.go b/controllers/showpost.go index 27a4bd1..cb5d748 100644 --- a/controllers/showpost.go +++ b/controllers/showpost.go @@ -2,7 +2,7 @@ package controllers import ( "go_blog/models" - "go_blog/themes" // <-- 确保导入 themes 包 + "go_blog/utils" "net/http" "strconv" @@ -16,7 +16,7 @@ func ShowPost(c *gin.Context) { c.String(http.StatusInternalServerError, "Theme manager not found") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "Invalid theme manager type") return diff --git a/main.go b/main.go index be0545f..7d9f692 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,11 @@ import ( "go_blog/config" "go_blog/models" "go_blog/routers" + "go_blog/utils" "log/slog" "os" "time" - "go_blog/themes" - "gorm.io/gorm" "github.com/gin-contrib/sessions" @@ -42,7 +41,7 @@ func main() { } }() // 4. 初始化主题系统 - themeManager := themes.NewManager("web\\themes") + themeManager := utils.NewManager("web\\themes") if err := themeManager.LoadTheme(conf.Theme.Current); err != nil { slog.Error("主题系统初始化失败", "error", err) os.Exit(1) @@ -98,7 +97,7 @@ func configMiddleware(cfg *config.Config) gin.HandlerFunc { } // 主题管理器中间件 -func themeMiddleware(manager *themes.ThemeManager) gin.HandlerFunc { +func themeMiddleware(manager *utils.ThemeManager) gin.HandlerFunc { return func(c *gin.Context) { c.Set("ThemeManager", manager) c.Next() diff --git a/models/account.go b/models/account.go index 005e3be..c278f5f 100644 --- a/models/account.go +++ b/models/account.go @@ -70,11 +70,10 @@ func (a *Account) CheckDuplicateUsername() bool { } } -// GetAccountByID 根据 ID 查询用户 func GetAccountByID(id uint) (*Account, error) { - var account Account - if err := DB.First(&account, id).Error; err != nil { - return nil, err - } - return &account, nil + var account Account + if err := DB.First(&account, id).Error; err != nil { + return nil, err + } + return &account, nil } diff --git a/routers/router.go b/routers/router.go index 728dd7d..4b9f37d 100644 --- a/routers/router.go +++ b/routers/router.go @@ -3,9 +3,9 @@ package routers // Add the package declaration at the top import ( "fmt" "go_blog/controllers" + "go_blog/controllers/admin" "go_blog/models" - "go_blog/serializers" - "go_blog/themes" // <-- 确保导入 themes 包 + "go_blog/utils" "log" "net/http" "strconv" @@ -73,14 +73,14 @@ func RegisterRoutes(r *gin.Engine) { c.String(http.StatusInternalServerError, "Theme manager not found") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "Invalid theme manager type") return } var items []models.Content - var pager serializers.Pager + var pager utils.Pager pager.InitPager(c) // 这会从查询参数中读取 page 和 pageSize offset := (pager.Page - 1) * pager.PageSize @@ -126,7 +126,7 @@ func RegisterRoutes(r *gin.Engine) { c.String(http.StatusInternalServerError, "Theme manager not found") return } - themeManager, ok := tm.(*themes.ThemeManager) + themeManager, ok := tm.(*utils.ThemeManager) if !ok { c.String(http.StatusInternalServerError, "Invalid theme manager type") return @@ -157,29 +157,30 @@ func RegisterRoutes(r *gin.Engine) { r.GET("/", controllers.Home) r.GET("/post/:id", controllers.ShowPost) - admin := r.Group("/admin") - admin.Use(CheckAdminAuth()) + adminGroup := r.Group("/admin") + adminGroup.Use(CheckAdminAuth()) { // 无需认证的公开路由(登录/注册) // 新增:处理 /admin 根路径跳转 - admin.GET("/", func(c *gin.Context) { + adminGroup.GET("/", func(c *gin.Context) { + // 由于 CheckAdminAuth 中间件已处理未登录场景,此处用户必定已登录 c.Redirect(http.StatusFound, "/admin/index") }) - admin.GET("/login", controllers.ShowLoginPage) - admin.GET("/register", controllers.ShowRegisterPage) - admin.POST("/login", controllers.UsersLoginHandler) - admin.POST("/register", controllers.UsersRegisterHandler) + adminGroup.GET("/login", admin.ShowLoginPage) + adminGroup.GET("/register", admin.ShowRegisterPage) + adminGroup.POST("/login", admin.UsersLoginHandler) + adminGroup.POST("/register", admin.UsersRegisterHandler) // 需要认证的路由组 - authAdmin := admin.Group("", SessionAuthRequired()) + authAdmin := adminGroup.Group("", SessionAuthRequired()) { - authAdmin.GET("/index", controllers.ShowAdminIndexPage) - authAdmin.GET("/themes", controllers.ListThemes) - authAdmin.POST("/themes/switch", controllers.SwitchTheme) - authAdmin.POST("/setpwd", controllers.UsersSetPwdHandler) - admin.GET("/logout", controllers.UsersLogoutHandler) // 添加退出路由 + authAdmin.GET("/index", admin.ShowAdminIndexPage) + authAdmin.GET("/themes", admin.ListThemes) + authAdmin.POST("/themes/switch", admin.SwitchTheme) + authAdmin.POST("/setpwd", admin.UsersSetPwdHandler) + authAdmin.GET("/logout", admin.UsersLogoutHandler) // 添加退出路由 } } diff --git a/serializers/account.go b/serializers/account.go deleted file mode 100644 index 5c47d35..0000000 --- a/serializers/account.go +++ /dev/null @@ -1,27 +0,0 @@ -/* -@Time : 2020/6/28 22:16 -@Author : xuyiqing -@File : users.py -*/ - -package serializers - -import "go_blog/models" - -type Login struct { - Username string `form:"username"; json:"username"` - Password string `form:"password"; json:"password"` -} - -func (l *Login) GetUser() *models.Account { - return &models.Account{ - Username: l.Username, - Password: l.Password, - } -} - -type Account struct { - Username string `form:"username" json:"username"` - OldPwd string `form:"oldPwd" json:"oldPwd"` - NewPwd string `form:"newPwd"json:"newPwd"` -} diff --git a/serializers/pagination.go b/utils/pagination.go similarity index 59% rename from serializers/pagination.go rename to utils/pagination.go index 2f0d369..47a86b1 100644 --- a/serializers/pagination.go +++ b/utils/pagination.go @@ -1,22 +1,17 @@ -/* -@Time : 2020/7/16 23:44 -@Author : xuyiqing -@File : common.py -*/ - -package serializers +package utils import ( - "github.com/gin-gonic/gin" "strconv" + + "github.com/gin-gonic/gin" ) type Pager struct { - Page int `json:"page" form:"page"` + Page int `json:"page" form:"page"` PageSize int `json:"pageSize" form:"pageSize"` - OffSet int `json:"-"` - Total int `json:"total"` - MaxPage int `json:"maxPage"` + OffSet int `json:"-"` + Total int `json:"total"` + MaxPage int `json:"maxPage"` } func (p *Pager) InitPager(ctx *gin.Context) { @@ -26,5 +21,5 @@ func (p *Pager) InitPager(ctx *gin.Context) { } func (p *Pager) GetPager() { - p.MaxPage = int(p.Total / p.PageSize) + 1 + p.MaxPage = int(p.Total/p.PageSize) + 1 } diff --git a/themes/manager.go b/utils/thememanager.go similarity index 99% rename from themes/manager.go rename to utils/thememanager.go index b0329a8..8b17ce7 100644 --- a/themes/manager.go +++ b/utils/thememanager.go @@ -1,4 +1,4 @@ -package themes +package utils import ( "embed"