主要变更: - 采用 go-zero 框架替代 Gin,提升开发效率 - 重构项目结构,API 文件模块化组织 - 将 model 移至 api/internal/model 目录 - 移除 common 包,改为标准 pkg 目录结构 - 实现统一的仓储模式,支持配置驱动数据库切换 - 简化测试策略,专注 API 集成测试 - 更新 CLAUDE.md 文档,提供详细的开发指导 技术栈更新: - 框架: Gin → go-zero v1.6.0+ - 代码生成: 引入 goctl 工具 - 架构模式: 四层架构 → go-zero 三层架构 (Handler→Logic→Model) - 项目布局: 遵循 Go 社区标准和 go-zero 最佳实践
119 lines
3.3 KiB
Go
119 lines
3.3 KiB
Go
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"}))
|
||
} |