236 lines
7.7 KiB
TypeScript
236 lines
7.7 KiB
TypeScript
/**
|
|
* API配置文件
|
|
* 统一管理所有API地址,方便部署时修改
|
|
* 支持环境变量覆盖配置
|
|
*/
|
|
|
|
// 环境配置类型
|
|
interface ApiConfig {
|
|
// 主API基础URL
|
|
baseUrl: string;
|
|
// 文档服务URL
|
|
documentUrl: string;
|
|
// 文档上传API URL
|
|
uploadUrl: string;
|
|
// OAuth2.0配置
|
|
oauth: {
|
|
// IDaaS服务器地址
|
|
serverUrl: string;
|
|
// OAuth2应用Client ID
|
|
clientId: string;
|
|
// OAuth2应用Client Secret
|
|
clientSecret: string;
|
|
// 回调地址
|
|
redirectUri: string;
|
|
// 应用ID(用于登出)
|
|
appId: string;
|
|
};
|
|
}
|
|
|
|
// 端口特定配置映射
|
|
// 根据不同端口提供不同的API配置
|
|
const portConfigs: Record<string, Partial<ApiConfig>> = {
|
|
'51704': {
|
|
baseUrl: 'http://172.16.0.55:51704',
|
|
documentUrl: 'http://172.16.0.55:51704/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:51704/admin/documents'
|
|
},
|
|
'51705': {
|
|
baseUrl: 'http://172.16.0.55:51705',
|
|
documentUrl: 'http://172.16.0.55:51705/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:51705/admin/documents'
|
|
},
|
|
'51706': {
|
|
baseUrl: 'http://172.16.0.55:51706',
|
|
documentUrl: 'http://172.16.0.55:51706/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:51706/admin/documents'
|
|
},
|
|
'51707': {
|
|
baseUrl: 'http://172.16.0.55:51707',
|
|
documentUrl: 'http://172.16.0.55:51707/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:51707/admin/documents'
|
|
},
|
|
'51708': {
|
|
baseUrl: 'http://172.16.0.55:51708',
|
|
documentUrl: 'http://172.16.0.55:51708/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:51708/admin/documents'
|
|
}
|
|
};
|
|
|
|
// 不同环境的默认配置
|
|
// 由于合同模板的上传,后续的的uploadUrl都不需要/upload,直接写/admin/documents,由程序自动添加/upload或/upload_contract_template
|
|
const configs: Record<string, ApiConfig> = {
|
|
// 开发环境
|
|
development: {
|
|
baseUrl: 'http://172.16.0.55:8008',
|
|
// baseUrl: 'http://172.16.0.81:3000',
|
|
// baseUrl: 'http://nas.7bm.co:3000',
|
|
// documentUrl: 'http://172.16.0.81:9000/docauditai/',
|
|
documentUrl: 'http://172.16.0.55:8008/docauditai/',
|
|
uploadUrl: 'http://172.16.0.55:8008/admin/documents',
|
|
// uploadUrl: 'http://172.16.0.58:8008/admin/documents',
|
|
// uploadUrl: 'http://172.16.0.58:8008/admin/documents',
|
|
oauth: {
|
|
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
|
|
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
|
|
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret
|
|
redirectUri: 'http://10.79.97.17/', // 回调地址
|
|
appId: 'idaasoauth2' // 应用ID,用于登出
|
|
}
|
|
},
|
|
|
|
// 测试环境
|
|
testing: {
|
|
baseUrl: 'http://nas.7bm.co:3000',
|
|
documentUrl: 'http://nas.7bm.co:9000/docauditai/',
|
|
uploadUrl: 'http://172.16.0.58:8008/admin/documents/upload',
|
|
oauth: {
|
|
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
|
|
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', // 需要替换为实际的Client ID
|
|
clientSecret: 'your_client_secret', // 需要替换为实际的Client Secret
|
|
redirectUri: 'http://nas.7bm.co:3000/callback', // 回调地址
|
|
appId: 'idaasoauth2' // 应用ID,用于登出
|
|
}
|
|
},
|
|
|
|
// 生产环境
|
|
production: {
|
|
// postgrest
|
|
baseUrl: 'http://10.79.97.17:8000',
|
|
// minio
|
|
documentUrl: 'http://10.76.244.156:9000/docauditai/',
|
|
// 文件上传
|
|
uploadUrl: 'http://10.79.97.17:8000/admin/documents',
|
|
oauth: {
|
|
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
|
|
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO',
|
|
clientSecret: 'VYk1AC5XIJEfnEXwyq0u9JEY3fi3byCfSD58zANGeb', // 需要替换为实际的Client Secret
|
|
redirectUri: 'http://10.79.97.17/', // 回调地址
|
|
appId: 'idaasoauth2' // 应用ID,用于登出
|
|
}
|
|
},
|
|
|
|
// 备用配置 (可以根据需要添加更多环境)
|
|
staging: {
|
|
baseUrl: 'http://172.16.0.119:9000/admin',
|
|
documentUrl: 'http://nas.7bm.co:9000/docauditai/',
|
|
uploadUrl: 'http://172.16.0.119:8000/admin/documents/upload',
|
|
oauth: {
|
|
serverUrl: 'http://10.79.112.85', // IDaaS服务器地址
|
|
clientId: '54d2a619fe5c81ae1250434c441fccccqMtKwh7H4fO', // 需要替换为实际的Client ID
|
|
clientSecret: 'your_client_secret', // 需要替换为实际的Client Secret
|
|
redirectUri: 'http://172.16.0.119:3000/callback', // 回调地址
|
|
appId: 'idaasoauth2' // 应用ID,用于登出
|
|
}
|
|
}
|
|
};
|
|
|
|
// 获取当前环境,默认为development
|
|
const getCurrentEnvironment = (): string => {
|
|
// 优先使用环境变量,然后使用 NODE_ENV
|
|
return process.env.NEXT_PUBLIC_API_ENV || process.env.NODE_ENV || 'development';
|
|
};
|
|
|
|
// 从环境变量获取配置,如果环境变量不存在则使用默认配置
|
|
const getConfigFromEnv = (defaultConfig: ApiConfig): ApiConfig => {
|
|
return {
|
|
baseUrl: process.env.NEXT_PUBLIC_API_BASE_URL || defaultConfig.baseUrl,
|
|
documentUrl: process.env.NEXT_PUBLIC_DOCUMENT_URL || defaultConfig.documentUrl,
|
|
uploadUrl: process.env.NEXT_PUBLIC_UPLOAD_URL || defaultConfig.uploadUrl,
|
|
oauth: {
|
|
serverUrl: process.env.NEXT_PUBLIC_OAUTH_SERVER_URL || defaultConfig.oauth.serverUrl,
|
|
clientId: process.env.NEXT_PUBLIC_OAUTH_CLIENT_ID || defaultConfig.oauth.clientId,
|
|
clientSecret: process.env.NEXT_PUBLIC_OAUTH_CLIENT_SECRET || defaultConfig.oauth.clientSecret,
|
|
redirectUri: process.env.NEXT_PUBLIC_OAUTH_REDIRECT_URI || defaultConfig.oauth.redirectUri,
|
|
appId: process.env.NEXT_PUBLIC_OAUTH_APP_ID || defaultConfig.oauth.appId
|
|
}
|
|
};
|
|
};
|
|
|
|
/**
|
|
* 获取当前端口号
|
|
* 优先从环境变量获取,然后从浏览器location获取
|
|
*/
|
|
const getCurrentPort = (): string => {
|
|
// 优先使用环境变量中的端口配置
|
|
if (process.env.API_PORT_CONFIG) {
|
|
return process.env.API_PORT_CONFIG;
|
|
}
|
|
|
|
// 如果是浏览器环境,从location获取端口
|
|
if (typeof window !== 'undefined' && window.location.port) {
|
|
return window.location.port;
|
|
}
|
|
|
|
return '';
|
|
};
|
|
|
|
/**
|
|
* 获取当前配置
|
|
* 支持根据端口动态切换API配置
|
|
*/
|
|
const getCurrentConfig = (): ApiConfig => {
|
|
const env = getCurrentEnvironment();
|
|
const port = getCurrentPort();
|
|
|
|
// 获取基础配置
|
|
let defaultConfig = configs[env] || configs.development;
|
|
|
|
// 如果有端口特定配置,则合并配置
|
|
if (port && portConfigs[port]) {
|
|
defaultConfig = {
|
|
...defaultConfig,
|
|
...portConfigs[port],
|
|
// 保持oauth配置不变,只覆盖API相关配置
|
|
oauth: defaultConfig.oauth
|
|
};
|
|
}
|
|
|
|
// 如果是浏览器环境,尝试从环境变量覆盖配置
|
|
if (typeof window !== 'undefined' || process.env.NEXT_PUBLIC_API_BASE_URL) {
|
|
return getConfigFromEnv(defaultConfig);
|
|
}
|
|
|
|
return defaultConfig;
|
|
};
|
|
|
|
// 导出当前环境的配置
|
|
export const apiConfig = getCurrentConfig();
|
|
|
|
// 导出具体的配置项,方便使用
|
|
export const {
|
|
baseUrl: API_BASE_URL,
|
|
documentUrl: DOCUMENT_URL,
|
|
uploadUrl: UPLOAD_URL,
|
|
oauth: OAUTH_CONFIG
|
|
} = apiConfig;
|
|
|
|
// 导出所有配置,供调试使用
|
|
export { configs };
|
|
|
|
// 工具函数:设置环境(主要用于测试)
|
|
export const setEnvironment = (env: string): ApiConfig => {
|
|
return configs[env] || configs.development;
|
|
};
|
|
|
|
/**
|
|
* 工具函数:获取当前端口配置信息(用于调试)
|
|
*/
|
|
export const getCurrentPortConfig = () => {
|
|
const port = getCurrentPort();
|
|
const env = getCurrentEnvironment();
|
|
return {
|
|
currentPort: port,
|
|
currentEnvironment: env,
|
|
hasPortConfig: !!(port && portConfigs[port]),
|
|
portConfig: port ? portConfigs[port] : null
|
|
};
|
|
};
|
|
|
|
// 调试信息(仅在开发环境显示)
|
|
// if (process.env.NODE_ENV === 'development') {
|
|
// console.log('📦 API配置信息:', {
|
|
// environment: getCurrentEnvironment(),
|
|
// config: apiConfig
|
|
// });
|
|
// }
|