171 lines
4.3 KiB
Go
171 lines
4.3 KiB
Go
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} models.LoginResponse
|
||
// @Failure 400 {object} models.ErrorResponse
|
||
// @Failure 401 {object} models.ErrorResponse
|
||
// @Router /auth/login [post]
|
||
func (c *AuthController) Login(ctx *gin.Context) {
|
||
var loginData models.LoginDTO
|
||
if !BindJSON(ctx, &loginData) {
|
||
return
|
||
}
|
||
|
||
user, err := c.authService.ValidateUser(loginData.Username, loginData.Password)
|
||
if err != nil {
|
||
ErrorResponse(ctx, http.StatusUnauthorized, err.Error())
|
||
return
|
||
}
|
||
|
||
token, err := c.authService.GenerateToken(user)
|
||
if err != nil {
|
||
ErrorResponse(ctx, http.StatusInternalServerError, "令牌生成失败")
|
||
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.DataResponse
|
||
// @Failure 401 {object} models.ErrorResponse
|
||
// @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} models.BaseResponse
|
||
// @Failure 400 {object} models.ErrorResponse
|
||
// @Failure 401 {object} models.ErrorResponse
|
||
// @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.DataResponse
|
||
// @Failure 400 {object} models.ErrorResponse
|
||
// @Failure 401 {object} models.ErrorResponse
|
||
// @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,
|
||
})
|
||
}
|
||
|
||
// Logout 登出
|
||
// @Summary 用户登出
|
||
// @Description 用户登出(JWT 无状态,前端清理令牌即可)
|
||
// @Tags 认证
|
||
// @Produce json
|
||
// @Security BearerAuth
|
||
// @Success 200 {object} models.BaseResponse
|
||
// @Failure 401 {object} models.ErrorResponse
|
||
// @Router /auth/logout [post]
|
||
func (c *AuthController) Logout(ctx *gin.Context) {
|
||
SuccessResponse(ctx, "登出成功")
|
||
}
|