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.
220 lines
6.6 KiB
220 lines
6.6 KiB
3 days ago
|
# 路由同步功能修改总结
|
||
|
|
||
|
## 修改概述
|
||
|
|
||
|
根据需求,对route_mappings表和相关程序逻辑进行了以下三个主要修改:
|
||
|
|
||
|
1. 删除route_mappings表中的`created_at`、`frontend_route`、`auth_group`字段及对应的取值写入逻辑
|
||
|
2. 将route_mappings表中`backend_route`字段的变量格式从冒号开头(`:var`)改写为大括弧包围(`{var}`)
|
||
|
3. 将`description`字段设置值为controller中swagger注释`@Summary`
|
||
|
|
||
|
## 详细修改内容
|
||
|
|
||
|
### 1. 数据库模型修改
|
||
|
|
||
|
**文件:** `backend/internal/auth/model/route_mapping.go`
|
||
|
|
||
|
**修改前:**
|
||
|
```go
|
||
|
type RouteMapping struct {
|
||
|
ID uint `gorm:"primarykey" json:"id"`
|
||
|
CreatedAt time.Time `json:"created_at"`
|
||
|
UpdatedAt time.Time `json:"updated_at"`
|
||
|
FrontendRoute string `json:"frontend_route"`
|
||
|
BackendRoute string `json:"backend_route"`
|
||
|
HTTPMethod string `json:"http_method"`
|
||
|
AuthGroup string `json:"auth_group"`
|
||
|
ResourceID *uint `json:"resource_id"`
|
||
|
Module string `json:"module"`
|
||
|
Description string `json:"description"`
|
||
|
Status int `gorm:"default:1" json:"status"`
|
||
|
}
|
||
|
```
|
||
|
|
||
|
**修改后:**
|
||
|
```go
|
||
|
type RouteMapping struct {
|
||
|
ID uint `gorm:"primarykey" json:"id"`
|
||
|
UpdatedAt time.Time `json:"updated_at"`
|
||
|
BackendRoute string `json:"backend_route"`
|
||
|
HTTPMethod string `json:"http_method"`
|
||
|
ResourceID *uint `json:"resource_id"`
|
||
|
Module string `json:"module"`
|
||
|
Description string `json:"description"`
|
||
|
Status int `gorm:"default:1" json:"status"`
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 2. 路径格式转换功能
|
||
|
|
||
|
**文件:** `backend/internal/auth/service/route_sync_service.go`
|
||
|
|
||
|
**新增方法:**
|
||
|
```go
|
||
|
// convertPathFormat 转换路径格式:将:var格式改为{var}格式
|
||
|
func (s *RouteSyncService) convertPathFormat(path string) string {
|
||
|
// 使用正则表达式替换:var格式为{var}格式
|
||
|
re := regexp.MustCompile(`:([a-zA-Z0-9_]+)`)
|
||
|
return re.ReplaceAllString(path, "{$1}")
|
||
|
}
|
||
|
```
|
||
|
|
||
|
**转换示例:**
|
||
|
- `/api/auth/users/:id` → `/api/auth/users/{id}`
|
||
|
- `/api/auth/roles/:roleId` → `/api/auth/roles/{roleId}`
|
||
|
- `/api/auth/permissions/:permissionId/assign` → `/api/auth/permissions/{permissionId}/assign`
|
||
|
|
||
|
### 3. Swagger注释解析功能
|
||
|
|
||
|
**新增文件:** `backend/internal/auth/service/swagger_parser.go`
|
||
|
|
||
|
**主要功能:**
|
||
|
- 解析Controller文件中的Swagger注释
|
||
|
- 提取`@Summary`、`@Description`、`@Tags`、`@Router`等信息
|
||
|
- 支持路径参数匹配(如`:id`参数)
|
||
|
|
||
|
**集成到RouteSyncService:**
|
||
|
```go
|
||
|
type RouteSyncService struct {
|
||
|
routeMappingRepo *repository.RouteMappingRepository
|
||
|
resourceRepo repository.ResourceRepository
|
||
|
log *zap.Logger
|
||
|
swaggerParser *SwaggerParser // 新增
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 4. 描述生成逻辑优化
|
||
|
|
||
|
**修改方法:** `generateDescription`
|
||
|
|
||
|
**修改前:** 基于HTTP方法和路径自动生成简单描述
|
||
|
**修改后:** 优先从Swagger注释中获取`@Summary`,回退到原有逻辑
|
||
|
|
||
|
```go
|
||
|
func (s *RouteSyncService) generateDescription(method, path string) string {
|
||
|
// 优先从Swagger注释中获取@Summary
|
||
|
if s.swaggerParser != nil {
|
||
|
summary := s.swaggerParser.GetSummary(method, path)
|
||
|
if summary != "" {
|
||
|
return summary
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 回退到原有逻辑
|
||
|
// ...
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 5. 数据库迁移
|
||
|
|
||
|
**新增文件:** `backend/internal/auth/migration/remove_unused_fields.go`
|
||
|
|
||
|
**功能:** 删除不再使用的字段
|
||
|
```go
|
||
|
func RemoveUnusedFields(db *gorm.DB) error {
|
||
|
// 删除created_at字段
|
||
|
db.Exec("ALTER TABLE route_mappings DROP COLUMN IF EXISTS created_at")
|
||
|
|
||
|
// 删除frontend_route字段
|
||
|
db.Exec("ALTER TABLE route_mappings DROP COLUMN IF EXISTS frontend_route")
|
||
|
|
||
|
// 删除auth_group字段
|
||
|
db.Exec("ALTER TABLE route_mappings DROP COLUMN IF EXISTS auth_group")
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 6. 服务方法重构
|
||
|
|
||
|
**修改的方法:**
|
||
|
- `SyncFrontendRoute` → `SyncRouteMapping`
|
||
|
- `BatchSyncFrontendRoutes` → `BatchSyncRouteMappings`
|
||
|
- `GetFrontendRoutes` → `GetRouteMappings`
|
||
|
|
||
|
**删除的方法:**
|
||
|
- `getAuthGroupByMethod`(因为AuthGroup字段已删除)
|
||
|
|
||
|
## 使用示例
|
||
|
|
||
|
### Controller注释规范
|
||
|
```go
|
||
|
// Login 用户登录
|
||
|
// @Summary 用户登录
|
||
|
// @Description 用户登录接口,支持验证码验证和密码错误次数限制
|
||
|
// @Tags 认证
|
||
|
// @Accept json
|
||
|
// @Produce json
|
||
|
// @Param request body model.LoginRequest true "登录请求参数"
|
||
|
// @Success 200 {object} response.Response{data=model.LoginResponse} "登录成功"
|
||
|
// @Failure 400 {object} response.Response "请求参数错误"
|
||
|
// @Failure 401 {object} response.Response "认证失败"
|
||
|
// @Router /auth/login [post]
|
||
|
func (c *AuthController) Login(ctx *gin.Context) {
|
||
|
// 实现代码
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### 路由同步结果
|
||
|
- **路径转换:** `/api/auth/users/:id` → `/api/auth/users/{id}`
|
||
|
- **描述来源:** 从`@Summary 用户登录`获取
|
||
|
- **字段简化:** 只保留必要的字段
|
||
|
|
||
|
## 影响分析
|
||
|
|
||
|
### 正面影响
|
||
|
1. **数据一致性:** 路径格式统一使用`{var}`格式
|
||
|
2. **描述准确性:** 直接从Controller注释获取,更准确
|
||
|
3. **表结构简化:** 删除不必要的字段,减少存储空间
|
||
|
4. **维护性提升:** 描述与代码同步更新
|
||
|
|
||
|
### 注意事项
|
||
|
1. **数据库迁移:** 需要运行迁移脚本删除字段
|
||
|
2. **API兼容性:** 前端调用需要适配新的路径格式
|
||
|
3. **注释规范:** 所有Controller方法需要添加完整的Swagger注释
|
||
|
|
||
|
## 部署步骤
|
||
|
|
||
|
1. **运行数据库迁移:**
|
||
|
```bash
|
||
|
go run main.go # 自动执行迁移
|
||
|
```
|
||
|
|
||
|
2. **验证路由同步:**
|
||
|
- 启动应用
|
||
|
- 检查route_mappings表数据
|
||
|
- 验证路径格式和描述内容
|
||
|
|
||
|
3. **更新前端代码:**
|
||
|
- 适配新的路径格式(如果需要)
|
||
|
- 更新API调用逻辑
|
||
|
|
||
|
## 测试验证
|
||
|
|
||
|
### 路径格式转换测试
|
||
|
```go
|
||
|
// 测试用例
|
||
|
testCases := []struct{
|
||
|
input string
|
||
|
expected string
|
||
|
}{
|
||
|
{"/api/auth/users/:id", "/api/auth/users/{id}"},
|
||
|
{"/api/auth/roles/:roleId", "/api/auth/roles/{roleId}"},
|
||
|
{"/api/auth/permissions/:permissionId/assign", "/api/auth/permissions/{permissionId}/assign"},
|
||
|
}
|
||
|
```
|
||
|
|
||
|
### Swagger解析测试
|
||
|
- 验证能正确解析Controller注释
|
||
|
- 验证路径参数匹配功能
|
||
|
- 验证描述提取准确性
|
||
|
|
||
|
## 总结
|
||
|
|
||
|
本次修改成功实现了三个主要需求:
|
||
|
1. ✅ 删除了不必要的字段,简化了表结构
|
||
|
2. ✅ 统一了路径参数格式为`{var}`形式
|
||
|
3. ✅ 实现了从Swagger注释自动提取描述的功能
|
||
|
|
||
|
这些修改提高了系统的可维护性和数据一致性,为后续的功能扩展奠定了良好的基础。
|