deploy-ease-platform/frontend/src/pages/Workflow/Design/utils/dataSourceLoader.ts
dengqichen fc1d7da919 1
2025-10-23 12:39:43 +08:00

182 lines
5.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from '@/utils/request';
/**
* 数据源类型枚举
*/
export enum DataSourceType {
JENKINS_SERVERS = 'JENKINS_SERVERS',
K8S_CLUSTERS = 'K8S_CLUSTERS',
GIT_REPOSITORIES = 'GIT_REPOSITORIES',
DOCKER_REGISTRIES = 'DOCKER_REGISTRIES',
NOTIFICATION_CHANNEL_TYPES = 'NOTIFICATION_CHANNEL_TYPES',
NOTIFICATION_CHANNELS = 'NOTIFICATION_CHANNELS',
USERS = 'USERS',
ROLES = 'ROLES',
DEPARTMENTS = 'DEPARTMENTS'
}
/**
* 数据源配置接口
*/
export interface DataSourceConfig {
url: string;
params?: Record<string, any>;
transform: (data: any) => Array<{ label: string; value: any; [key: string]: any }>;
}
/**
* 数据源选项接口
*/
export interface DataSourceOption {
label: string;
value: any;
[key: string]: any;
}
/**
* 数据源配置注册表
*/
export const DATA_SOURCE_REGISTRY: Record<DataSourceType, DataSourceConfig> = {
[DataSourceType.JENKINS_SERVERS]: {
url: '/api/v1/external-system/list',
params: {type: 'JENKINS', enabled: true},
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.name} (${item.url})`,
value: item.id,
url: item.url,
name: item.name
}));
}
},
[DataSourceType.NOTIFICATION_CHANNEL_TYPES]: {
url: '/api/v1/notification-channel/types',
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.label}`,
value: item.code
}));
}
},
[DataSourceType.NOTIFICATION_CHANNELS]: {
url: '/api/v1/notification-channel/list',
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `(${item.channelType})-${item.name}`,
value: item.id
}));
}
},
[DataSourceType.K8S_CLUSTERS]: {
url: '/api/v1/k8s-cluster/list',
params: {enabled: true},
transform: (data: any[]) => {
return data.map((item: any) => ({
label: item.name,
value: item.id,
apiServer: item.apiServer
}));
}
},
[DataSourceType.GIT_REPOSITORIES]: {
url: '/api/v1/git-repo/list',
params: {enabled: true},
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.name} (${item.url})`,
value: item.id,
url: item.url
}));
}
},
[DataSourceType.DOCKER_REGISTRIES]: {
url: '/api/v1/docker-registry/list',
params: {enabled: true},
transform: (data: any[]) => {
return data.map((item: any) => ({
label: item.name,
value: item.id,
url: item.url
}));
}
},
[DataSourceType.USERS]: {
url: '/api/v1/user/list',
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.nickname} (${item.username})`,
value: item.username, // 后台使用 username 进行审批
id: item.id,
email: item.email,
departmentName: item.departmentName
}));
}
},
[DataSourceType.ROLES]: {
url: '/api/v1/role/list',
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.name} (${item.code})`,
value: item.code, // 使用 code 作为值
id: item.id,
description: item.description
}));
}
},
[DataSourceType.DEPARTMENTS]: {
url: '/api/v1/department/list',
transform: (data: any[]) => {
return data.map((item: any) => ({
label: `${item.name} (${item.code})`,
value: item.code, // 使用 code 作为值
id: item.id,
description: item.description,
parentId: item.parentId
}));
}
}
};
/**
* 加载数据源
* @param type 数据源类型
* @returns 选项列表
*/
export const loadDataSource = async (type: DataSourceType): Promise<DataSourceOption[]> => {
const config = DATA_SOURCE_REGISTRY[type];
if (!config) {
console.error(`数据源类型 ${type} 未配置`);
return [];
}
try {
const response = await request.get(config.url, {params: config.params});
// request 拦截器已经提取了 data 字段response 直接是数组
return config.transform(response || []);
} catch (error) {
console.error(`加载数据源 ${type} 失败:`, error);
return [];
}
};
/**
* 批量加载多个数据源(用于预加载)
* @param types 数据源类型列表
* @returns 数据源数据映射
*/
export const loadMultipleDataSources = async (
types: DataSourceType[]
): Promise<Record<DataSourceType, DataSourceOption[]>> => {
const results = await Promise.all(
types.map(type => loadDataSource(type))
);
return types.reduce((acc, type, index) => {
acc[type] = results[index];
return acc;
}, {} as Record<DataSourceType, DataSourceOption[]>);
};