fix
This commit is contained in:
@@ -5,8 +5,8 @@ import (
|
||||
"database/sql"
|
||||
"time"
|
||||
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/common/xerr"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/xerr"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -2,9 +2,9 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/pkg/lzkit/lzUtils"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
@@ -3,11 +3,11 @@ package service
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/pkg/lzkit/lzUtils"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/pkg/lzkit/lzUtils"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
|
||||
223
app/main/api/internal/service/apiRegistryService.go
Normal file
223
app/main/api/internal/service/apiRegistryService.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"tydata-server/app/main/model"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type ApiRegistryService struct {
|
||||
adminApiModel model.AdminApiModel
|
||||
}
|
||||
|
||||
func NewApiRegistryService(adminApiModel model.AdminApiModel) *ApiRegistryService {
|
||||
return &ApiRegistryService{
|
||||
adminApiModel: adminApiModel,
|
||||
}
|
||||
}
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
logx.Errorf("插入API失败: %v, apiCode: %s", err, apiInfo.ApiCode)
|
||||
continue
|
||||
}
|
||||
registeredCount++
|
||||
logx.Infof("注册API成功: %s %s", apiInfo.Method, apiInfo.Url)
|
||||
} else {
|
||||
// 如果存在但信息有变化,则更新
|
||||
if s.shouldUpdateApi(existing, apiInfo) {
|
||||
existing.ApiName = apiInfo.ApiName
|
||||
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)
|
||||
continue
|
||||
}
|
||||
logx.Infof("更新API成功: %s %s", apiInfo.Method, apiInfo.Url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logx.Infof("API注册完成,新增: %d, 跳过: %d", registeredCount, skippedCount)
|
||||
return nil
|
||||
}
|
||||
|
||||
// shouldSkipApi 判断是否应该跳过此API
|
||||
func (s *ApiRegistryService) shouldSkipApi(path string) bool {
|
||||
// 跳过公开API
|
||||
skipPaths := []string{
|
||||
"/api/v1/admin/auth/login", // 登录接口
|
||||
"/api/v1/app/", // 前端应用接口
|
||||
"/api/v1/agent/", // 代理接口
|
||||
"/api/v1/user/", // 用户接口
|
||||
"/api/v1/auth/", // 认证接口
|
||||
"/api/v1/notification/", // 通知接口
|
||||
"/api/v1/pay/", // 支付接口
|
||||
"/api/v1/query/", // 查询接口
|
||||
"/api/v1/product/", // 产品接口
|
||||
"/api/v1/authorization/", // 授权接口
|
||||
"/health", // 健康检查
|
||||
}
|
||||
|
||||
for _, skipPath := range skipPaths {
|
||||
if strings.HasPrefix(path, skipPath) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// parseRouteToApi 将路由解析为API信息
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// generateApiName 生成API名称
|
||||
func (s *ApiRegistryService) generateApiName(path string) string {
|
||||
// 从路径中提取模块和操作
|
||||
parts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
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": "订单管理",
|
||||
"platform_user": "平台用户",
|
||||
"product": "产品管理",
|
||||
"promotion": "推广管理",
|
||||
"query": "查询管理",
|
||||
"role": "角色管理",
|
||||
"user": "用户管理",
|
||||
}
|
||||
|
||||
actionMap := map[string]string{
|
||||
"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)
|
||||
}
|
||||
|
||||
// generateDescription 生成API描述
|
||||
func (s *ApiRegistryService) generateDescription(method, path string) string {
|
||||
methodMap := map[string]string{
|
||||
"GET": "查询",
|
||||
"POST": "创建",
|
||||
"PUT": "更新",
|
||||
"DELETE": "删除",
|
||||
}
|
||||
|
||||
methodDesc := methodMap[method]
|
||||
if methodDesc == "" {
|
||||
methodDesc = method
|
||||
}
|
||||
|
||||
apiName := s.generateApiName(path)
|
||||
|
||||
return fmt.Sprintf("%s%s", methodDesc, apiName)
|
||||
}
|
||||
|
||||
// shouldUpdateApi 判断是否需要更新API
|
||||
func (s *ApiRegistryService) shouldUpdateApi(existing, new *model.AdminApi) bool {
|
||||
return existing.ApiName != new.ApiName ||
|
||||
existing.Method != new.Method ||
|
||||
existing.Url != new.Url ||
|
||||
existing.Description != new.Description
|
||||
}
|
||||
@@ -5,14 +5,14 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
tianyuanapi "hm-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"hm-server/app/main/model"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
tianyuanapi "tydata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"tydata-server/app/main/model"
|
||||
|
||||
"github.com/Masterminds/squirrel"
|
||||
"github.com/tidwall/gjson"
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"crypto/ecdsa"
|
||||
"crypto/x509"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"encoding/json"
|
||||
"encoding/pem"
|
||||
"fmt"
|
||||
@@ -11,7 +12,6 @@ import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
|
||||
"github.com/golang-jwt/jwt/v4"
|
||||
)
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/api/internal/types"
|
||||
"encoding/json"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/api/internal/types"
|
||||
|
||||
"github.com/hibiken/asynq"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
|
||||
@@ -3,10 +3,10 @@ package service
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -2,11 +2,11 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
@@ -2,8 +2,8 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tydata-server/app/main/model"
|
||||
"errors"
|
||||
"hm-server/app/main/model"
|
||||
)
|
||||
|
||||
type DictService struct {
|
||||
|
||||
@@ -2,12 +2,12 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/ctxdata"
|
||||
jwtx "tydata-server/common/jwt"
|
||||
"tydata-server/common/xerr"
|
||||
"database/sql"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/common/ctxdata"
|
||||
jwtx "hm-server/common/jwt"
|
||||
"hm-server/common/xerr"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
tianyuanapi "tydata-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
tianyuanapi "hm-server/app/main/api/internal/service/tianyuanapi_sdk"
|
||||
|
||||
"github.com/tidwall/gjson"
|
||||
)
|
||||
|
||||
@@ -2,14 +2,14 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"tydata-server/app/main/api/internal/config"
|
||||
"tydata-server/app/main/model"
|
||||
"tydata-server/common/ctxdata"
|
||||
"tydata-server/pkg/lzkit/lzUtils"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
"hm-server/app/main/api/internal/config"
|
||||
"hm-server/app/main/model"
|
||||
"hm-server/common/ctxdata"
|
||||
"hm-server/pkg/lzkit/lzUtils"
|
||||
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||||
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
|
||||
|
||||
Reference in New Issue
Block a user