This commit is contained in:
2025-09-12 16:58:39 +08:00
parent 63db640abe
commit 4a78aa1c20
9 changed files with 799 additions and 34 deletions

View File

@@ -30,30 +30,41 @@ func NewController(userRepo repository.UserRepository, logger *logs.Logger, toke
// CreateUserRequest 定义创建用户请求的结构体
type CreateUserRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required,min=6"`
Username string `json:"username" binding:"required" example:"newuser"`
Password string `json:"password" binding:"required,min=6" example:"password123"`
}
// LoginRequest 定义登录请求的结构体
type LoginRequest struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
Username string `json:"username" binding:"required" example:"testuser"`
Password string `json:"password" binding:"required" example:"password123"`
}
// CreateUserResponse 定义创建用户成功响应的结构体
type CreateUserResponse struct {
Username string `json:"username"`
ID uint `json:"id"`
Username string `json:"username" example:"newuser"`
ID uint `json:"id" example:"1"`
}
// LoginResponse 定义登录成功响应的结构体
type LoginResponse struct {
Username string `json:"username"`
ID uint `json:"id"`
Token string `json:"token"`
Username string `json:"username" example:"testuser"`
ID uint `json:"id" example:"1"`
Token string `json:"token" example:"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."`
}
// CreateUser 处理创建用户的请求
// CreateUser godoc
// @Summary 创建新用户
// @Description 根据用户名和密码创建一个新的系统用户。
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param user body CreateUserRequest true "用户信息"
// @Success 200 {object} controller.Response{data=CreateUserResponse} "用户创建成功"
// @Failure 400 {object} controller.Response "请求参数错误"
// @Failure 409 {object} controller.Response "用户名已存在"
// @Failure 500 {object} controller.Response "服务器内部错误"
// @Router /users [post]
func (c *Controller) CreateUser(ctx *gin.Context) {
var req CreateUserRequest
if err := ctx.ShouldBindJSON(&req); err != nil {
@@ -88,7 +99,18 @@ func (c *Controller) CreateUser(ctx *gin.Context) {
})
}
// Login 处理用户登录请求
// Login godoc
// @Summary 用户登录
// @Description 用户使用用户名和密码登录,成功后返回 JWT 令牌。
// @Tags 用户管理
// @Accept json
// @Produce json
// @Param credentials body LoginRequest true "登录凭证"
// @Success 200 {object} controller.Response{data=LoginResponse} "登录成功"
// @Failure 400 {object} controller.Response "请求参数错误"
// @Failure 401 {object} controller.Response "用户名或密码不正确"
// @Failure 500 {object} controller.Response "服务器内部错误"
// @Router /users/login [post]
func (c *Controller) Login(ctx *gin.Context) {
var req LoginRequest
if err := ctx.ShouldBindJSON(&req); err != nil {