2024-01-10 16:05:15 +08:00
|
|
|
/*
|
|
|
|
|
@Time : 2020/6/28 21:40
|
|
|
|
|
@Author : xuyiqing
|
|
|
|
|
@File : users.py
|
|
|
|
|
*/
|
|
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
package controllers
|
2024-01-10 16:05:15 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"go_blog/models"
|
|
|
|
|
"go_blog/pkg/jwt"
|
|
|
|
|
"go_blog/pkg/util"
|
|
|
|
|
"go_blog/serializers"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 登录
|
|
|
|
|
func UsersLoginHandler(ctx *gin.Context) {
|
|
|
|
|
response := Response{Ctx: ctx}
|
|
|
|
|
var loginUser serializers.Login
|
|
|
|
|
if err := ctx.ShouldBind(&loginUser); err != nil {
|
2025-05-27 19:01:05 +08:00
|
|
|
response.BadRequest("请求参数错误: " + err.Error()) // 替换 panic 为错误响应
|
|
|
|
|
return
|
2024-01-10 16:05:15 +08:00
|
|
|
}
|
|
|
|
|
user := loginUser.GetUser()
|
|
|
|
|
isLoginUser := user.CheckPassword()
|
|
|
|
|
if !isLoginUser {
|
|
|
|
|
response.BadRequest("密码错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-05-27 19:01:05 +08:00
|
|
|
token, err := jwt.GenerateToken(user)
|
2024-01-10 16:05:15 +08:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
data, _ := util.PrecisionLost(user)
|
|
|
|
|
data["token"] = token
|
|
|
|
|
response.Response(data, nil)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 注册
|
|
|
|
|
func UsersRegisterHandler(ctx *gin.Context) {
|
|
|
|
|
response := Response{Ctx: ctx}
|
|
|
|
|
var registerUser serializers.Login
|
|
|
|
|
if err := ctx.ShouldBind(®isterUser); err != nil {
|
2025-05-27 19:01:05 +08:00
|
|
|
response.BadRequest("请求参数错误: " + err.Error()) // 替换 panic 为错误响应
|
|
|
|
|
return
|
2024-01-10 16:05:15 +08:00
|
|
|
}
|
|
|
|
|
user := registerUser.GetUser()
|
|
|
|
|
status := user.CheckDuplicateUsername()
|
|
|
|
|
if status == false {
|
|
|
|
|
response.BadRequest("用户名已存在")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := user.SetPassword(user.Password); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
user.IsActive = true
|
|
|
|
|
models.DB.Create(&user)
|
|
|
|
|
response.Response(nil, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改用户信息
|
|
|
|
|
func UsersSetInfoHandler(ctx *gin.Context) {
|
|
|
|
|
response := Response{Ctx: ctx}
|
|
|
|
|
jsonData, err := util.GetBodyData(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.BadRequest("参数解析失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
fmt.Println(jsonData)
|
|
|
|
|
if jsonData == nil {
|
|
|
|
|
response.BadRequest("获取不到参数")
|
|
|
|
|
return
|
|
|
|
|
}
|
2025-05-27 19:01:05 +08:00
|
|
|
// 从上下文中获取用户(假设 JWT 中间件已将用户存入 "user" 键)
|
|
|
|
|
user, exists := ctx.Get("user")
|
|
|
|
|
if !exists {
|
|
|
|
|
response.Unauthenticated("未登录")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
currentUser, ok := user.(*models.Account) // 明确类型为 models.Account
|
|
|
|
|
if !ok {
|
|
|
|
|
response.ServerError("用户类型错误")
|
2024-01-10 16:05:15 +08:00
|
|
|
return
|
|
|
|
|
}
|
2025-05-27 19:01:05 +08:00
|
|
|
|
|
|
|
|
models.DB.Model(currentUser).Updates(jsonData)
|
|
|
|
|
response.Response(currentUser, nil)
|
2024-01-10 16:05:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 修改密码
|
|
|
|
|
func UsersSetPwdHandler(ctx *gin.Context) {
|
|
|
|
|
response := Response{Ctx: ctx}
|
|
|
|
|
currentUser := jwt.AssertUser(ctx)
|
|
|
|
|
if currentUser == nil {
|
|
|
|
|
response.Unauthenticated("未验证登录")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var user serializers.Account
|
|
|
|
|
if err := ctx.ShouldBindJSON(&user); err != nil {
|
|
|
|
|
response.BadRequest(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if user.Username != currentUser.Username {
|
|
|
|
|
response.BadRequest("当前登录用户用户名与输入用户名不符")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if user.OldPwd == user.NewPwd {
|
|
|
|
|
response.BadRequest("两次输入的密码相同")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if isPwd := currentUser.IsPasswordEqual(user.OldPwd); !isPwd {
|
|
|
|
|
response.BadRequest("原密码错误")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := currentUser.SetPassword(user.NewPwd); err != nil {
|
|
|
|
|
response.BadRequest(err.Error())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
models.DB.Save(¤tUser)
|
|
|
|
|
response.Response(nil, nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func UsersListHandler(ctx *gin.Context) {
|
|
|
|
|
response := Response{Ctx: ctx}
|
|
|
|
|
var pager serializers.Pager
|
|
|
|
|
pager.InitPager(ctx)
|
|
|
|
|
var users []models.Account
|
2025-05-27 19:01:05 +08:00
|
|
|
|
|
|
|
|
// 先查询总记录数
|
|
|
|
|
var totalCount int64
|
|
|
|
|
models.DB.Model(&models.Account{}).Count(&totalCount)
|
|
|
|
|
pager.Total = int(totalCount) // 正确设置总数
|
|
|
|
|
|
|
|
|
|
// 分页查询
|
|
|
|
|
models.DB.Offset(pager.OffSet()).Limit(pager.PageSize).Find(&users)
|
2024-01-10 16:05:15 +08:00
|
|
|
pager.GetPager()
|
|
|
|
|
response.Response(users, pager)
|
|
|
|
|
}
|