1. 新增大量中英文语言包文件,覆盖自定义组件、页面、工具类的文案 2. 替换硬编码文本为多语言调用,统一全局文案管理 3. 优化语言包结构,拆分公共模块与业务模块 4. 修复部分语言加载与国际化适配问题
230 lines
4.9 KiB
JavaScript
230 lines
4.9 KiB
JavaScript
/**
|
|
* 统一平台检测模块
|
|
* 消除分散在 http.js, util.js, event-bus.js, customer-service.js 中的重复平台检测代码
|
|
*/
|
|
|
|
let cachedPlatform = null;
|
|
let cachedAppType = null;
|
|
let cachedAppTypeName = null;
|
|
|
|
// 延迟加载 lang 模块,避免与 lang.js 形成循环依赖
|
|
// (lang.js 顶部 import 了 Platform,所以 platform.js 不能顶部 import lang)
|
|
function _getLang() {
|
|
try {
|
|
return require('@/common/js/lang.js').default;
|
|
} catch (e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function _t(key, fallback) {
|
|
const lang = _getLang();
|
|
if (lang && typeof lang.lang === 'function') {
|
|
const result = lang.lang(key);
|
|
// lang() 在找不到 key 时会返回 key 本身(开发环境)或空串(生产环境)
|
|
if (result && result !== key) {
|
|
return result;
|
|
}
|
|
}
|
|
return fallback;
|
|
}
|
|
|
|
export const Platform = {
|
|
/**
|
|
* 检测当前运行平台
|
|
* @returns {string} 平台标识
|
|
*/
|
|
detect() {
|
|
if (cachedPlatform) {
|
|
return cachedPlatform;
|
|
}
|
|
|
|
// #ifdef H5
|
|
cachedPlatform = 'h5';
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
cachedPlatform = 'weapp';
|
|
// #endif
|
|
|
|
// #ifdef MP-ALIPAY
|
|
cachedPlatform = 'alipay';
|
|
// #endif
|
|
|
|
// #ifdef MP-BAIDU
|
|
cachedPlatform = 'baidu';
|
|
// #endif
|
|
|
|
// #ifdef MP-TOUTIAO
|
|
cachedPlatform = 'toutiao';
|
|
// #endif
|
|
|
|
// #ifdef MP-QQ
|
|
cachedPlatform = 'qq';
|
|
// #endif
|
|
|
|
// #ifdef MP-KUAISHOU
|
|
cachedPlatform = 'kuaishou';
|
|
// #endif
|
|
|
|
// #ifdef MP-HARMONY
|
|
cachedPlatform = 'harmony';
|
|
// #endif
|
|
|
|
// #ifdef MP-JD
|
|
cachedPlatform = 'jd';
|
|
// #endif
|
|
|
|
// #ifdef MP-360
|
|
cachedPlatform = '360';
|
|
// #endif
|
|
|
|
// #ifdef MP-XHS
|
|
cachedPlatform = 'xhs';
|
|
// #endif
|
|
|
|
// #ifdef APP-PLUS
|
|
cachedPlatform = 'app';
|
|
// #endif
|
|
|
|
// #ifdef PC
|
|
cachedPlatform = 'pc';
|
|
// #endif
|
|
|
|
if (!cachedPlatform) {
|
|
cachedPlatform = 'unknown';
|
|
}
|
|
|
|
return cachedPlatform;
|
|
},
|
|
|
|
/**
|
|
* 获取平台类型标识 (用于API请求)
|
|
* @returns {string} app_type 值
|
|
*/
|
|
getAppType() {
|
|
if (cachedAppType) {
|
|
return cachedAppType;
|
|
}
|
|
|
|
const platform = this.detect();
|
|
|
|
// #ifdef H5
|
|
if (platform === 'h5') {
|
|
cachedAppType = this.isWeiXin() ? 'wechat' : 'h5';
|
|
return cachedAppType;
|
|
}
|
|
// #endif
|
|
|
|
const platformMap = {
|
|
weapp: 'weapp',
|
|
alipay: 'aliapp',
|
|
baidu: 'baiduapp',
|
|
toutiao: 'MP-TOUTIAO',
|
|
qq: 'MP-QQ',
|
|
kuaishou: 'kuaishou',
|
|
jd: 'jd',
|
|
'360': '360',
|
|
xhs: 'xhs',
|
|
harmony: 'harmony',
|
|
app: 'app',
|
|
pc: 'pc'
|
|
};
|
|
|
|
cachedAppType = platformMap[platform] || 'unknown';
|
|
return cachedAppType;
|
|
},
|
|
|
|
/**
|
|
* 获取平台类型名称 (用于显示)
|
|
* @returns {string} app_type_name 值
|
|
*/
|
|
getAppTypeName() {
|
|
if (cachedAppTypeName) {
|
|
return cachedAppTypeName;
|
|
}
|
|
|
|
const platform = this.detect();
|
|
|
|
// #ifdef H5
|
|
if (platform === 'h5') {
|
|
cachedAppTypeName = this.isWeiXin()
|
|
? _t('common.platform.wechatOfficialAccount', '微信公众号')
|
|
: _t('common.platform.h5', 'H5');
|
|
return cachedAppTypeName;
|
|
}
|
|
// #endif
|
|
|
|
const platformNameMap = {
|
|
weapp: _t('common.platform.wechatMiniProgram', '微信小程序'),
|
|
alipay: _t('common.platform.alipayMiniProgram', '支付宝小程序'),
|
|
baidu: _t('common.platform.baiduMiniProgram', '百度小程序'),
|
|
toutiao: _t('common.platform.toutiaoMiniProgram', '头条小程序'),
|
|
qq: _t('common.platform.qqMiniProgram', 'QQ小程序'),
|
|
kuaishou: _t('common.platform.kuaishouMiniProgram', '快手小程序'),
|
|
jd: _t('common.platform.jdMiniProgram', '京东小程序'),
|
|
'360': _t('common.platform.miniProgram360', '360小程序'),
|
|
xhs: _t('common.platform.xhsMiniProgram', '小红书小程序'),
|
|
harmony: _t('common.platform.harmonyService', '鸿蒙元服务'),
|
|
app: _t('common.platform.app', 'APP'),
|
|
pc: _t('common.platform.pc', 'PC端'),
|
|
};
|
|
|
|
cachedAppTypeName = platformNameMap[platform] || _t('common.platform.unknown', '未知平台');
|
|
return cachedAppTypeName;
|
|
},
|
|
|
|
/**
|
|
* 判断是否运行在微信浏览器中 (H5环境)
|
|
* @returns {boolean}
|
|
*/
|
|
isWeiXin() {
|
|
// #ifdef H5
|
|
return navigator.userAgent.toLowerCase().includes('micromessenger');
|
|
// #endif
|
|
return false;
|
|
},
|
|
|
|
/**
|
|
* 判断是否是小程序环境
|
|
* @returns {boolean}
|
|
*/
|
|
isMiniProgram() {
|
|
const platform = this.detect();
|
|
const miniProgramPlatforms = ['weapp', 'alipay', 'baidu', 'toutiao', 'qq', 'kuaishou', 'jd', '360', 'xhs', 'harmony'];
|
|
return miniProgramPlatforms.includes(platform);
|
|
},
|
|
|
|
/**
|
|
* 判断是否是移动端 (小程序或H5或APP)
|
|
* @returns {boolean}
|
|
*/
|
|
isMobile() {
|
|
const platform = this.detect();
|
|
return platform !== 'pc' && platform !== 'unknown';
|
|
},
|
|
|
|
/**
|
|
* 获取完整的平台信息对象
|
|
* @returns {Object} 包含 platform, appType, appTypeName
|
|
*/
|
|
getPlatformInfo() {
|
|
return {
|
|
platform: this.detect(),
|
|
appType: this.getAppType(),
|
|
appTypeName: this.getAppTypeName()
|
|
};
|
|
},
|
|
|
|
/**
|
|
* 清除缓存 (用于测试或需要重新检测的场景)
|
|
*/
|
|
clearCache() {
|
|
cachedPlatform = null;
|
|
cachedAppType = null;
|
|
cachedAppTypeName = null;
|
|
}
|
|
};
|
|
|
|
export default Platform;
|