132 lines
3.4 KiB
Go
132 lines
3.4 KiB
Go
package sync
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
dao "dataengine/dao/dict"
|
|
dto "dataengine/model/dto/dict"
|
|
entity "dataengine/model/entity/dict"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// PlatformConfig 运行时平台配置
|
|
type PlatformConfig struct {
|
|
*entity.DatasourcePlatform
|
|
AccessToken string
|
|
AppKey string
|
|
AppSecret string
|
|
}
|
|
|
|
// GetApiUrl 拼接完整 API URL
|
|
func (c *PlatformConfig) GetApiUrl(apiPath string) string {
|
|
if c.ApiBaseUrl == "" {
|
|
return apiPath
|
|
}
|
|
return c.ApiBaseUrl + apiPath
|
|
}
|
|
|
|
// PlatformManager 平台配置管理器
|
|
type PlatformManager struct{}
|
|
|
|
// GetPlatform 根据平台编码获取配置
|
|
func (m *PlatformManager) GetPlatform(ctx context.Context, platformCode string) (*PlatformConfig, error) {
|
|
platform, err := dao.DatasourcePlatform.GetByPlatformCode(ctx, platformCode)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("查询平台配置失败 [%s]: %w", platformCode, err)
|
|
}
|
|
if platform == nil {
|
|
return nil, fmt.Errorf("平台不存在 [%s]", platformCode)
|
|
}
|
|
if platform.Status != "ACTIVE" {
|
|
return nil, fmt.Errorf("平台 [%s] 未启用", platformCode)
|
|
}
|
|
|
|
cfg := &PlatformConfig{DatasourcePlatform: platform}
|
|
|
|
switch platform.AuthType {
|
|
case "TOKEN":
|
|
cfg.AccessToken = platform.Token
|
|
case "OAUTH2":
|
|
if platform.Token != "" {
|
|
cfg.AccessToken = platform.Token
|
|
}
|
|
case "API_KEY":
|
|
cfg.AccessToken = platform.ApiKey
|
|
case "SIGN":
|
|
if platform.AuthConfig != nil {
|
|
if ak, _ := platform.AuthConfig["app_key"].(string); ak != "" {
|
|
cfg.AppKey = ak
|
|
}
|
|
if as, _ := platform.AuthConfig["app_secret"].(string); as != "" {
|
|
cfg.AppSecret = as
|
|
}
|
|
}
|
|
case "APP_SIGNATURE":
|
|
if platform.AuthConfig != nil {
|
|
if aid, _ := platform.AuthConfig["app_id"].(string); aid != "" {
|
|
cfg.AppKey = aid
|
|
}
|
|
if as, _ := platform.AuthConfig["app_secret"].(string); as != "" {
|
|
cfg.AppSecret = as
|
|
}
|
|
}
|
|
default:
|
|
logrus.Warnf("平台 %s 认证类型 %s 未处理", platformCode, platform.AuthType)
|
|
}
|
|
|
|
return cfg, nil
|
|
}
|
|
|
|
// GetInterfaces 获取平台下的活跃接口列表
|
|
func (m *PlatformManager) GetInterfaces(ctx context.Context, platformId int64) ([]entity.ApiInterface, error) {
|
|
interfaces, _, err := dao.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
|
|
PlatformId: platformId,
|
|
Status: "active",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return interfaces, nil
|
|
}
|
|
|
|
// GetInterfaceByCode 根据编码获取接口定义
|
|
func (m *PlatformManager) GetInterfaceByCode(ctx context.Context, platformId int64, code string) (*entity.ApiInterface, error) {
|
|
all, _, err := dao.ApiInterface.List(ctx, &dto.ListApiInterfaceReq{
|
|
PlatformId: platformId,
|
|
Code: code,
|
|
Status: "active",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(all) == 0 {
|
|
return nil, fmt.Errorf("未找到接口 [code=%s]", code)
|
|
}
|
|
return &all[0], nil
|
|
}
|
|
|
|
// GetPlatformWithInterfaces 获取平台及所有接口
|
|
func (m *PlatformManager) GetPlatformWithInterfaces(ctx context.Context, platformCode string) (*PlatformConfig, []entity.ApiInterface, error) {
|
|
cfg, err := m.GetPlatform(ctx, platformCode)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
interfaces, err := m.GetInterfaces(ctx, cfg.ID)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return cfg, interfaces, nil
|
|
}
|
|
|
|
// FindInterfaceUrl 在接口列表中查找指定编码的 URL
|
|
func FindInterfaceUrl(ifaces []entity.ApiInterface, code string) string {
|
|
for _, iface := range ifaces {
|
|
if iface.Code == code {
|
|
return iface.Url
|
|
}
|
|
}
|
|
return ""
|
|
}
|