feat: 配置文件关于本地的图标的处理

This commit is contained in:
2025-09-22 23:20:06 +08:00
parent 5841d92d5f
commit dfaed6188b
3 changed files with 44 additions and 73 deletions

View File

@@ -1,9 +1,42 @@
// 导入统一的客户端配置
import { CLIENT_CONFIGS } from './client-config.js';
import DH from "@/pages/login/images/dh.png";
import DHWQ from "@/pages/login/images/dhwq.png";
import TM from "@/pages/login/images/tm.png";
import WSMM from "@/pages/login/images/wsmm.png";
// 小程序特有相关: 同时更改manifest.json和 project.config.json文件中的appid
// 重新导出配置供其他模块使用
export { CLIENT_CONFIGS };
/**
* 客户端配置管理模块
*
* 功能说明:
* 所有配置从根目录的 client-configs.json 文件中读取
* 图片通过映射函数将路径转换为实际导入的模块
*/
// 直接导入配置文件
import rawConfigs from '../../client-configs.json' with { type: 'json' };
// 图片路径映射表
const imageMap = {
'@/pages/login/images/dh.png': DH,
'@/pages/login/images/dhwq.png': DHWQ,
'@/pages/login/images/tm.png': TM,
'@/pages/login/images/wsmm.png': WSMM,
};
// 处理配置中的图片路径
const processConfigs = (configs) => {
const processed = {};
for (const [key, config] of Object.entries(configs)) {
processed[key] = {
...config,
logo: imageMap[config.logo] || config.logo,
subLogo: imageMap[config.subLogo] || config.subLogo,
};
}
return processed;
};
// 所有用户端的配置 - 处理后的配置
export const CLIENT_CONFIGS = processConfigs(rawConfigs);
// 获取当前用户端配置
export const getCurrentConfig = () => CLIENT_CONFIGS.tianmu;

View File

@@ -1,62 +0,0 @@
/**
* 客户端配置管理模块
*
* 功能说明:
* 所有配置现在从根目录的 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);