chore(test): 测试代码
This commit is contained in:
152
common/js/config-external.js
Normal file
152
common/js/config-external.js
Normal file
@@ -0,0 +1,152 @@
|
||||
// 外置配置管理
|
||||
// 用于管理需要外置的配置项,如语言包、主题配置等
|
||||
|
||||
// 语言包配置
|
||||
export const langConfig = {
|
||||
// 语言列表
|
||||
langList: ['zh-cn', 'en-us'],
|
||||
// 默认语言
|
||||
defaultLang: 'zh-cn',
|
||||
// 语言包加载方式:'lazy'(按需加载)或 'preload'(预加载)
|
||||
loadMode: 'lazy'
|
||||
};
|
||||
|
||||
// 主题配置
|
||||
export const themeConfig = {
|
||||
// 主题列表
|
||||
themeList: ['default', 'red', 'green', 'blue'],
|
||||
// 默认主题
|
||||
defaultTheme: 'default'
|
||||
};
|
||||
|
||||
// API配置
|
||||
export const apiConfig = {
|
||||
// API请求超时时间
|
||||
timeout: 30000,
|
||||
// 是否开启重试
|
||||
retry: true,
|
||||
// 重试次数
|
||||
retryCount: 3,
|
||||
// 重试间隔时间(毫秒)
|
||||
retryDelay: 1000
|
||||
};
|
||||
|
||||
// 配置外置管理类
|
||||
class ConfigExternal {
|
||||
constructor() {
|
||||
this.loadedConfigs = {};
|
||||
this.loadPromises = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载语言包
|
||||
* @param {string} lang - 语言类型
|
||||
* @returns {Promise} - 加载结果
|
||||
*/
|
||||
async loadLang(lang = langConfig.defaultLang) {
|
||||
if (this.loadedConfigs[`lang_${lang}`]) {
|
||||
return this.loadedConfigs[`lang_${lang}`];
|
||||
}
|
||||
|
||||
if (this.loadPromises[`lang_${lang}`]) {
|
||||
return this.loadPromises[`lang_${lang}`];
|
||||
}
|
||||
|
||||
this.loadPromises[`lang_${lang}`] = new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 动态加载语言包
|
||||
const langData = require(`@/lang/${lang}/common.js`).lang;
|
||||
this.loadedConfigs[`lang_${lang}`] = langData;
|
||||
resolve(langData);
|
||||
} catch (error) {
|
||||
console.error(`加载语言包 ${lang} 失败:`, error);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
return this.loadPromises[`lang_${lang}`];
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载页面语言包
|
||||
* @param {string} lang - 语言类型
|
||||
* @param {string} pagePath - 页面路径
|
||||
* @returns {Promise} - 加载结果
|
||||
*/
|
||||
async loadPageLang(lang = langConfig.defaultLang, pagePath) {
|
||||
const key = `page_${lang}_${pagePath}`;
|
||||
if (this.loadedConfigs[key]) {
|
||||
return this.loadedConfigs[key];
|
||||
}
|
||||
|
||||
if (this.loadPromises[key]) {
|
||||
return this.loadPromises[key];
|
||||
}
|
||||
|
||||
this.loadPromises[key] = new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 动态加载页面语言包
|
||||
const pageLangData = require(`@/lang/${lang}/${pagePath}.js`).lang;
|
||||
this.loadedConfigs[key] = pageLangData;
|
||||
resolve(pageLangData);
|
||||
} catch (error) {
|
||||
console.error(`加载页面语言包 ${lang}/${pagePath} 失败:`, error);
|
||||
resolve({});
|
||||
}
|
||||
});
|
||||
|
||||
return this.loadPromises[key];
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载主题配置(同步方式)
|
||||
* @param {string} theme - 主题名称
|
||||
* @returns {object} - 主题配置
|
||||
*/
|
||||
loadThemeSync(theme = themeConfig.defaultTheme) {
|
||||
if (this.loadedConfigs[`theme_${theme}`]) {
|
||||
return this.loadedConfigs[`theme_${theme}`];
|
||||
}
|
||||
|
||||
try {
|
||||
// 动态加载主题配置
|
||||
const themeData = require(`@/common/js/style_color.js`)[theme];
|
||||
this.loadedConfigs[`theme_${theme}`] = themeData;
|
||||
return themeData;
|
||||
} catch (error) {
|
||||
console.error(`加载主题 ${theme} 失败:`, error);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载主题配置(异步方式)
|
||||
* @param {string} theme - 主题名称
|
||||
* @returns {Promise} - 加载结果
|
||||
*/
|
||||
async loadTheme(theme = themeConfig.defaultTheme) {
|
||||
if (this.loadedConfigs[`theme_${theme}`]) {
|
||||
return this.loadedConfigs[`theme_${theme}`];
|
||||
}
|
||||
|
||||
if (this.loadPromises[`theme_${theme}`]) {
|
||||
return this.loadPromises[`theme_${theme}`];
|
||||
}
|
||||
|
||||
this.loadPromises[`theme_${theme}`] = new Promise((resolve, reject) => {
|
||||
try {
|
||||
// 动态加载主题配置
|
||||
const themeData = require(`@/common/js/style_color.js`)[theme];
|
||||
this.loadedConfigs[`theme_${theme}`] = themeData;
|
||||
resolve(themeData);
|
||||
} catch (error) {
|
||||
console.error(`加载主题 ${theme} 失败:`, error);
|
||||
reject(error);
|
||||
}
|
||||
});
|
||||
|
||||
return this.loadPromises[`theme_${theme}`];
|
||||
}
|
||||
}
|
||||
|
||||
export default new ConfigExternal();
|
||||
Reference in New Issue
Block a user