整理
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
package controllers
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package controllers
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
"go_blog/config"
|
"go_blog/config"
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/themes"
|
"go_blog/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@@ -19,7 +19,7 @@ func ListThemes(c *gin.Context) {
|
|||||||
c.String(http.StatusInternalServerError, "主题管理器未找到")
|
c.String(http.StatusInternalServerError, "主题管理器未找到")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "主题管理器类型错误")
|
c.String(http.StatusInternalServerError, "主题管理器类型错误")
|
||||||
return
|
return
|
||||||
@@ -59,7 +59,7 @@ func SwitchTheme(c *gin.Context) {
|
|||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器未找到"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "主题管理器类型错误"})
|
||||||
return
|
return
|
||||||
@@ -4,13 +4,12 @@
|
|||||||
@File : users.py
|
@File : users.py
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package controllers
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/pkg/util"
|
"go_blog/pkg/util"
|
||||||
"go_blog/serializers"
|
"go_blog/utils"
|
||||||
"go_blog/themes"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -20,7 +19,7 @@ import (
|
|||||||
|
|
||||||
// 登录
|
// 登录
|
||||||
func UsersLoginHandler(ctx *gin.Context) {
|
func UsersLoginHandler(ctx *gin.Context) {
|
||||||
var loginUser serializers.Login
|
var loginUser models.Account
|
||||||
if err := ctx.ShouldBind(&loginUser); err != nil {
|
if err := ctx.ShouldBind(&loginUser); err != nil {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
@@ -45,7 +44,7 @@ func UsersLoginHandler(ctx *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 改为设置Session(替代JWT)
|
// 改为设置Session(不使用JWT)
|
||||||
session := sessions.Default(ctx)
|
session := sessions.Default(ctx)
|
||||||
session.Set("user_id", user.ID) // 存储用户ID到Session
|
session.Set("user_id", user.ID) // 存储用户ID到Session
|
||||||
session.Save()
|
session.Save()
|
||||||
@@ -63,7 +62,7 @@ func UsersLoginHandler(ctx *gin.Context) {
|
|||||||
|
|
||||||
// 注册
|
// 注册
|
||||||
func UsersRegisterHandler(ctx *gin.Context) {
|
func UsersRegisterHandler(ctx *gin.Context) {
|
||||||
var registerUser serializers.Login
|
var registerUser models.Account
|
||||||
if err := ctx.ShouldBind(®isterUser); err != nil {
|
if err := ctx.ShouldBind(®isterUser); err != nil {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
@@ -71,8 +70,8 @@ func UsersRegisterHandler(ctx *gin.Context) {
|
|||||||
}) // 替换 panic 为错误响应
|
}) // 替换 panic 为错误响应
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
user := registerUser.GetUser()
|
|
||||||
status := user.CheckDuplicateUsername()
|
status := registerUser.CheckDuplicateUsername()
|
||||||
if status == false {
|
if status == false {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
@@ -80,11 +79,15 @@ func UsersRegisterHandler(ctx *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := user.SetPassword(user.Password); err != nil {
|
if err := registerUser.SetPassword(registerUser.Password); err != nil {
|
||||||
panic(err)
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
|
"code": http.StatusBadRequest,
|
||||||
|
"msg": "密码设置错误: " + err.Error(),
|
||||||
|
})
|
||||||
|
return
|
||||||
}
|
}
|
||||||
user.IsActive = true
|
registerUser.IsActive = true
|
||||||
models.DB.Create(&user)
|
models.DB.Create(®isterUser)
|
||||||
ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "注册成功"})
|
ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "注册成功"})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +118,7 @@ func UsersSetPwdHandler(ctx *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var user serializers.Account
|
var user models.Account
|
||||||
if err := ctx.ShouldBindJSON(&user); err != nil {
|
if err := ctx.ShouldBindJSON(&user); err != nil {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
@@ -130,21 +133,21 @@ func UsersSetPwdHandler(ctx *gin.Context) {
|
|||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if user.OldPwd == user.NewPwd {
|
if ctx.GetString("OldPwd") == ctx.GetString("NewPwd") {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
"msg": "两次输入的密码相同",
|
"msg": "两次输入的密码相同",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if isPwd := currentUser.IsPasswordEqual(user.OldPwd); !isPwd {
|
if isPwd := currentUser.IsPasswordEqual(user.Password); !isPwd {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
"msg": "原密码错误",
|
"msg": "原密码错误",
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err := currentUser.SetPassword(user.NewPwd); err != nil {
|
if err := currentUser.SetPassword(ctx.GetString("NewPwd")); err != nil {
|
||||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||||
"code": http.StatusBadRequest,
|
"code": http.StatusBadRequest,
|
||||||
"msg": err.Error(),
|
"msg": err.Error(),
|
||||||
@@ -157,9 +160,8 @@ func UsersSetPwdHandler(ctx *gin.Context) {
|
|||||||
"msg": "密码修改成功",
|
"msg": "密码修改成功",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func UsersListHandler(ctx *gin.Context) {
|
func UsersListHandler(ctx *gin.Context) {
|
||||||
var pager serializers.Pager
|
var pager utils.Pager
|
||||||
pager.InitPager(ctx)
|
pager.InitPager(ctx)
|
||||||
var users []models.Account
|
var users []models.Account
|
||||||
|
|
||||||
@@ -206,7 +208,7 @@ func ShowRegisterPage(c *gin.Context) {
|
|||||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||||
return
|
return
|
||||||
@@ -2,7 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/themes" // <-- 确保导入 themes 包
|
"go_blog/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -15,7 +15,7 @@ func Home(c *gin.Context) {
|
|||||||
c.String(http.StatusInternalServerError, "Theme manager not found in context")
|
c.String(http.StatusInternalServerError, "Theme manager not found in context")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "Invalid theme manager type in context")
|
c.String(http.StatusInternalServerError, "Invalid theme manager type in context")
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/themes" // <-- 确保导入 themes 包
|
"go_blog/utils"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -16,7 +16,7 @@ func ShowPost(c *gin.Context) {
|
|||||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||||
return
|
return
|
||||||
|
|||||||
7
main.go
7
main.go
@@ -4,12 +4,11 @@ import (
|
|||||||
"go_blog/config"
|
"go_blog/config"
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/routers"
|
"go_blog/routers"
|
||||||
|
"go_blog/utils"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go_blog/themes"
|
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
@@ -42,7 +41,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
// 4. 初始化主题系统
|
// 4. 初始化主题系统
|
||||||
themeManager := themes.NewManager("web\\themes")
|
themeManager := utils.NewManager("web\\themes")
|
||||||
if err := themeManager.LoadTheme(conf.Theme.Current); err != nil {
|
if err := themeManager.LoadTheme(conf.Theme.Current); err != nil {
|
||||||
slog.Error("主题系统初始化失败", "error", err)
|
slog.Error("主题系统初始化失败", "error", err)
|
||||||
os.Exit(1)
|
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) {
|
return func(c *gin.Context) {
|
||||||
c.Set("ThemeManager", manager)
|
c.Set("ThemeManager", manager)
|
||||||
c.Next()
|
c.Next()
|
||||||
|
|||||||
@@ -70,7 +70,6 @@ func (a *Account) CheckDuplicateUsername() bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAccountByID 根据 ID 查询用户
|
|
||||||
func GetAccountByID(id uint) (*Account, error) {
|
func GetAccountByID(id uint) (*Account, error) {
|
||||||
var account Account
|
var account Account
|
||||||
if err := DB.First(&account, id).Error; err != nil {
|
if err := DB.First(&account, id).Error; err != nil {
|
||||||
|
|||||||
@@ -3,9 +3,9 @@ package routers // Add the package declaration at the top
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go_blog/controllers"
|
"go_blog/controllers"
|
||||||
|
"go_blog/controllers/admin"
|
||||||
"go_blog/models"
|
"go_blog/models"
|
||||||
"go_blog/serializers"
|
"go_blog/utils"
|
||||||
"go_blog/themes" // <-- 确保导入 themes 包
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -73,14 +73,14 @@ func RegisterRoutes(r *gin.Engine) {
|
|||||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var items []models.Content
|
var items []models.Content
|
||||||
var pager serializers.Pager
|
var pager utils.Pager
|
||||||
pager.InitPager(c) // 这会从查询参数中读取 page 和 pageSize
|
pager.InitPager(c) // 这会从查询参数中读取 page 和 pageSize
|
||||||
offset := (pager.Page - 1) * pager.PageSize
|
offset := (pager.Page - 1) * pager.PageSize
|
||||||
|
|
||||||
@@ -126,7 +126,7 @@ func RegisterRoutes(r *gin.Engine) {
|
|||||||
c.String(http.StatusInternalServerError, "Theme manager not found")
|
c.String(http.StatusInternalServerError, "Theme manager not found")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
themeManager, ok := tm.(*themes.ThemeManager)
|
themeManager, ok := tm.(*utils.ThemeManager)
|
||||||
if !ok {
|
if !ok {
|
||||||
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
c.String(http.StatusInternalServerError, "Invalid theme manager type")
|
||||||
return
|
return
|
||||||
@@ -157,29 +157,30 @@ func RegisterRoutes(r *gin.Engine) {
|
|||||||
r.GET("/", controllers.Home)
|
r.GET("/", controllers.Home)
|
||||||
r.GET("/post/:id", controllers.ShowPost)
|
r.GET("/post/:id", controllers.ShowPost)
|
||||||
|
|
||||||
admin := r.Group("/admin")
|
adminGroup := r.Group("/admin")
|
||||||
admin.Use(CheckAdminAuth())
|
adminGroup.Use(CheckAdminAuth())
|
||||||
{
|
{
|
||||||
// 无需认证的公开路由(登录/注册)
|
// 无需认证的公开路由(登录/注册)
|
||||||
// 新增:处理 /admin 根路径跳转
|
// 新增:处理 /admin 根路径跳转
|
||||||
admin.GET("/", func(c *gin.Context) {
|
adminGroup.GET("/", func(c *gin.Context) {
|
||||||
|
|
||||||
// 由于 CheckAdminAuth 中间件已处理未登录场景,此处用户必定已登录
|
// 由于 CheckAdminAuth 中间件已处理未登录场景,此处用户必定已登录
|
||||||
c.Redirect(http.StatusFound, "/admin/index")
|
c.Redirect(http.StatusFound, "/admin/index")
|
||||||
})
|
})
|
||||||
|
|
||||||
admin.GET("/login", controllers.ShowLoginPage)
|
adminGroup.GET("/login", admin.ShowLoginPage)
|
||||||
admin.GET("/register", controllers.ShowRegisterPage)
|
adminGroup.GET("/register", admin.ShowRegisterPage)
|
||||||
admin.POST("/login", controllers.UsersLoginHandler)
|
adminGroup.POST("/login", admin.UsersLoginHandler)
|
||||||
admin.POST("/register", controllers.UsersRegisterHandler)
|
adminGroup.POST("/register", admin.UsersRegisterHandler)
|
||||||
|
|
||||||
// 需要认证的路由组
|
// 需要认证的路由组
|
||||||
authAdmin := admin.Group("", SessionAuthRequired())
|
authAdmin := adminGroup.Group("", SessionAuthRequired())
|
||||||
{
|
{
|
||||||
authAdmin.GET("/index", controllers.ShowAdminIndexPage)
|
authAdmin.GET("/index", admin.ShowAdminIndexPage)
|
||||||
authAdmin.GET("/themes", controllers.ListThemes)
|
authAdmin.GET("/themes", admin.ListThemes)
|
||||||
authAdmin.POST("/themes/switch", controllers.SwitchTheme)
|
authAdmin.POST("/themes/switch", admin.SwitchTheme)
|
||||||
authAdmin.POST("/setpwd", controllers.UsersSetPwdHandler)
|
authAdmin.POST("/setpwd", admin.UsersSetPwdHandler)
|
||||||
admin.GET("/logout", controllers.UsersLogoutHandler) // 添加退出路由
|
authAdmin.GET("/logout", admin.UsersLogoutHandler) // 添加退出路由
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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"`
|
|
||||||
}
|
|
||||||
@@ -1,14 +1,9 @@
|
|||||||
/*
|
package utils
|
||||||
@Time : 2020/7/16 23:44
|
|
||||||
@Author : xuyiqing
|
|
||||||
@File : common.py
|
|
||||||
*/
|
|
||||||
|
|
||||||
package serializers
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Pager struct {
|
type Pager struct {
|
||||||
@@ -26,5 +21,5 @@ func (p *Pager) InitPager(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *Pager) GetPager() {
|
func (p *Pager) GetPager() {
|
||||||
p.MaxPage = int(p.Total / p.PageSize) + 1
|
p.MaxPage = int(p.Total/p.PageSize) + 1
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package themes
|
package utils
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
Reference in New Issue
Block a user