refactor(platform): extract platform detection into separate module
移除各文件中重复的平台检测代码,统一封装到platform.js模块,添加缓存优化,支持多端平台检测、app_type和显示名称获取等能力,简化后续平台相关代码维护
This commit is contained in:
199
common/js/platform.js
Normal file
199
common/js/platform.js
Normal file
@@ -0,0 +1,199 @@
|
||||
/**
|
||||
* 统一平台检测模块
|
||||
* 消除分散在 http.js, util.js, event-bus.js, customer-service.js 中的重复平台检测代码
|
||||
*/
|
||||
|
||||
let cachedPlatform = null;
|
||||
let cachedAppType = null;
|
||||
let cachedAppTypeName = null;
|
||||
|
||||
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-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',
|
||||
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() ? '微信公众号' : 'H5';
|
||||
return cachedAppTypeName;
|
||||
}
|
||||
// #endif
|
||||
|
||||
const platformNameMap = {
|
||||
weapp: '微信小程序',
|
||||
alipay: '支付宝小程序',
|
||||
baidu: '百度小程序',
|
||||
toutiao: '头条小程序',
|
||||
qq: 'QQ小程序',
|
||||
kuaishou: '快手小程序',
|
||||
jd: '京东小程序',
|
||||
'360': '360小程序',
|
||||
xhs: '小红书小程序',
|
||||
app: 'APP',
|
||||
pc: 'PC端'
|
||||
};
|
||||
|
||||
cachedAppTypeName = platformNameMap[platform] || '未知平台';
|
||||
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'];
|
||||
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;
|
||||
Reference in New Issue
Block a user