You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
5.9 KiB
187 lines
5.9 KiB
1 week ago
|
package controller
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
|
||
|
"gofaster/internal/auth/model"
|
||
|
"gofaster/internal/auth/service"
|
||
|
"gofaster/internal/shared/response"
|
||
|
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"go.uber.org/zap"
|
||
|
)
|
||
|
|
||
|
// RouteSyncController 路由同步控制器
|
||
|
type RouteSyncController struct {
|
||
|
routeSyncService *service.RouteSyncService
|
||
|
log *zap.Logger
|
||
|
}
|
||
|
|
||
|
// NewRouteSyncController 创建路由同步控制器
|
||
|
func NewRouteSyncController(routeSyncService *service.RouteSyncService, log *zap.Logger) *RouteSyncController {
|
||
|
return &RouteSyncController{
|
||
|
routeSyncService: routeSyncService,
|
||
|
log: log,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// SyncFrontendRoute 同步前端路由映射
|
||
|
// @Summary 同步前端路由映射
|
||
|
// @Description 接收前端路由信息并同步到数据库
|
||
|
// @Tags 路由同步
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param routeMapping body model.RouteMapping true "路由映射信息"
|
||
|
// @Success 200 {object} response.Response{data=model.RouteMapping}
|
||
|
// @Failure 400 {object} response.Response
|
||
|
// @Router /api/route-mappings/sync [post]
|
||
|
func (c *RouteSyncController) SyncFrontendRoute(ctx *gin.Context) {
|
||
|
var routeMapping model.RouteMapping
|
||
|
if err := ctx.ShouldBindJSON(&routeMapping); err != nil {
|
||
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 验证必填字段
|
||
|
if routeMapping.FrontendRoute == "" {
|
||
|
response.Error(ctx, http.StatusBadRequest, "前端路由不能为空", "")
|
||
|
return
|
||
|
}
|
||
|
if routeMapping.BackendRoute == "" {
|
||
|
response.Error(ctx, http.StatusBadRequest, "后端路由不能为空", "")
|
||
|
return
|
||
|
}
|
||
|
if routeMapping.HTTPMethod == "" {
|
||
|
response.Error(ctx, http.StatusBadRequest, "HTTP方法不能为空", "")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 设置默认值
|
||
|
if routeMapping.Module == "" {
|
||
|
routeMapping.Module = "unknown"
|
||
|
}
|
||
|
if routeMapping.Description == "" {
|
||
|
routeMapping.Description = "前端路由映射"
|
||
|
}
|
||
|
if routeMapping.AuthGroup == "" {
|
||
|
// 根据HTTP方法设置权限分组
|
||
|
editMethods := []string{"POST", "PUT", "PATCH", "DELETE"}
|
||
|
routeMapping.AuthGroup = "Read"
|
||
|
for _, method := range editMethods {
|
||
|
if routeMapping.HTTPMethod == method {
|
||
|
routeMapping.AuthGroup = "Edit"
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if routeMapping.Status == 0 {
|
||
|
routeMapping.Status = 1
|
||
|
}
|
||
|
|
||
|
// 同步到数据库
|
||
|
if err := c.routeSyncService.SyncFrontendRoute(&routeMapping); err != nil {
|
||
|
c.log.Error("同步前端路由失败",
|
||
|
zap.String("frontendRoute", routeMapping.FrontendRoute),
|
||
|
zap.String("backendRoute", routeMapping.BackendRoute),
|
||
|
zap.Error(err))
|
||
|
response.Error(ctx, http.StatusInternalServerError, "同步前端路由失败", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
c.log.Info("前端路由同步成功",
|
||
|
zap.String("frontendRoute", routeMapping.FrontendRoute),
|
||
|
zap.String("backendRoute", routeMapping.BackendRoute),
|
||
|
zap.String("module", routeMapping.Module))
|
||
|
|
||
|
response.Success(ctx, "前端路由同步成功", routeMapping)
|
||
|
}
|
||
|
|
||
|
// BatchSyncFrontendRoutes 批量同步前端路由
|
||
|
// @Summary 批量同步前端路由
|
||
|
// @Description 批量接收前端路由信息并同步到数据库
|
||
|
// @Tags 路由同步
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param routeMappings body []model.RouteMapping true "路由映射信息列表"
|
||
|
// @Success 200 {object} response.Response{data=map[string]interface{}}
|
||
|
// @Failure 400 {object} response.Response
|
||
|
// @Router /api/route-mappings/batch-sync [post]
|
||
|
func (c *RouteSyncController) BatchSyncFrontendRoutes(ctx *gin.Context) {
|
||
|
var routeMappings []model.RouteMapping
|
||
|
if err := ctx.ShouldBindJSON(&routeMappings); err != nil {
|
||
|
response.Error(ctx, http.StatusBadRequest, "请求参数错误", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if len(routeMappings) == 0 {
|
||
|
response.Error(ctx, http.StatusBadRequest, "路由映射列表不能为空", "")
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// 批量同步
|
||
|
successCount, errorCount, errors := c.routeSyncService.BatchSyncFrontendRoutes(routeMappings)
|
||
|
|
||
|
result := map[string]interface{}{
|
||
|
"total": len(routeMappings),
|
||
|
"successCount": successCount,
|
||
|
"errorCount": errorCount,
|
||
|
"errors": errors,
|
||
|
}
|
||
|
|
||
|
if errorCount > 0 {
|
||
|
c.log.Warn("批量同步前端路由完成,存在部分错误",
|
||
|
zap.Int("total", len(routeMappings)),
|
||
|
zap.Int("success", successCount),
|
||
|
zap.Int("errors", errorCount))
|
||
|
response.Success(ctx, "批量同步完成,存在部分错误", result)
|
||
|
} else {
|
||
|
c.log.Info("批量同步前端路由成功",
|
||
|
zap.Int("total", len(routeMappings)),
|
||
|
zap.Int("success", successCount))
|
||
|
response.Success(ctx, "批量同步前端路由成功", result)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// GetSyncStatus 获取同步状态
|
||
|
// @Summary 获取同步状态
|
||
|
// @Description 获取路由同步的状态信息
|
||
|
// @Tags 路由同步
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Success 200 {object} response.Response{data=map[string]interface{}}
|
||
|
// @Failure 400 {object} response.Response
|
||
|
// @Router /api/route-mappings/sync-status [get]
|
||
|
func (c *RouteSyncController) GetSyncStatus(ctx *gin.Context) {
|
||
|
status, err := c.routeSyncService.GetSyncStatus()
|
||
|
if err != nil {
|
||
|
response.Error(ctx, http.StatusInternalServerError, "获取同步状态失败", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.Success(ctx, "获取同步状态成功", status)
|
||
|
}
|
||
|
|
||
|
// GetFrontendRoutes 获取前端路由列表
|
||
|
// @Summary 获取前端路由列表
|
||
|
// @Description 获取所有前端路由映射信息
|
||
|
// @Tags 路由同步
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param module query string false "模块名称"
|
||
|
// @Param authGroup query string false "权限分组"
|
||
|
// @Success 200 {object} response.Response{data=[]model.RouteMapping}
|
||
|
// @Failure 400 {object} response.Response
|
||
|
// @Router /api/route-mappings/frontend [get]
|
||
|
func (c *RouteSyncController) GetFrontendRoutes(ctx *gin.Context) {
|
||
|
module := ctx.Query("module")
|
||
|
authGroup := ctx.Query("authGroup")
|
||
|
|
||
|
routes, err := c.routeSyncService.GetFrontendRoutes(module, authGroup)
|
||
|
if err != nil {
|
||
|
response.Error(ctx, http.StatusInternalServerError, "获取前端路由失败", err.Error())
|
||
|
return
|
||
|
}
|
||
|
|
||
|
response.Success(ctx, "获取前端路由成功", routes)
|
||
|
}
|