Initial commit

This commit is contained in:
2026-02-12 14:31:30 +08:00
commit e01cdc9889
25 changed files with 3227 additions and 0 deletions

View File

@@ -0,0 +1,165 @@
package controllers
import (
"net/http"
"github.com/gin-gonic/gin"
"git.beifan.cn/trace-system/backend-go/models"
"git.beifan.cn/trace-system/backend-go/services"
)
// AuthController 认证控制器
type AuthController struct {
authService services.AuthService
}
// NewAuthController 创建认证控制器实例
func NewAuthController() *AuthController {
return &AuthController{
authService: services.AuthService{},
}
}
// Login 登录
// @Summary 用户登录
// @Description 验证用户身份并返回 JWT 令牌
// @Tags 认证
// @Accept json
// @Produce json
// @Param loginData body models.LoginDTO true "登录数据"
// @Success 200 {object} gin.H{message: string, accessToken: string, user: models.UserDTO}
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Router /auth/login [post]
func (c *AuthController) Login(ctx *gin.Context) {
var loginData models.LoginDTO
if err := ctx.ShouldBindJSON(&loginData); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{
"message": "无效的请求数据",
"error": err.Error(),
})
return
}
user, err := c.authService.ValidateUser(loginData.Username, loginData.Password)
if err != nil {
ctx.JSON(http.StatusUnauthorized, gin.H{
"message": err.Error(),
})
return
}
token, err := c.authService.GenerateToken(user)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": "令牌生成失败",
})
return
}
ctx.JSON(http.StatusOK, gin.H{
"message": "登录成功",
"accessToken": token,
"user": models.UserDTO{
ID: user.ID,
Username: user.Username,
Name: user.Name,
Email: user.Email,
Role: user.Role,
CreatedAt: user.CreatedAt,
},
})
}
// GetProfile 获取用户信息
// @Summary 获取用户信息
// @Description 获取当前登录用户的个人信息
// @Tags 认证
// @Produce json
// @Security BearerAuth
// @Success 200 {object} models.UserDTO
// @Failure 401 {object} gin.H{message: string}
// @Router /auth/profile [get]
func (c *AuthController) GetProfile(ctx *gin.Context) {
userModel, ok := GetCurrentUser(ctx)
if !ok {
return
}
profile, err := c.authService.GetProfile(userModel.ID)
if err != nil {
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
return
}
SuccessResponse(ctx, "获取用户信息成功", gin.H{
"user": profile,
})
}
// ChangePassword 修改密码
// @Summary 修改密码
// @Description 修改当前登录用户的密码
// @Tags 认证
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param passwordData body models.ChangePasswordDTO true "密码修改数据"
// @Success 200 {object} gin.H{message: string}
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Router /auth/change-password [post]
func (c *AuthController) ChangePassword(ctx *gin.Context) {
userModel, ok := GetCurrentUser(ctx)
if !ok {
return
}
var changePasswordData models.ChangePasswordDTO
if !BindJSON(ctx, &changePasswordData) {
return
}
err := c.authService.ChangePassword(userModel.ID, changePasswordData.CurrentPassword, changePasswordData.NewPassword)
if err != nil {
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
return
}
SuccessResponse(ctx, "密码修改成功")
}
// UpdateProfile 更新用户信息
// @Summary 更新用户信息
// @Description 更新当前登录用户的个人信息
// @Tags 认证
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param profileData body models.UpdateProfileDTO true "用户信息更新数据"
// @Success 200 {object} models.UserDTO
// @Failure 400 {object} gin.H{message: string}
// @Failure 401 {object} gin.H{message: string}
// @Router /auth/profile [put]
func (c *AuthController) UpdateProfile(ctx *gin.Context) {
userModel, ok := GetCurrentUser(ctx)
if !ok {
return
}
var updateProfileData models.UpdateProfileDTO
if !BindJSON(ctx, &updateProfileData) {
return
}
profile, err := c.authService.UpdateProfile(userModel.ID, updateProfileData.Name, updateProfileData.Email)
if err != nil {
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
return
}
SuccessResponse(ctx, "用户信息更新成功", gin.H{
"user": profile,
})
}