模板还不行,需要改变。
This commit is contained in:
@@ -7,7 +7,6 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_blog/models"
|
||||
"go_blog/pkg/util"
|
||||
"go_blog/serializers"
|
||||
@@ -30,7 +29,6 @@ func UsersLoginHandler(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证用户逻辑不变
|
||||
user := &models.Account{Username: loginUser.Username}
|
||||
if err := models.DB.Where("username = ?", user.Username).First(user).Error; err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
@@ -90,50 +88,6 @@ func UsersRegisterHandler(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, gin.H{"code": http.StatusOK, "msg": "注册成功"})
|
||||
}
|
||||
|
||||
// 修改用户信息
|
||||
func UsersSetInfoHandler(ctx *gin.Context) {
|
||||
|
||||
jsonData, err := util.GetBodyData(ctx)
|
||||
if err != nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"msg": "参数解析失败",
|
||||
})
|
||||
return
|
||||
}
|
||||
fmt.Println(jsonData)
|
||||
if jsonData == nil {
|
||||
ctx.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
|
||||
"code": http.StatusBadRequest,
|
||||
"msg": "获取不到参数",
|
||||
})
|
||||
return
|
||||
}
|
||||
// 从上下文中获取用户(假设 JWT 中间件已将用户存入 "user" 键)
|
||||
user, exists := ctx.Get("user")
|
||||
if !exists {
|
||||
ctx.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{
|
||||
"code": http.StatusUnauthorized,
|
||||
"msg": "未登录",
|
||||
})
|
||||
return
|
||||
}
|
||||
currentUser, ok := user.(*models.Account) // 明确类型为 models.Account
|
||||
if !ok {
|
||||
ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
|
||||
"code": http.StatusInternalServerError,
|
||||
"msg": "用户类型错误",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
models.DB.Model(currentUser).Updates(jsonData)
|
||||
ctx.JSON(http.StatusOK, gin.H{
|
||||
"code": http.StatusOK,
|
||||
"msg": "更新成功",
|
||||
})
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
func UsersSetPwdHandler(ctx *gin.Context) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user