206 lines
3.8 KiB
JavaScript
206 lines
3.8 KiB
JavaScript
/**
|
|
* 统一平台检测模块
|
|
* 消除分散在 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-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() ? '微信公众号' : 'H5';
|
|
return cachedAppTypeName;
|
|
}
|
|
// #endif
|
|
|
|
const platformNameMap = {
|
|
weapp: '微信小程序',
|
|
alipay: '支付宝小程序',
|
|
baidu: '百度小程序',
|
|
toutiao: '头条小程序',
|
|
qq: 'QQ小程序',
|
|
kuaishou: '快手小程序',
|
|
jd: '京东小程序',
|
|
'360': '360小程序',
|
|
xhs: '小红书小程序',
|
|
harmony: '鸿蒙元服务',
|
|
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', '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;
|