|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"gofaster/internal/auth/controller"
|
|
|
|
"gofaster/internal/auth/repository"
|
|
|
|
"gofaster/internal/auth/routes"
|
|
|
|
"gofaster/internal/auth/service"
|
|
|
|
"gofaster/internal/core"
|
|
|
|
"gofaster/internal/shared/config"
|
|
|
|
"gofaster/internal/shared/database"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Module 认证模块
|
|
|
|
type Module struct {
|
|
|
|
userController *controller.UserController
|
|
|
|
authController *controller.AuthController
|
|
|
|
passwordController *controller.PasswordController
|
|
|
|
menuRouteController *controller.MenuRouteController
|
|
|
|
frontendRouteController *controller.FrontendRouteController
|
|
|
|
routeSyncService *service.RouteSyncService
|
|
|
|
db *gorm.DB
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewModule 创建新的认证模块
|
|
|
|
func NewModule() *Module {
|
|
|
|
return &Module{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name 获取模块名称
|
|
|
|
func (m *Module) Name() string {
|
|
|
|
return "auth"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init 初始化模块
|
|
|
|
func (m *Module) Init(cfg *config.Config, logger *zap.Logger, db *gorm.DB, redis *database.RedisClient) error {
|
|
|
|
m.db = db
|
|
|
|
m.logger = logger
|
|
|
|
|
|
|
|
// 初始化仓库
|
|
|
|
userRepo := repository.NewUserRepository(db)
|
|
|
|
passwordPolicyRepo := repository.NewPasswordPolicyRepository(db)
|
|
|
|
passwordHistoryRepo := repository.NewPasswordHistoryRepository(db)
|
|
|
|
passwordResetRepo := repository.NewPasswordResetRepository(db)
|
|
|
|
routeMappingRepo := repository.NewRouteMappingRepository(db)
|
|
|
|
resourceRepo := repository.NewResourceRepository(db)
|
|
|
|
menuRepo := repository.NewMenuRepository(db)
|
|
|
|
menuRouteRepo := repository.NewMenuRouteRepository(db)
|
|
|
|
frontendRouteRepo := repository.NewFrontendRouteRepository(db)
|
|
|
|
frontendBackendRouteRepo := repository.NewFrontendBackendRouteRepository(db)
|
|
|
|
|
|
|
|
// 初始化服务
|
|
|
|
userService := service.NewUserService(userRepo, db)
|
|
|
|
captchaRepo := repository.NewCaptchaRepository(db)
|
|
|
|
authService := service.NewAuthService(userRepo, captchaRepo)
|
|
|
|
passwordService := service.NewPasswordService(
|
|
|
|
userRepo,
|
|
|
|
passwordPolicyRepo,
|
|
|
|
passwordHistoryRepo,
|
|
|
|
passwordResetRepo,
|
|
|
|
)
|
|
|
|
menuRouteService := service.NewMenuRouteService(menuRouteRepo, menuRepo, routeMappingRepo, logger)
|
|
|
|
frontendRouteService := service.NewFrontendRouteService(frontendRouteRepo, frontendBackendRouteRepo, logger)
|
|
|
|
m.routeSyncService = service.NewRouteSyncService(routeMappingRepo, resourceRepo, logger)
|
|
|
|
|
|
|
|
// 初始化控制器
|
|
|
|
m.userController = controller.NewUserController(userService)
|
|
|
|
m.authController = controller.NewAuthController(authService)
|
|
|
|
m.passwordController = controller.NewPasswordController(passwordService, userService)
|
|
|
|
m.menuRouteController = controller.NewMenuRouteController(menuRouteService, logger)
|
|
|
|
m.frontendRouteController = controller.NewFrontendRouteController(frontendRouteService, logger)
|
|
|
|
|
|
|
|
log.Printf("✅ 认证模块初始化完成")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start 启动模块
|
|
|
|
func (m *Module) Start(config *config.Config, db *gorm.DB) error {
|
|
|
|
// 这个方法在新接口中不再需要,保留是为了兼容性
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop 停止模块
|
|
|
|
func (m *Module) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup 清理模块资源
|
|
|
|
func (m *Module) Cleanup() {
|
|
|
|
// 清理资源
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterRoutes 注册路由
|
|
|
|
func (m *Module) RegisterRoutes(router *gin.RouterGroup) {
|
|
|
|
// 注册认证相关路由
|
|
|
|
// 从配置中获取JWT密钥,这里暂时使用默认值
|
|
|
|
jwtSecret := "your-jwt-secret" // 应该从配置中获取
|
|
|
|
routes.RegisterAuthRoutes(router, m.db, jwtSecret)
|
|
|
|
|
|
|
|
// 注册菜单路由关联表路由
|
|
|
|
routes.RegisterMenuRouteRoutes(router, m.menuRouteController)
|
|
|
|
|
|
|
|
// 注册路由同步路由
|
|
|
|
routes.RegisterRouteSyncRoutes(router, m.db, m.logger)
|
|
|
|
|
|
|
|
// 注册前台路由路由
|
|
|
|
routes.RegisterFrontendRouteRoutes(router, m.db, m.logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SyncRoutes 同步路由信息
|
|
|
|
func (m *Module) SyncRoutes(router *gin.Engine) error {
|
|
|
|
if m.routeSyncService != nil {
|
|
|
|
return m.routeSyncService.SyncRoutes(router)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// init 函数,在包导入时自动执行
|
|
|
|
func init() {
|
|
|
|
// 注册模块类型到核心模块发现系统
|
|
|
|
core.RegisterModuleType(&Module{})
|
|
|
|
}
|