officialAccount/uni_modules/vk-uview-ui/libs/function/getSystemInfoSync.js

39 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* getSystemInfoSync的优化版本在微信小程序不会提示API已过期的警告
*/
function getSystemInfoSync() {
// #ifdef MP-WEIXIN
return getCompatibleSystemInfo();
// #endif
// #ifndef MP-WEIXIN
return uni.getSystemInfoSync();
// #endif
}
export default getSystemInfoSync;
// #ifdef MP-WEIXIN
function getCompatibleSystemInfo() {
if (wx.canIUse('getDeviceInfo') && wx.canIUse('getWindowInfo') && wx.canIUse('getAppBaseInfo') && wx.canIUse('getSystemSetting')) {
const deviceInfo = uni.getDeviceInfo();
const windowInfo = uni.getWindowInfo();
const appBaseInfo = uni.getAppBaseInfo();
const systemSetting = uni.getSystemSetting();
// const appAuthorizeSetting = wx.getAppAuthorizeSetting(); // 这个API效率低不放在这里了
// 最终会少 batteryLevel 属性和 appAuthorizeSetting 属性
return {
devicePixelRatio: windowInfo.pixelRatio,
hostFontSizeSetting: appBaseInfo.fontSizeSetting,
batteryLevel: 100, // 设置一个假的固定值进去防止出错如果再调用电量API这效率会比较低
...deviceInfo,
...windowInfo,
...appBaseInfo,
...systemSetting,
// ...appAuthorizeSetting,
};
} else {
return uni.getSystemInfoSync();
}
}
// #endif