This commit is contained in:
xujiang
2025-07-10 18:09:11 +08:00
parent 5cbdc5af73
commit 604b9e59ba
95 changed files with 23709 additions and 19 deletions

View File

@ -0,0 +1,119 @@
package handlers
import (
"net/http"
"github.com/gin-gonic/gin"
"photography-backend/internal/model/entity"
"photography-backend/internal/model/dto"
"photography-backend/internal/service/auth"
"photography-backend/internal/api/middleware"
"photography-backend/pkg/response"
)
// AuthHandler 认证处理器
type AuthHandler struct {
authService *auth.AuthService
}
// NewAuthHandler 创建认证处理器
func NewAuthHandler(authService *auth.AuthService) *AuthHandler {
return &AuthHandler{
authService: authService,
}
}
// Login 用户登录
func (h *AuthHandler) Login(c *gin.Context) {
var req dto.LoginRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
loginResp, err := h.authService.Login(&req)
if err != nil {
c.JSON(http.StatusUnauthorized, response.Error(http.StatusUnauthorized, err.Error()))
return
}
c.JSON(http.StatusOK, response.Success(loginResp))
}
// Register 用户注册
func (h *AuthHandler) Register(c *gin.Context) {
var req dto.CreateUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
user, err := h.authService.Register(&req)
if err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
c.JSON(http.StatusCreated, response.Success(user))
}
// RefreshToken 刷新令牌
func (h *AuthHandler) RefreshToken(c *gin.Context) {
var req dto.RefreshTokenRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
loginResp, err := h.authService.RefreshToken(&req)
if err != nil {
c.JSON(http.StatusUnauthorized, response.Error(http.StatusUnauthorized, err.Error()))
return
}
c.JSON(http.StatusOK, response.Success(loginResp))
}
// GetProfile 获取用户资料
func (h *AuthHandler) GetProfile(c *gin.Context) {
userID, exists := middleware.GetCurrentUser(c)
if !exists {
c.JSON(http.StatusUnauthorized, response.Error(http.StatusUnauthorized, "User not authenticated"))
return
}
user, err := h.authService.GetUserByID(userID)
if err != nil {
c.JSON(http.StatusInternalServerError, response.Error(http.StatusInternalServerError, err.Error()))
return
}
c.JSON(http.StatusOK, response.Success(user))
}
// UpdatePassword 更新密码
func (h *AuthHandler) UpdatePassword(c *gin.Context) {
userID, exists := middleware.GetCurrentUser(c)
if !exists {
c.JSON(http.StatusUnauthorized, response.Error(http.StatusUnauthorized, "User not authenticated"))
return
}
var req dto.ChangePasswordRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
if err := h.authService.UpdatePassword(userID, &req); err != nil {
c.JSON(http.StatusBadRequest, response.Error(http.StatusBadRequest, err.Error()))
return
}
c.JSON(http.StatusOK, response.Success(gin.H{"message": "Password updated successfully"}))
}
// Logout 用户登出
func (h *AuthHandler) Logout(c *gin.Context) {
// 简单实现实际应用中可能需要将token加入黑名单
c.JSON(http.StatusOK, response.Success(gin.H{"message": "Logged out successfully"}))
}