Files
urbanLifeline/urbanLifelineWeb/packages/platform/src/config/index.ts

221 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-12-11 14:21:36 +08:00
/**
2026-01-02 14:56:14 +08:00
* @description
2025-12-20 15:14:44 +08:00
*
*
2026-01-02 14:56:14 +08:00
* 1. 使
2025-12-20 15:14:44 +08:00
* 2. window.APP_RUNTIME_CONFIG app-config.js
2026-01-02 14:56:14 +08:00
* 3. Docker部署 app-config.js
2025-12-11 14:21:36 +08:00
*/
2025-12-20 15:14:44 +08:00
// ============================================
// 类型定义
// ============================================
export interface AppRuntimeConfig {
env?: string;
api: {
baseUrl: string;
timeout: number;
};
baseUrl: string;
file: {
downloadUrl: string;
uploadUrl: string;
maxSize: {
image: number;
video: number;
document: number;
};
acceptTypes: {
image: string;
video: string;
document: string;
};
};
token: {
key: string;
refreshThreshold: number;
};
publicImgPath: string;
publicWebPath: string;
sso?: {
2026-01-02 14:56:14 +08:00
platformUrl: string;
workcaseUrl: string;
biddingUrl: string;
2025-12-20 15:14:44 +08:00
};
2026-01-02 14:56:14 +08:00
aesSecretKey?: string;
2025-12-20 15:14:44 +08:00
features?: {
enableDebug?: boolean;
enableMockData?: boolean;
[key: string]: any;
};
}
// ============================================
2026-01-02 14:56:14 +08:00
// 环境检测
2025-12-20 15:14:44 +08:00
// ============================================
const isDev = (import.meta as any).env?.DEV ?? false;
2026-01-02 14:56:14 +08:00
// ============================================
2025-12-20 15:14:44 +08:00
// 开发环境配置
2026-01-02 14:56:14 +08:00
// ============================================
2025-12-20 15:14:44 +08:00
const devConfig: AppRuntimeConfig = {
env: 'development',
api: {
baseUrl: '/api',
timeout: 30000
},
baseUrl: '/',
file: {
downloadUrl: '/api/urban-lifeline/file/download/',
uploadUrl: '/api/urban-lifeline/file/upload',
2026-01-02 14:56:14 +08:00
maxSize: { image: 5, video: 100, document: 10 },
2025-12-20 15:14:44 +08:00
acceptTypes: {
image: 'image/*',
video: 'video/*',
document: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx'
}
},
token: {
key: 'token',
refreshThreshold: 300000
},
2026-01-02 14:56:14 +08:00
publicImgPath: '/img',
publicWebPath: '/',
2025-12-20 15:14:44 +08:00
sso: {
2026-01-02 14:56:14 +08:00
platformUrl: '/',
workcaseUrl: '/workcase',
biddingUrl: '/bidding'
2025-12-20 15:14:44 +08:00
},
2026-01-02 14:56:14 +08:00
aesSecretKey: 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=',
2025-12-20 15:14:44 +08:00
features: {
enableDebug: true,
enableMockData: false
}
};
2026-01-02 14:56:14 +08:00
// ============================================
2025-12-20 15:14:44 +08:00
// 生产环境默认配置(兜底)
2026-01-02 14:56:14 +08:00
// ============================================
2025-12-20 15:14:44 +08:00
const prodDefaultConfig: AppRuntimeConfig = {
env: 'production',
api: {
baseUrl: '/api',
timeout: 30000
},
baseUrl: '/',
file: {
downloadUrl: '/api/urban-lifeline/file/download/',
uploadUrl: '/api/urban-lifeline/file/upload',
2026-01-02 14:56:14 +08:00
maxSize: { image: 5, video: 100, document: 10 },
2025-12-20 15:14:44 +08:00
acceptTypes: {
image: 'image/*',
video: 'video/*',
document: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx'
}
},
token: {
key: 'token',
refreshThreshold: 300000
},
publicImgPath: '/img',
publicWebPath: '/',
sso: {
platformUrl: '/',
workcaseUrl: '/workcase',
biddingUrl: '/bidding'
},
2026-01-02 14:56:14 +08:00
aesSecretKey: 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=',
2025-12-20 15:14:44 +08:00
features: {
enableDebug: false,
enableMockData: false
}
};
// ============================================
// 配置加载
// ============================================
2025-12-11 14:21:36 +08:00
2026-01-02 14:56:14 +08:00
/**
*
*/
const isPlaceholder = (value: any): boolean => {
return typeof value === 'string' && value.startsWith('__') && value.endsWith('__');
};
/**
*
*/
const mergeConfig = (target: any, source: any): any => {
const result = { ...target };
for (const key in source) {
const value = source[key];
if (value && typeof value === 'object' && !Array.isArray(value)) {
result[key] = mergeConfig(target[key] || {}, value);
} else if (!isPlaceholder(value)) {
result[key] = value;
}
}
return result;
};
2025-12-11 14:21:36 +08:00
/**
2025-12-20 15:14:44 +08:00
*
2025-12-11 14:21:36 +08:00
*/
2025-12-20 15:14:44 +08:00
const getRuntimeConfig = (): AppRuntimeConfig => {
if (isDev) {
2026-01-02 14:56:14 +08:00
console.log('[Config] 开发环境,使用内置配置');
2025-12-20 15:14:44 +08:00
return devConfig;
}
try {
const runtimeConfig = (window as any).APP_RUNTIME_CONFIG;
if (runtimeConfig && typeof runtimeConfig === 'object') {
2026-01-02 14:56:14 +08:00
// 合并配置,未替换的占位符使用默认值
const merged = mergeConfig(prodDefaultConfig, runtimeConfig);
console.log('[Config] 加载运行时配置', merged);
return merged;
2025-12-20 15:14:44 +08:00
}
} catch (e) {
2026-01-02 14:56:14 +08:00
console.warn('[Config] 无法读取外部配置', e);
2025-12-20 15:14:44 +08:00
}
2026-01-02 14:56:14 +08:00
console.log('[Config] 使用默认生产配置');
2025-12-20 15:14:44 +08:00
return prodDefaultConfig;
};
2026-01-02 14:56:14 +08:00
// 当前配置
2025-12-20 15:14:44 +08:00
const config = getRuntimeConfig();
// ============================================
2026-01-02 14:56:14 +08:00
// 导出
2025-12-20 15:14:44 +08:00
// ============================================
2026-01-02 14:56:14 +08:00
// AES 密钥
export const AES_SECRET_KEY = config.aesSecretKey || 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=';
// 常用配置项
2025-12-20 15:14:44 +08:00
export const API_BASE_URL = config.api.baseUrl;
export const FILE_DOWNLOAD_URL = config.file.downloadUrl;
2026-01-02 14:56:14 +08:00
export const FILE_UPLOAD_URL = config.file.uploadUrl;
2025-12-20 15:14:44 +08:00
export const PUBLIC_IMG_PATH = config.publicImgPath;
export const PUBLIC_WEB_PATH = config.publicWebPath;
2026-01-02 14:56:14 +08:00
// 完整配置对象
2025-12-11 14:21:36 +08:00
export const APP_CONFIG = {
2025-12-20 15:14:44 +08:00
title: '泰豪电源 AI 数智化平台',
name: '泰豪电源 AI 数智化平台',
version: '1.0.0',
copyright: '泰豪电源',
env: config.env || 'production',
baseUrl: config.baseUrl,
2026-01-02 14:56:14 +08:00
api: config.api,
file: config.file,
token: config.token,
2025-12-20 15:14:44 +08:00
publicImgPath: config.publicImgPath,
publicWebPath: config.publicWebPath,
2026-01-02 14:56:14 +08:00
sso: config.sso || { platformUrl: '/', workcaseUrl: '/workcase', biddingUrl: '/bidding' },
2025-12-20 15:14:44 +08:00
features: config.features || {}
};
export default APP_CONFIG;