From a527dacac198d2163234c33860c21bf5cfb52e35 Mon Sep 17 00:00:00 2001 From: DCloud_UNI_BFC Date: Mon, 30 Sep 2024 16:42:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=B0=8F=E7=A8=8B=E5=BA=8F=E8=BF=90?= =?UTF-8?q?=E8=A1=8C=E6=97=B6=E5=B0=86getSystemInfoSync=20=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E6=9B=BF=E6=8D=A2=E4=B8=BA=E5=90=84=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E7=9A=84getBaseSystemInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/uni-mp-core/src/api/locale.ts | 7 +- packages/uni-mp-core/src/runtime/app.ts | 5 +- packages/uni-mp-weixin/dist/uni.api.esm.js | 105 ++++++++++++- packages/uni-mp-weixin/dist/uni.mp.esm.js | 105 ++++++++++++- packages/uni-mp-weixin/src/platform/index.ts | 155 ++++++++++++++++++- 5 files changed, 367 insertions(+), 10 deletions(-) diff --git a/packages/uni-mp-core/src/api/locale.ts b/packages/uni-mp-core/src/api/locale.ts index 3009799d6a7..1917ee7586f 100644 --- a/packages/uni-mp-core/src/api/locale.ts +++ b/packages/uni-mp-core/src/api/locale.ts @@ -1,13 +1,16 @@ import { LOCALE_EN, normalizeLocale } from '@dcloudio/uni-i18n' import { isFunction } from '@vue/shared' - +import { getBaseSystemInfo } from '@dcloudio/uni-platform' export const getLocale: typeof uni.getLocale = () => { // 优先使用 $locale const app = isFunction(getApp) && getApp({ allowDefault: true }) if (app && app.$vm) { return app.$vm.$locale } - return normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN + return ( + normalizeLocale((getBaseSystemInfo as any)('language').language) || + LOCALE_EN + ) } export const setLocale: typeof uni.setLocale = (locale) => { diff --git a/packages/uni-mp-core/src/runtime/app.ts b/packages/uni-mp-core/src/runtime/app.ts index 57d267b0b3f..ab30175bb51 100644 --- a/packages/uni-mp-core/src/runtime/app.ts +++ b/packages/uni-mp-core/src/runtime/app.ts @@ -4,7 +4,7 @@ import { type ComponentOptions, type ComponentPublicInstance, ref } from 'vue' import { initBaseInstance } from './componentInstance' import { initHooks, initUnknownHooks } from './componentHooks' import { LOCALE_EN, normalizeLocale } from '@dcloudio/uni-i18n' - +import { getBaseSystemInfo } from '@dcloudio/uni-platform' import App = WechatMiniprogram.App import { ON_ERROR, @@ -161,7 +161,8 @@ export function initAppLifecycle( function initLocale(appVm: ComponentPublicInstance) { const locale = ref( - normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN + normalizeLocale((getBaseSystemInfo as any)('language').language) || + LOCALE_EN ) Object.defineProperty(appVm, '$locale', { get() { diff --git a/packages/uni-mp-weixin/dist/uni.api.esm.js b/packages/uni-mp-weixin/dist/uni.api.esm.js index 8f69ffc238d..64c2dcf319b 100644 --- a/packages/uni-mp-weixin/dist/uni.api.esm.js +++ b/packages/uni-mp-weixin/dist/uni.api.esm.js @@ -2,8 +2,107 @@ import { isArray, hasOwn, isString, isPlainObject, isObject, capitalize, toRawTy import { normalizeLocale, LOCALE_EN } from '@dcloudio/uni-i18n'; import { Emitter, sortObject, onCreateVueApp, invokeCreateVueAppHook } from '@dcloudio/uni-shared'; -function getBaseSystemInfo() { - return wx.getSystemInfoSync(); +/** + * 获取wx系统基本信息, + * @param key 指定key,则调用指定wx的api + * @returns + */ +function getBaseSystemInfo(key) { + if (key) { + if (isGetSystemSetting(key)) + return wx.getSystemSetting(); + if (isGetAppAuthorizeSetting(key)) + return wx.getAppAuthorizeSetting(); + if (isGetDeviceInfo(key)) + return wx.getDeviceInfo(); + if (isGetWindowInfo(key)) + return wx.getWindowInfo(); + if (isGetAppBaseInfo(key)) + return wx.getAppBaseInfo(); + } + // 以下方法都是微信2.20.1 开始支持 + if (wx.getSystemSetting && wx.getAppAuthorizeSetting && wx.getDeviceInfo && wx.getWindowInfo && wx.getAppBaseInfo) { + const systemSetting = wx.getSystemSetting(); + const appAuthorizeSetting = wx.getAppAuthorizeSetting(); + const deviceInfo = wx.getDeviceInfo(); + const windowInfo = wx.getWindowInfo(); + const appBaseInfo = wx.getAppBaseInfo(); + return { + systemSetting, + appAuthorizeSetting, + deviceInfo, + windowInfo, + appBaseInfo + }; + } + else { + return wx.getSystemInfoSync(); + } +} +function isGetSystemSetting(key) { + switch (key) { + case 'bluetoothEnabled': return true; + case 'deviceOrientation': return true; + case 'locationEnabled': return true; + case 'wifiEnabled': return true; + default: return false; + } +} +function isGetAppAuthorizeSetting(key) { + switch (key) { + case 'albumAuthorized': return true; + case 'bluetoothAuthorized': return true; + case 'cameraAuthorized': return true; + case 'locationAuthorized': return true; + case 'locationReducedAccuracy': return true; + case 'microphoneAuthorized': return true; + case 'notificationAlertAuthorized': return true; + case 'notificationAuthorized': return true; + case 'notificationBadgeAuthorized': return true; + case 'phoneCalendarAuthorized': return true; + case 'notificationSoundAuthorized': return true; + default: return false; + } +} +function isGetDeviceInfo(key) { + switch (key) { + case 'brand': return true; + case 'model': return true; + case 'abi': return true; + case 'benchmarkLevel': return true; + case 'cpuType': return true; + case 'deviceAbi': return true; + case 'memorySize': return true; + case 'platform': return true; + case 'system': return true; + default: return false; + } +} +function isGetWindowInfo(key) { + switch (key) { + case 'windowWidth': return true; + case 'windowHeight': return true; + case 'statusBarHeight': return true; + case 'safeArea': return true; + case 'pixelRatio': return true; + case 'screenHeight': return true; + case 'screenTop': return true; + case 'screenWidth': return true; + default: return false; + } +} +function isGetAppBaseInfo(key) { + switch (key) { + case 'SDKVersion': return true; + case 'version': return true; + case 'language': return true; + case 'enableDebug': return true; + case 'fontSizeScaleFactor': return true; + case 'fontSizeSetting': return true; + case 'host': return true; + case 'theme': return true; + default: return false; + } } function validateProtocolFail(name, msg) { @@ -856,7 +955,7 @@ const getLocale = () => { if (app && app.$vm) { return app.$vm.$locale; } - return normalizeLocale(wx.getSystemInfoSync().language) || LOCALE_EN; + return normalizeLocale(getBaseSystemInfo('language').language) || LOCALE_EN; }; const setLocale = (locale) => { const app = isFunction(getApp) && getApp(); diff --git a/packages/uni-mp-weixin/dist/uni.mp.esm.js b/packages/uni-mp-weixin/dist/uni.mp.esm.js index ebd55f42c0e..10b8df88922 100644 --- a/packages/uni-mp-weixin/dist/uni.mp.esm.js +++ b/packages/uni-mp-weixin/dist/uni.mp.esm.js @@ -172,6 +172,109 @@ function initMixinRuntimeHooks(mpOptions) { initHooks(mpOptions, findMixinRuntimeHooks()); } +/** + * 获取wx系统基本信息, + * @param key 指定key,则调用指定wx的api + * @returns + */ +function getBaseSystemInfo(key) { + { + if (isGetSystemSetting(key)) + return wx.getSystemSetting(); + if (isGetAppAuthorizeSetting(key)) + return wx.getAppAuthorizeSetting(); + if (isGetDeviceInfo(key)) + return wx.getDeviceInfo(); + if (isGetWindowInfo(key)) + return wx.getWindowInfo(); + if (isGetAppBaseInfo(key)) + return wx.getAppBaseInfo(); + } + // 以下方法都是微信2.20.1 开始支持 + if (wx.getSystemSetting && wx.getAppAuthorizeSetting && wx.getDeviceInfo && wx.getWindowInfo && wx.getAppBaseInfo) { + const systemSetting = wx.getSystemSetting(); + const appAuthorizeSetting = wx.getAppAuthorizeSetting(); + const deviceInfo = wx.getDeviceInfo(); + const windowInfo = wx.getWindowInfo(); + const appBaseInfo = wx.getAppBaseInfo(); + return { + systemSetting, + appAuthorizeSetting, + deviceInfo, + windowInfo, + appBaseInfo + }; + } + else { + return wx.getSystemInfoSync(); + } +} +function isGetSystemSetting(key) { + switch (key) { + case 'bluetoothEnabled': return true; + case 'deviceOrientation': return true; + case 'locationEnabled': return true; + case 'wifiEnabled': return true; + default: return false; + } +} +function isGetAppAuthorizeSetting(key) { + switch (key) { + case 'albumAuthorized': return true; + case 'bluetoothAuthorized': return true; + case 'cameraAuthorized': return true; + case 'locationAuthorized': return true; + case 'locationReducedAccuracy': return true; + case 'microphoneAuthorized': return true; + case 'notificationAlertAuthorized': return true; + case 'notificationAuthorized': return true; + case 'notificationBadgeAuthorized': return true; + case 'phoneCalendarAuthorized': return true; + case 'notificationSoundAuthorized': return true; + default: return false; + } +} +function isGetDeviceInfo(key) { + switch (key) { + case 'brand': return true; + case 'model': return true; + case 'abi': return true; + case 'benchmarkLevel': return true; + case 'cpuType': return true; + case 'deviceAbi': return true; + case 'memorySize': return true; + case 'platform': return true; + case 'system': return true; + default: return false; + } +} +function isGetWindowInfo(key) { + switch (key) { + case 'windowWidth': return true; + case 'windowHeight': return true; + case 'statusBarHeight': return true; + case 'safeArea': return true; + case 'pixelRatio': return true; + case 'screenHeight': return true; + case 'screenTop': return true; + case 'screenWidth': return true; + default: return false; + } +} +function isGetAppBaseInfo(key) { + switch (key) { + case 'SDKVersion': return true; + case 'version': return true; + case 'language': return true; + case 'enableDebug': return true; + case 'fontSizeScaleFactor': return true; + case 'fontSizeSetting': return true; + case 'host': return true; + case 'theme': return true; + default: return false; + } +} + const HOOKS = [ ON_SHOW, ON_HIDE, @@ -279,7 +382,7 @@ function initAppLifecycle(appOptions, vm) { } } function initLocale(appVm) { - const locale = ref(normalizeLocale(wx.getSystemInfoSync().language) || LOCALE_EN); + const locale = ref(normalizeLocale(getBaseSystemInfo('language').language) || LOCALE_EN); Object.defineProperty(appVm, '$locale', { get() { return locale.value; diff --git a/packages/uni-mp-weixin/src/platform/index.ts b/packages/uni-mp-weixin/src/platform/index.ts index 80a7678ea42..22c4eb4af71 100644 --- a/packages/uni-mp-weixin/src/platform/index.ts +++ b/packages/uni-mp-weixin/src/platform/index.ts @@ -1,3 +1,154 @@ -export function getBaseSystemInfo() { - return wx.getSystemInfoSync() +/** + * 获取wx系统基本信息, + * @param key 指定key,则调用指定wx的api + * @returns + */ +export function getBaseSystemInfo(key?: any) { + if (key) { + if (isGetSystemSetting(key)) return wx.getSystemSetting() + if (isGetAppAuthorizeSetting(key)) return wx.getAppAuthorizeSetting() + if (isGetDeviceInfo(key)) return wx.getDeviceInfo() + if (isGetWindowInfo(key)) return wx.getWindowInfo() + if (isGetAppBaseInfo(key)) return wx.getAppBaseInfo() + } + // 以下方法都是微信2.20.1 开始支持 + if ( + wx.getSystemSetting && + wx.getAppAuthorizeSetting && + wx.getDeviceInfo && + wx.getWindowInfo && + wx.getAppBaseInfo + ) { + const systemSetting = wx.getSystemSetting() + const appAuthorizeSetting = wx.getAppAuthorizeSetting() + const deviceInfo = wx.getDeviceInfo() + const windowInfo = wx.getWindowInfo() + const appBaseInfo = wx.getAppBaseInfo() + return { + systemSetting, + appAuthorizeSetting, + deviceInfo, + windowInfo, + appBaseInfo, + } + } else { + return wx.getSystemInfoSync() + } +} + +function isGetSystemSetting(key: keyof WechatMiniprogram.SystemSetting) { + switch (key) { + case 'bluetoothEnabled': + return true + case 'deviceOrientation': + return true + case 'locationEnabled': + return true + case 'wifiEnabled': + return true + default: + return false + } +} + +function isGetAppAuthorizeSetting( + key: keyof WechatMiniprogram.AppAuthorizeSetting +) { + switch (key) { + case 'albumAuthorized': + return true + case 'bluetoothAuthorized': + return true + case 'cameraAuthorized': + return true + case 'locationAuthorized': + return true + case 'locationReducedAccuracy': + return true + case 'microphoneAuthorized': + return true + case 'notificationAlertAuthorized': + return true + case 'notificationAuthorized': + return true + case 'notificationBadgeAuthorized': + return true + case 'phoneCalendarAuthorized': + return true + case 'notificationSoundAuthorized': + return true + default: + return false + } +} + +function isGetDeviceInfo(key: keyof WechatMiniprogram.DeviceInfo) { + switch (key) { + case 'brand': + return true + case 'model': + return true + case 'abi': + return true + case 'benchmarkLevel': + return true + case 'cpuType': + return true + case 'deviceAbi': + return true + case 'memorySize': + return true + case 'platform': + return true + case 'system': + return true + default: + return false + } +} + +function isGetWindowInfo(key: keyof WechatMiniprogram.WindowInfo) { + switch (key) { + case 'windowWidth': + return true + case 'windowHeight': + return true + case 'statusBarHeight': + return true + case 'safeArea': + return true + case 'pixelRatio': + return true + case 'screenHeight': + return true + case 'screenTop': + return true + case 'screenWidth': + return true + default: + return false + } +} + +function isGetAppBaseInfo(key: keyof WechatMiniprogram.AppBaseInfo) { + switch (key) { + case 'SDKVersion': + return true + case 'version': + return true + case 'language': + return true + case 'enableDebug': + return true + case 'fontSizeScaleFactor': + return true + case 'fontSizeSetting': + return true + case 'host': + return true + case 'theme': + return true + default: + return false + } }