Files
YGChatCS/src/constant/client-config.js

63 lines
1.9 KiB
JavaScript
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.

/**
* 客户端配置管理模块
*
* 功能说明:
* 所有配置现在从根目录的 client-configs.json 文件中读取
* 支持智能图片路径处理,自动适配不同环境
*/
// 读取完整配置文件
let rawConfigs;
try {
if (typeof window === 'undefined') {
// Node.js环境 - 读取JSON文件
const fs = require('fs');
const path = require('path');
const configPath = path.join(process.cwd(), 'client-configs.json');
const configContent = fs.readFileSync(configPath, 'utf8');
rawConfigs = JSON.parse(configContent);
} else {
// 浏览器环境 - 读取JSON文件需要构建时处理
// 这里应该通过构建工具将JSON内容注入或者通过API获取
throw new Error('浏览器环境需要通过构建工具或API获取配置');
}
} catch (error) {
console.error('读取配置文件失败:', error);
rawConfigs = {};
}
// 动态导入图片的辅助函数
const importImage = (imagePath) => {
try {
// 检查是否在Node.js环境中
if (typeof window === 'undefined') {
// Node.js环境中返回路径字符串
return imagePath;
}
// 将 @/ 路径转换为实际的导入路径
const actualPath = imagePath.replace('@/', '../');
return require(actualPath);
} catch (error) {
console.warn(`无法加载图片: ${imagePath}`, error);
return imagePath; // 返回原路径作为fallback
}
};
// 处理配置,将图片路径转换为实际的导入对象
const processConfigs = (configs) => {
const processedConfigs = {};
for (const [clientName, config] of Object.entries(configs)) {
processedConfigs[clientName] = {
...config,
logo: importImage(config.logo),
subLogo: importImage(config.subLogo),
};
}
return processedConfigs;
};
// 所有用户端的配置 - 从JSON配置文件生成
export const CLIENT_CONFIGS = processConfigs(rawConfigs);