整理
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package controllers
|
||||
package admin
|
||||
|
||||
import (
|
||||
"html/template"
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user