package handler import ( "net/http" "github.com/gin-gonic/gin" "flowstack/internal/crypto" "flowstack/internal/database" "flowstack/internal/model" ) // Login POST /api/auth/login {username, password} func Login(c *gin.Context) { var body struct { Username string `json:"username" binding:"required"` Password string `json:"password" binding:"required"` } if err := c.ShouldBindJSON(&body); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": "用户名和密码不能为空"}) return } var user model.User if err := database.DB.Where("username = ?", body.Username).First(&user).Error; err != nil { c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"}) return } if !crypto.VerifyPassword(body.Password, user.Password) { c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"}) return } c.JSON(http.StatusOK, gin.H{ "success": true, "username": user.Username, "nickname": user.Nickname, "role": user.Role, }) }