refactor: 重构后端架构为 go-zero 框架,优化项目结构

主要变更:
- 采用 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 最佳实践
This commit is contained in:
xujiang
2025-07-10 15:05:52 +08:00
parent a2f2f66f88
commit 39a42695d3
52 changed files with 6047 additions and 2349 deletions

View File

@ -5,7 +5,7 @@ import (
"strings"
"github.com/gin-gonic/gin"
"photography-backend/internal/service/auth"
"photography-backend/internal/models"
"photography-backend/internal/model/entity"
)
// AuthMiddleware 认证中间件
@ -100,12 +100,12 @@ func (m *AuthMiddleware) RequireRole(requiredRole string) gin.HandlerFunc {
// RequireAdmin 需要管理员权限的中间件
func (m *AuthMiddleware) RequireAdmin() gin.HandlerFunc {
return m.RequireRole(models.RoleAdmin)
return m.RequireRole(string(entity.UserRoleAdmin))
}
// RequireEditor 需要编辑者权限的中间件
func (m *AuthMiddleware) RequireEditor() gin.HandlerFunc {
return m.RequireRole(models.RoleEditor)
// RequirePhotographer 需要摄影师权限的中间件
func (m *AuthMiddleware) RequirePhotographer() gin.HandlerFunc {
return m.RequireRole(string(entity.UserRolePhotographer))
}
// OptionalAuth 可选认证中间件
@ -183,24 +183,24 @@ func IsAdmin(c *gin.Context) bool {
if !exists {
return false
}
return role == models.RoleAdmin
return role == string(entity.UserRoleAdmin)
}
// IsEditor 检查是否为编辑者或以上
func IsEditor(c *gin.Context) bool {
// IsPhotographer 检查是否为摄影师或以上
func IsPhotographer(c *gin.Context) bool {
role, exists := GetCurrentUserRole(c)
if !exists {
return false
}
return role == models.RoleEditor || role == models.RoleAdmin
return role == string(entity.UserRolePhotographer) || role == string(entity.UserRoleAdmin)
}
// hasPermission 检查权限
func (m *AuthMiddleware) hasPermission(userRole, requiredRole string) bool {
roleLevel := map[string]int{
models.RoleUser: 1,
models.RoleEditor: 2,
models.RoleAdmin: 3,
string(entity.UserRoleUser): 1,
string(entity.UserRolePhotographer): 2,
string(entity.UserRoleAdmin): 3,
}
userLevel, exists := roleLevel[userRole]