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

@@ -11,7 +11,7 @@
## 配置文件结构
配置文件位于项目根目录的 `client-configs.json`,包含所有客户端的完整配置信息:
配置文件位于项目根目录的 `client-configs.json`,包含所有客户端的完整配置信息。配置会通过 `src/constant/base.js` 文件导入和使用
```json
{
@@ -58,14 +58,14 @@
### 使用客户端名称切换
```bash
# 切换到天沐客户端
npm run switch-client tianmu
# 切换到智念客户端
npm run switch-client zhinian
# 切换到朵花客户端
npm run switch-client duohua
# 切换到天沐客户端
npm run switch-client tianmu
```
### 直接运行脚本
@@ -139,7 +139,7 @@ node scripts/update-appid.js tianmu
## 注意事项
1. **配置同步**: 确保 `client-configs.json``src/constant/client-config.js` 中的基础配置保持同步
1. **配置同步**: 确保 `client-configs.json``src/constant/base.js` 中的基础配置保持同步
2. **测试建议**: 切换配置后建议先进行测试,确认功能正常
3. **备份建议**: 重要更新前建议备份相关配置文件
@@ -167,7 +167,7 @@ A: 在 `client-configs.json` 中添加新的客户端配置:
"subLogo": "@/pages/login/images/sublogo.png"
}
```
`src/constant/client-config.js` 会自动读取并处理新配置,无需手动修改。
`src/constant/base.js` 会自动读取并处理新配置,无需手动修改。
**Q: 脚本提示"读取配置文件失败"怎么办?**
A: 检查 `client-configs.json` 文件是否存在且格式正确。

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);