382 lines
9.1 KiB
Go
382 lines
9.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"yinli-api/internal/middleware"
|
|
"yinli-api/internal/model"
|
|
"yinli-api/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// UserHandler 用户处理器
|
|
type UserHandler struct {
|
|
userService service.UserService
|
|
}
|
|
|
|
// NewUserHandler 创建用户处理器实例
|
|
func NewUserHandler(userService service.UserService) *UserHandler {
|
|
return &UserHandler{
|
|
userService: userService,
|
|
}
|
|
}
|
|
|
|
// Register 用户注册
|
|
// @Summary 用户注册
|
|
// @Description 用户注册接口
|
|
// @Tags 用户认证
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.UserRegisterRequest true "注册信息"
|
|
// @Success 200 {object} model.LoginResponse
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/auth/register [post]
|
|
func (h *UserHandler) Register(c *gin.Context) {
|
|
var req model.UserRegisterRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
response, err := h.userService.Register(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "注册成功",
|
|
"data": response,
|
|
})
|
|
}
|
|
|
|
// Login 用户登录
|
|
// @Summary 用户登录
|
|
// @Description 用户登录接口
|
|
// @Tags 用户认证
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body model.UserLoginRequest true "登录信息"
|
|
// @Success 200 {object} model.LoginResponse
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/auth/login [post]
|
|
func (h *UserHandler) Login(c *gin.Context) {
|
|
var req model.UserLoginRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
response, err := h.userService.Login(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "登录成功",
|
|
"data": response,
|
|
})
|
|
}
|
|
|
|
// GetProfile 获取用户资料
|
|
// @Summary 获取用户资料
|
|
// @Description 获取当前用户的资料信息
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Success 200 {object} model.UserResponse
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/user/profile [get]
|
|
func (h *UserHandler) GetProfile(c *gin.Context) {
|
|
userID, exists := middleware.GetUserID(c)
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"code": 401,
|
|
"message": "未认证",
|
|
})
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.GetProfile(userID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "获取成功",
|
|
"data": user,
|
|
})
|
|
}
|
|
|
|
// UpdateProfile 更新用户资料
|
|
// @Summary 更新用户资料
|
|
// @Description 更新当前用户的资料信息
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param request body map[string]interface{} true "更新信息"
|
|
// @Success 200 {object} model.UserResponse
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/user/profile [put]
|
|
func (h *UserHandler) UpdateProfile(c *gin.Context) {
|
|
userID, exists := middleware.GetUserID(c)
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"code": 401,
|
|
"message": "未认证",
|
|
})
|
|
return
|
|
}
|
|
|
|
var updates map[string]interface{}
|
|
if err := c.ShouldBindJSON(&updates); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
user, err := h.userService.UpdateProfile(userID, updates)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "更新成功",
|
|
"data": user,
|
|
})
|
|
}
|
|
|
|
// ChangePassword 修改密码
|
|
// @Summary 修改密码
|
|
// @Description 修改当前用户的密码
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param request body map[string]string true "密码信息"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/user/password [put]
|
|
func (h *UserHandler) ChangePassword(c *gin.Context) {
|
|
userID, exists := middleware.GetUserID(c)
|
|
if !exists {
|
|
c.JSON(http.StatusUnauthorized, gin.H{
|
|
"code": 401,
|
|
"message": "未认证",
|
|
})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
OldPassword string `json:"old_password" binding:"required"`
|
|
NewPassword string `json:"new_password" binding:"required,min=6"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
err := h.userService.ChangePassword(userID, req.OldPassword, req.NewPassword)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "密码修改成功",
|
|
})
|
|
}
|
|
|
|
// GetUserList 获取用户列表(管理员)
|
|
// @Summary 获取用户列表
|
|
// @Description 获取用户列表(管理员权限)
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param page query int false "页码" default(1)
|
|
// @Param limit query int false "每页数量" default(10)
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 403 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/admin/users [get]
|
|
func (h *UserHandler) GetUserList(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
|
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if limit < 1 || limit > 100 {
|
|
limit = 10
|
|
}
|
|
|
|
offset := (page - 1) * limit
|
|
|
|
users, total, err := h.userService.GetUserList(offset, limit)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{
|
|
"code": 500,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "获取成功",
|
|
"data": gin.H{
|
|
"users": users,
|
|
"pagination": gin.H{
|
|
"page": page,
|
|
"limit": limit,
|
|
"total": total,
|
|
},
|
|
},
|
|
})
|
|
}
|
|
|
|
// DeleteUser 删除用户(管理员)
|
|
// @Summary 删除用户
|
|
// @Description 删除指定用户(管理员权限)
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param id path int true "用户ID"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 403 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/admin/users/{id} [delete]
|
|
func (h *UserHandler) DeleteUser(c *gin.Context) {
|
|
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "无效的用户ID",
|
|
})
|
|
return
|
|
}
|
|
|
|
err = h.userService.DeleteUser(uint(userID))
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "删除成功",
|
|
})
|
|
}
|
|
|
|
// UpdateUserStatus 更新用户状态(管理员)
|
|
// @Summary 更新用户状态
|
|
// @Description 更新指定用户的状态(管理员权限)
|
|
// @Tags 用户管理
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security ApiKeyAuth
|
|
// @Param id path int true "用户ID"
|
|
// @Param request body map[string]int true "状态信息"
|
|
// @Success 200 {object} map[string]interface{}
|
|
// @Failure 400 {object} map[string]interface{}
|
|
// @Failure 401 {object} map[string]interface{}
|
|
// @Failure 403 {object} map[string]interface{}
|
|
// @Failure 500 {object} map[string]interface{}
|
|
// @Router /api/admin/users/{id}/status [put]
|
|
func (h *UserHandler) UpdateUserStatus(c *gin.Context) {
|
|
userID, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "无效的用户ID",
|
|
})
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
Status int `json:"status" binding:"required,oneof=0 1"`
|
|
}
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": "请求参数错误",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
err = h.userService.UpdateUserStatus(uint(userID), req.Status)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"code": 400,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 200,
|
|
"message": "状态更新成功",
|
|
})
|
|
}
|