This commit is contained in:
2025-12-09 18:55:28 +08:00
parent 8d00d67540
commit c23ab8338b
209 changed files with 5445 additions and 3963 deletions

View File

@@ -1,16 +1,17 @@
package service
import (
"context"
"fmt"
"regexp"
"strings"
"context"
"fmt"
"regexp"
"strings"
"ycc-server/app/main/model"
"ycc-server/app/main/model"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest"
"github.com/google/uuid"
)
type ApiRegistryService struct {
@@ -26,27 +27,27 @@ func NewApiRegistryService(adminApiModel model.AdminApiModel) *ApiRegistryServic
// RegisterAllApis 自动注册所有API到数据库
func (s *ApiRegistryService) RegisterAllApis(ctx context.Context, routes []rest.Route) error {
logx.Infof("开始注册API共 %d 个路由", len(routes))
registeredCount := 0
skippedCount := 0
for _, route := range routes {
// 跳过不需要权限控制的API
if s.shouldSkipApi(route.Path) {
skippedCount++
continue
}
// 解析API信息
apiInfo := s.parseRouteToApi(route)
// 检查是否已存在
existing, err := s.adminApiModel.FindOneByApiCode(ctx, apiInfo.ApiCode)
if err != nil && !errors.Is(err, model.ErrNotFound) {
logx.Errorf("查询API失败: %v, apiCode: %s", err, apiInfo.ApiCode)
continue
}
// 如果不存在则插入
if existing == nil {
_, err = s.adminApiModel.Insert(ctx, nil, apiInfo)
@@ -63,7 +64,7 @@ func (s *ApiRegistryService) RegisterAllApis(ctx context.Context, routes []rest.
existing.Method = apiInfo.Method
existing.Url = apiInfo.Url
existing.Description = apiInfo.Description
_, err = s.adminApiModel.Update(ctx, nil, existing)
if err != nil {
logx.Errorf("更新API失败: %v, apiCode: %s", err, apiInfo.ApiCode)
@@ -73,7 +74,7 @@ func (s *ApiRegistryService) RegisterAllApis(ctx context.Context, routes []rest.
}
}
}
logx.Infof("API注册完成新增: %d, 跳过: %d", registeredCount, skippedCount)
return nil
}
@@ -94,13 +95,13 @@ func (s *ApiRegistryService) shouldSkipApi(path string) bool {
"/api/v1/authorization/", // 授权接口
"/health", // 健康检查
}
for _, skipPath := range skipPaths {
if strings.HasPrefix(path, skipPath) {
return true
}
}
return false
}
@@ -108,33 +109,34 @@ func (s *ApiRegistryService) shouldSkipApi(path string) bool {
func (s *ApiRegistryService) parseRouteToApi(route rest.Route) *model.AdminApi {
// 生成API编码
apiCode := s.generateApiCode(route.Method, route.Path)
// 生成API名称
apiName := s.generateApiName(route.Path)
// 生成描述
description := s.generateDescription(route.Method, route.Path)
return &model.AdminApi{
ApiName: apiName,
ApiCode: apiCode,
Method: route.Method,
Url: route.Path,
Status: 1, // 默认启用
Description: description,
}
return &model.AdminApi{
Id: uuid.NewString(),
ApiName: apiName,
ApiCode: apiCode,
Method: route.Method,
Url: route.Path,
Status: 1, // 默认启用
Description: description,
}
}
// generateApiCode 生成API编码
func (s *ApiRegistryService) generateApiCode(method, path string) string {
// 移除路径参数,如 :id
cleanPath := regexp.MustCompile(`/:[\w]+`).ReplaceAllString(path, "")
// 转换为小写并替换特殊字符
apiCode := strings.ToLower(method) + "_" + strings.ReplaceAll(cleanPath, "/", "_")
apiCode = strings.TrimPrefix(apiCode, "_")
apiCode = strings.TrimSuffix(apiCode, "_")
return apiCode
}
@@ -145,52 +147,52 @@ func (s *ApiRegistryService) generateApiName(path string) string {
if len(parts) < 3 {
return path
}
// 获取模块名和操作名
module := parts[len(parts)-2]
action := parts[len(parts)-1]
// 转换为中文描述
moduleMap := map[string]string{
"agent": "代理管理",
"auth": "认证管理",
"feature": "功能管理",
"menu": "菜单管理",
"notification": "通知管理",
"order": "订单管理",
"agent": "代理管理",
"auth": "认证管理",
"feature": "功能管理",
"menu": "菜单管理",
"notification": "通知管理",
"order": "订单管理",
"platform_user": "平台用户",
"product": "产品管理",
"query": "查询管理",
"role": "角色管理",
"user": "用户管理",
"product": "产品管理",
"query": "查询管理",
"role": "角色管理",
"user": "用户管理",
}
actionMap := map[string]string{
"list": "列表",
"create": "创建",
"update": "更新",
"delete": "删除",
"detail": "详情",
"login": "登录",
"config": "配置",
"example": "示例",
"refund": "退款",
"link": "链接",
"stats": "统计",
"cleanup": "清理",
"record": "记录",
"list": "列表",
"create": "创建",
"update": "更新",
"delete": "删除",
"detail": "详情",
"login": "登录",
"config": "配置",
"example": "示例",
"refund": "退款",
"link": "链接",
"stats": "统计",
"cleanup": "清理",
"record": "记录",
}
moduleName := moduleMap[module]
if moduleName == "" {
moduleName = module
}
actionName := actionMap[action]
if actionName == "" {
actionName = action
}
return fmt.Sprintf("%s-%s", moduleName, actionName)
}
@@ -202,14 +204,14 @@ func (s *ApiRegistryService) generateDescription(method, path string) string {
"PUT": "更新",
"DELETE": "删除",
}
methodDesc := methodMap[method]
if methodDesc == "" {
methodDesc = method
}
apiName := s.generateApiName(path)
return fmt.Sprintf("%s%s", methodDesc, apiName)
}