From 3928facff5d52ae055378ca5df3dbda212e5a13b Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Fri, 27 May 2022 15:49:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(mp-weixin):=20getDeviceInfo=E3=80=81getApp?= =?UTF-8?q?BaseInfo=E3=80=81getWindowInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/service/api/device/systemInfo.ts | 3 +- packages/uni-mp-core/src/api/promise.ts | 2 +- .../src/api/protocols/enhanceSystemInfo.ts | 81 +++++++++++-------- .../src/api/protocols/getAppBaseInfo.ts | 27 +++++++ .../src/api/protocols/getDeviceInfo.ts | 22 +++++ .../src/api/protocols/getWindowInfo.ts | 14 ++++ .../uni-mp-core/src/api/protocols/index.ts | 3 + packages/uni-mp-core/src/index.ts | 3 + packages/uni-mp-weixin/src/api/protocols.ts | 3 + 9 files changed, 123 insertions(+), 35 deletions(-) create mode 100644 packages/uni-mp-core/src/api/protocols/getAppBaseInfo.ts create mode 100644 packages/uni-mp-core/src/api/protocols/getDeviceInfo.ts create mode 100644 packages/uni-mp-core/src/api/protocols/getWindowInfo.ts diff --git a/packages/uni-app-plus/src/service/api/device/systemInfo.ts b/packages/uni-app-plus/src/service/api/device/systemInfo.ts index b6be43b5e..86d200306 100644 --- a/packages/uni-app-plus/src/service/api/device/systemInfo.ts +++ b/packages/uni-app-plus/src/service/api/device/systemInfo.ts @@ -49,7 +49,6 @@ export const getAppBaseInfo = defineSyncApi( const { hostPackageName, hostName, - osLanguage, hostVersion, hostLanguage, hostTheme, @@ -69,7 +68,7 @@ export const getAppBaseInfo = defineSyncApi( appVersionCode, appLanguage: uni.getLocale(), version: plus.runtime.innerVersion!, - language: osLanguage, + language: hostLanguage, theme: '', hostPackageName, hostName, diff --git a/packages/uni-mp-core/src/api/promise.ts b/packages/uni-mp-core/src/api/promise.ts index 135ecc7c1..e8e9643ef 100644 --- a/packages/uni-mp-core/src/api/promise.ts +++ b/packages/uni-mp-core/src/api/promise.ts @@ -7,7 +7,7 @@ import { } from '@dcloudio/uni-api/src/helpers/interceptor' const SYNC_API_RE = - /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64/ + /^\$|getLocale|setLocale|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getDeviceInfo|getAppBaseInfo|getWindowInfo/ const CONTEXT_API_RE = /^create|Manager$/ diff --git a/packages/uni-mp-core/src/api/protocols/enhanceSystemInfo.ts b/packages/uni-mp-core/src/api/protocols/enhanceSystemInfo.ts index c4b5d8ddc..e34bf069a 100644 --- a/packages/uni-mp-core/src/api/protocols/enhanceSystemInfo.ts +++ b/packages/uni-mp-core/src/api/protocols/enhanceSystemInfo.ts @@ -1,12 +1,13 @@ import { extend } from '@vue/shared' -function getDeviceBrand(model: string) { +function _getDeviceBrand(model: string) { if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' } if (/windows/gi.test(model)) { return 'microsoft' } + return '' } const UUID_KEY = '__DC_STAT_UUID' @@ -84,45 +85,17 @@ export function populateParameters( } // deviceType - let deviceType = fromRes.deviceType || 'phone' - if (__PLATFORM__ !== 'mp-baidu') { - type DeviceTypeMapsKeys = keyof typeof deviceTypeMaps - const deviceTypeMaps = { - ipad: 'pad', - windows: 'pc', - mac: 'pc', - } - const deviceTypeMapsKeys = Object.keys( - deviceTypeMaps - ) as DeviceTypeMapsKeys[] - const _model = model.toLocaleLowerCase() - for (let index = 0; index < deviceTypeMapsKeys.length; index++) { - const _m = deviceTypeMapsKeys[index] - if (_model.indexOf(_m) !== -1) { - deviceType = deviceTypeMaps[_m] - break - } - } - } + let deviceType = getGetDeviceType(fromRes, model) // deviceModel - let deviceBrand = model.split(' ')[0].toLocaleLowerCase() - if ( - __PLATFORM__ === 'mp-toutiao' || - __PLATFORM__ === 'mp-lark' || - isQuickApp - ) { - deviceBrand = brand.toLocaleLowerCase() - } else { - deviceBrand = getDeviceBrand(deviceBrand) - } + let deviceBrand = getDeviceBrand(brand, model, isQuickApp) // hostName let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd if (__PLATFORM__ === 'mp-weixin') { if (environment) { _hostName = environment - } else if (fromRes.host) { + } else if (fromRes.host && fromRes.host.env) { _hostName = fromRes.host.env } } @@ -189,3 +162,47 @@ export function populateParameters( extend(toRes, parameters) } + +export function getGetDeviceType(fromRes: any, model: string) { + // deviceType + let deviceType = fromRes.deviceType || 'phone' + if (__PLATFORM__ !== 'mp-baidu') { + type DeviceTypeMapsKeys = keyof typeof deviceTypeMaps + const deviceTypeMaps = { + ipad: 'pad', + windows: 'pc', + mac: 'pc', + } + const deviceTypeMapsKeys = Object.keys( + deviceTypeMaps + ) as DeviceTypeMapsKeys[] + const _model = model.toLocaleLowerCase() + for (let index = 0; index < deviceTypeMapsKeys.length; index++) { + const _m = deviceTypeMapsKeys[index] + if (_model.indexOf(_m) !== -1) { + deviceType = deviceTypeMaps[_m] + break + } + } + } + return deviceType +} + +export function getDeviceBrand( + brand: string, + model: string, + isQuickApp: boolean = false +) { + // deviceModel + let deviceBrand = model.split(' ')[0].toLocaleLowerCase() + if ( + __PLATFORM__ === 'mp-toutiao' || + __PLATFORM__ === 'mp-lark' || + isQuickApp + ) { + deviceBrand = brand.toLocaleLowerCase() + } else { + deviceBrand = _getDeviceBrand(deviceBrand) + } + return deviceBrand +} diff --git a/packages/uni-mp-core/src/api/protocols/getAppBaseInfo.ts b/packages/uni-mp-core/src/api/protocols/getAppBaseInfo.ts new file mode 100644 index 000000000..7b3d347b4 --- /dev/null +++ b/packages/uni-mp-core/src/api/protocols/getAppBaseInfo.ts @@ -0,0 +1,27 @@ +import { MPProtocol } from './types' +import { extend } from '@vue/shared' + +export const getAppBaseInfo: MPProtocol = { + returnValue: (fromRes, toRes) => { + const { version, language, SDKVersion, theme } = fromRes + + let _hostName = __PLATFORM__.split('-')[1] // mp-jd + if (__PLATFORM__ === 'mp-weixin') { + if (fromRes.host && fromRes.host.env) { + _hostName = fromRes.host.env + } + } + + extend(toRes, { + hostVersion: version, + hostLanguage: language.replace('_', '-'), + hostName: _hostName, + hostSDKVersion: SDKVersion, + hostTheme: theme, + appId: process.env.UNI_APP_ID, + appName: process.env.UNI_APP_NAME, + appVersion: process.env.UNI_APP_VERSION_NAME, + appVersionCode: process.env.UNI_APP_VERSION_CODE, + }) + }, +} diff --git a/packages/uni-mp-core/src/api/protocols/getDeviceInfo.ts b/packages/uni-mp-core/src/api/protocols/getDeviceInfo.ts new file mode 100644 index 000000000..d68fb00fc --- /dev/null +++ b/packages/uni-mp-core/src/api/protocols/getDeviceInfo.ts @@ -0,0 +1,22 @@ +import { MPProtocol } from './types' +import { + getGetDeviceType, + getDeviceBrand, + useDeviceId, +} from './enhanceSystemInfo' +import { extend } from '@vue/shared' + +export const getDeviceInfo: MPProtocol = { + returnValue: (fromRes, toRes) => { + const { brand, model } = fromRes + let deviceType = getGetDeviceType(fromRes, model) + let deviceBrand = getDeviceBrand(brand, model) + useDeviceId()(fromRes, toRes) + + extend(toRes, { + deviceType, + deviceBrand, + deviceModel: model, + }) + }, +} diff --git a/packages/uni-mp-core/src/api/protocols/getWindowInfo.ts b/packages/uni-mp-core/src/api/protocols/getWindowInfo.ts new file mode 100644 index 000000000..aa905a8b6 --- /dev/null +++ b/packages/uni-mp-core/src/api/protocols/getWindowInfo.ts @@ -0,0 +1,14 @@ +import { MPProtocol } from './types' +import { extend } from '@vue/shared' +import { addSafeAreaInsets } from './enhanceSystemInfo' + +export const getWindowInfo: MPProtocol = { + returnValue: (fromRes, toRes) => { + addSafeAreaInsets(fromRes, toRes) + + extend(toRes, { + windowTop: 0, + windowBottom: 0, + }) + }, +} diff --git a/packages/uni-mp-core/src/api/protocols/index.ts b/packages/uni-mp-core/src/api/protocols/index.ts index 37ea6bbb5..d73f67093 100644 --- a/packages/uni-mp-core/src/api/protocols/index.ts +++ b/packages/uni-mp-core/src/api/protocols/index.ts @@ -9,4 +9,7 @@ export { redirectTo } from './redirectTo' export { previewImage } from './previewImage' export { showActionSheet } from './showActionSheet' export { navigateTo, initEventChannel, getEventChannel } from './navigateTo' +export { getDeviceInfo } from './getDeviceInfo' +export { getAppBaseInfo } from './getAppBaseInfo' +export { getWindowInfo } from './getWindowInfo' export * from './types' diff --git a/packages/uni-mp-core/src/index.ts b/packages/uni-mp-core/src/index.ts index f5447b0ad..d214083af 100644 --- a/packages/uni-mp-core/src/index.ts +++ b/packages/uni-mp-core/src/index.ts @@ -54,6 +54,9 @@ export { useDeviceId, populateParameters, showActionSheet, + getDeviceInfo, + getAppBaseInfo, + getWindowInfo, } from './api/protocols' // types export { MiniProgramAppOptions, MiniProgramAppInstance } from './runtime/app' diff --git a/packages/uni-mp-weixin/src/api/protocols.ts b/packages/uni-mp-weixin/src/api/protocols.ts index 4dafa9469..000f5d699 100644 --- a/packages/uni-mp-weixin/src/api/protocols.ts +++ b/packages/uni-mp-weixin/src/api/protocols.ts @@ -4,4 +4,7 @@ export { getSystemInfo, getSystemInfoSync, showActionSheet, + getDeviceInfo, + getAppBaseInfo, + getWindowInfo, } from '@dcloudio/uni-mp-core' -- GitLab