227 lines
5.1 KiB
TypeScript
227 lines
5.1 KiB
TypeScript
/**
|
||
* @description 应用运行时配置
|
||
* @author yslg
|
||
* @since 2025-12-06
|
||
*
|
||
* 配置加载策略:
|
||
* 1. 开发环境:使用下面定义的开发配置
|
||
* 2. 生产环境:从 window.APP_RUNTIME_CONFIG 读取(来自 app-config.js)
|
||
* 3. Docker部署:替换 app-config.js 文件实现配置外挂
|
||
*
|
||
* 配置结构说明:
|
||
* 此文件的配置结构与 app-config.js 完全对应
|
||
* 修改 app-config.js 后,这里的配置会自动应用
|
||
*/
|
||
|
||
// ============================================
|
||
// 类型定义
|
||
// ============================================
|
||
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;
|
||
features?: {
|
||
enableDebug?: boolean;
|
||
enableMockData?: boolean;
|
||
[key: string]: any;
|
||
};
|
||
}
|
||
|
||
// ============================================
|
||
// 配置定义(与 app-config.js 结构一致)
|
||
// ============================================
|
||
const isDev = (import.meta as any).env?.DEV ?? false;
|
||
|
||
// 开发环境配置
|
||
const devConfig: AppRuntimeConfig = {
|
||
env: 'development',
|
||
|
||
api: {
|
||
// 开发环境通过 Vite 代理转发到后端,避免浏览器直接跨域
|
||
// 实际请求路径示例:/api/... → 由代理转发到实际后端
|
||
baseUrl: '/api',
|
||
timeout: 30000
|
||
},
|
||
|
||
baseUrl: '/',
|
||
|
||
file: {
|
||
// 同样走代理,保持与 api.baseUrl 一致
|
||
downloadUrl: '/api/file/download/',
|
||
uploadUrl: '/api/file/upload',
|
||
maxSize: {
|
||
image: 5,
|
||
video: 100,
|
||
document: 10
|
||
},
|
||
acceptTypes: {
|
||
image: 'image/*',
|
||
video: 'video/*',
|
||
document: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx'
|
||
}
|
||
},
|
||
|
||
token: {
|
||
key: 'token',
|
||
refreshThreshold: 300000
|
||
},
|
||
|
||
publicImgPath: 'http://localhost:5173/img',
|
||
publicWebPath: 'http://localhost:5173',
|
||
|
||
features: {
|
||
enableDebug: true,
|
||
enableMockData: false
|
||
}
|
||
};
|
||
|
||
// 生产环境默认配置(兜底)
|
||
const prodDefaultConfig: AppRuntimeConfig = {
|
||
env: 'production',
|
||
|
||
api: {
|
||
baseUrl: '/api',
|
||
timeout: 30000
|
||
},
|
||
|
||
baseUrl: '/',
|
||
|
||
file: {
|
||
downloadUrl: '/api/file/download/',
|
||
uploadUrl: '/api/file/upload',
|
||
maxSize: {
|
||
image: 5,
|
||
video: 100,
|
||
document: 10
|
||
},
|
||
acceptTypes: {
|
||
image: 'image/*',
|
||
video: 'video/*',
|
||
document: '.pdf,.doc,.docx,.xls,.xlsx,.ppt,.pptx'
|
||
}
|
||
},
|
||
|
||
token: {
|
||
key: 'token',
|
||
refreshThreshold: 300000
|
||
},
|
||
|
||
publicImgPath: '/img',
|
||
publicWebPath: '/',
|
||
|
||
features: {
|
||
enableDebug: false,
|
||
enableMockData: false
|
||
}
|
||
};
|
||
|
||
// ============================================
|
||
// 配置加载
|
||
// ============================================
|
||
|
||
/**
|
||
* 获取运行时配置
|
||
* 生产环境优先从 window.APP_RUNTIME_CONFIG 读取(app-config.js 注入)
|
||
*/
|
||
const getRuntimeConfig = (): AppRuntimeConfig => {
|
||
if (isDev) {
|
||
console.log('[配置] 开发环境,使用内置配置');
|
||
return devConfig;
|
||
}
|
||
|
||
// 生产环境:尝试读取外部配置
|
||
try {
|
||
const runtimeConfig = (window as any).APP_RUNTIME_CONFIG;
|
||
if (runtimeConfig && typeof runtimeConfig === 'object') {
|
||
console.log('[配置] 加载外部配置 app-config.js');
|
||
console.log('[配置] API地址:', runtimeConfig.api?.baseUrl);
|
||
console.log('[配置] 环境:', runtimeConfig.env || 'production');
|
||
return runtimeConfig as AppRuntimeConfig;
|
||
}
|
||
} catch (e) {
|
||
console.warn('[配置] 无法读取外部配置,使用默认配置', e);
|
||
}
|
||
|
||
console.log('[配置] 使用默认生产配置');
|
||
return prodDefaultConfig;
|
||
};
|
||
|
||
// 当前应用配置
|
||
const config = getRuntimeConfig();
|
||
console.log('[配置] 当前配置', config);
|
||
|
||
// ============================================
|
||
// 导出配置(向后兼容)
|
||
// ============================================
|
||
|
||
// 单独导出常用配置项
|
||
export const API_BASE_URL = config.api.baseUrl;
|
||
export const FILE_DOWNLOAD_URL = config.file.downloadUrl;
|
||
export const PUBLIC_IMG_PATH = config.publicImgPath;
|
||
export const PUBLIC_WEB_PATH = config.publicWebPath;
|
||
|
||
// 导出完整配置对象
|
||
export const APP_CONFIG = {
|
||
// 应用标题
|
||
title: '泰豪电源 AI 数智化平台',
|
||
|
||
// 环境标识
|
||
env: config.env || 'production',
|
||
|
||
// 应用基础路径
|
||
baseUrl: config.baseUrl,
|
||
|
||
// API 配置
|
||
api: {
|
||
baseUrl: config.api.baseUrl,
|
||
timeout: config.api.timeout
|
||
},
|
||
|
||
// 文件配置
|
||
file: {
|
||
downloadUrl: config.file.downloadUrl,
|
||
uploadUrl: config.file.uploadUrl,
|
||
maxSize: config.file.maxSize,
|
||
acceptTypes: config.file.acceptTypes
|
||
},
|
||
|
||
// Token 配置
|
||
token: {
|
||
key: config.token.key,
|
||
refreshThreshold: config.token.refreshThreshold
|
||
},
|
||
|
||
// 公共路径
|
||
publicImgPath: config.publicImgPath,
|
||
publicWebPath: config.publicWebPath,
|
||
|
||
// 功能开关
|
||
features: config.features || {}
|
||
};
|
||
|
||
// 默认导出
|
||
export default APP_CONFIG;
|