Skip to content

Commit

Permalink
fix: 小程序运行时将getSystemInfoSync 方法替换为各平台的getBaseSystemInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
DCloud_UNI_BFC committed Sep 30, 2024
1 parent 5746483 commit a527dac
Show file tree
Hide file tree
Showing 5 changed files with 367 additions and 10 deletions.
7 changes: 5 additions & 2 deletions packages/uni-mp-core/src/api/locale.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
5 changes: 3 additions & 2 deletions packages/uni-mp-core/src/runtime/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -161,7 +161,8 @@ export function initAppLifecycle(

function initLocale(appVm: ComponentPublicInstance) {
const locale = ref<string>(
normalizeLocale(__GLOBAL__.getSystemInfoSync().language) || LOCALE_EN
normalizeLocale((getBaseSystemInfo as any)('language').language) ||
LOCALE_EN
)
Object.defineProperty(appVm, '$locale', {
get() {
Expand Down
105 changes: 102 additions & 3 deletions packages/uni-mp-weixin/dist/uni.api.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down
105 changes: 104 additions & 1 deletion packages/uni-mp-weixin/dist/uni.mp.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit a527dac

Please sign in to comment.