123 lines
3.0 KiB
JavaScript
123 lines
3.0 KiB
JavaScript
function getSafeSystemInfo() {
|
||
try {
|
||
// #ifdef MP-WEIXIN
|
||
// 优先使用原生 wx 新 API,避开 uni 包装层内部对 getSystemInfoSync 的兼容调用
|
||
if (
|
||
typeof wx !== "undefined" &&
|
||
wx.getWindowInfo &&
|
||
wx.getAppBaseInfo &&
|
||
wx.getDeviceInfo
|
||
) {
|
||
return {
|
||
...wx.getWindowInfo(),
|
||
...wx.getAppBaseInfo(),
|
||
...wx.getDeviceInfo(),
|
||
};
|
||
}
|
||
// #endif
|
||
|
||
if (uni.getWindowInfo && uni.getAppBaseInfo && uni.getDeviceInfo) {
|
||
return {
|
||
...uni.getWindowInfo(),
|
||
...uni.getAppBaseInfo(),
|
||
...uni.getDeviceInfo(),
|
||
};
|
||
}
|
||
} catch (e) {
|
||
// fall through to legacy fallback
|
||
}
|
||
|
||
return uni.getSystemInfoSync();
|
||
}
|
||
|
||
let SYSTEM = getSafeSystemInfo();
|
||
|
||
export const getStatusBarHeight = () => SYSTEM.statusBarHeight || 15;
|
||
|
||
export const getTitleBarHeight = () => {
|
||
// #ifdef MP-ALIPAY
|
||
return 44;
|
||
// #endif
|
||
|
||
if (uni.getMenuButtonBoundingClientRect) {
|
||
try {
|
||
const rect = uni.getMenuButtonBoundingClientRect();
|
||
if (rect && rect.top && rect.height) {
|
||
return rect.height + (rect.top - getStatusBarHeight()) * 2;
|
||
}
|
||
} catch (e) {
|
||
console.error(e);
|
||
}
|
||
}
|
||
return 44;
|
||
};
|
||
|
||
export const getBavBarHeight = () => getStatusBarHeight() + getTitleBarHeight();
|
||
|
||
export const getLeftIconLeft = () => {
|
||
// if(tt?.getMenuButtonBoundingClientRect) {
|
||
// const { leftIcon: {left, width}} = tt.getMenuButtonBoundingClientRect
|
||
// return left + parseInt(width) + 5
|
||
// } else {
|
||
// return 0
|
||
// }
|
||
// #ifdef MP-TOUTIAO
|
||
const {
|
||
leftIcon: { left, width },
|
||
} = tt.getCustomButtonBoundingClientRect();
|
||
return left + parseInt(width);
|
||
// #endif
|
||
// #ifndef MP-TOUTIAO
|
||
return 0;
|
||
// #endif
|
||
};
|
||
|
||
export function getPlatform() {
|
||
return getPlatformProvider().replace("mp-", "");
|
||
}
|
||
|
||
export function getPlatformProvider() {
|
||
const platform = process.env.UNI_PLATFORM;
|
||
return platform || "mp-weixin";
|
||
// const platform = uni.getSystemInfoSync().platform;
|
||
// console.log(1111111, platform)
|
||
// // H5 模拟器调试时使用 __wxConfig 环境变量判断
|
||
// if (typeof __wxConfig !== 'undefined') return 'weixin';
|
||
// if (platform === 'devtools') {
|
||
// // 可以根据小程序环境变量判断
|
||
// // 抖音开发工具会定义 tt 对象
|
||
// return typeof tt !== 'undefined' ? 'toutiao' : 'weixin';
|
||
// }
|
||
|
||
// return typeof tt !== 'undefined' ? 'toutiao' : 'weixin';
|
||
}
|
||
|
||
export function getDeviceInfo() {
|
||
const info = getSafeSystemInfo();
|
||
|
||
return {
|
||
platform: info.platform,
|
||
brand: info.brand,
|
||
model: info.model,
|
||
system: info.system,
|
||
screenWidth: info.screenWidth,
|
||
screenHeight: info.screenHeight,
|
||
pixelRatio: info.pixelRatio,
|
||
language: info.language,
|
||
version: info.version,
|
||
SDKVersion: info.SDKVersion,
|
||
appId: "69665538a49b8ae3be50fe5d",
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 判断是否处于朋友圈单页模式
|
||
*/
|
||
export function isSinglePageMode() {
|
||
// #ifdef MP-WEIXIN
|
||
const launchOptions = uni.getLaunchOptionsSync();
|
||
return launchOptions.scene === 1154;
|
||
// #endif
|
||
return false;
|
||
}
|