package handler import ( "net/http" "gen2d/internal/model" "github.com/gin-gonic/gin" ) // LoginRequest 登录请求参数。 type LoginRequest struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"` } // LoginResponse 登录成功返回的凭证及用户信息。 type LoginResponse struct { Token string `json:"token"` ExpiresIn int64 `json:"expiresIn"` User model.User `json:"user"` } // Login 用户登录接口,校验用户名/密码并返回 JWT 令牌。 func Login(c *gin.Context) { var req LoginRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "参数错误: "+err.Error())) return } token, expiresIn, user, err := authSvc.Login(c.Request.Context(), req.Username, req.Password) if err != nil { c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, err.Error())) return } c.JSON(http.StatusOK, model.OK(LoginResponse{ Token: token, ExpiresIn: expiresIn, User: *user, })) }