feat(login/register): 实现用户注册登录handler以及用户模型
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gen2d/internal/model"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// RegisterRequest 注册请求参数。
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=32"` // 用户名
|
||||
Password string `json:"password" binding:"required,min=6,max=64"` // 密码
|
||||
Email string `json:"email" binding:"omitempty,email"` // 邮箱(可选)
|
||||
}
|
||||
|
||||
// RegisterResponse 注册成功返回的用户信息。
|
||||
type RegisterResponse struct {
|
||||
ID uint `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// Register 用户注册接口,创建新用户账号。
|
||||
func Register(c *gin.Context) {
|
||||
var req RegisterRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "参数错误: "+err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: 调用 service 层创建用户
|
||||
c.JSON(http.StatusCreated, model.OK(RegisterResponse{
|
||||
ID: 0,
|
||||
Username: req.Username,
|
||||
}))
|
||||
}
|
||||
Reference in New Issue
Block a user