From 4559fff39d2551ee7eac89c5860aac819a435246 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Tue, 24 May 2022 21:02:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20getWindowInfo=E3=80=81getDeviceInfo?= =?UTF-8?q?=E3=80=81getAppBaseInfo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/apis.js | 3 + src/core/helpers/promise.js | 2 +- .../service/api/device/get-window-info.js | 89 +++++ .../app-plus/service/api/device/system.js | 192 +++++----- src/platforms/app-plus/service/api/index.js | 1 + .../h5/service/api/base/get-browser-info.js | 220 +++++++++++ .../h5/service/api/device/get-system-info.js | 344 +++++------------- .../h5/service/api/device/get-window-info.js | 51 +++ .../mp-weixin/helpers/enhance-system-info.js | 55 ++- 9 files changed, 594 insertions(+), 363 deletions(-) create mode 100644 src/platforms/app-plus/service/api/device/get-window-info.js create mode 100644 src/platforms/h5/service/api/base/get-browser-info.js create mode 100644 src/platforms/h5/service/api/device/get-window-info.js diff --git a/lib/apis.js b/lib/apis.js index ff3cb2cfa..56ac35fdc 100644 --- a/lib/apis.js +++ b/lib/apis.js @@ -74,6 +74,9 @@ const media = [ const device = [ 'getSystemInfo', 'getSystemInfoSync', + 'getWindowInfo', + 'getDeviceInfo', + 'getAppBaseInfo', 'canIUse', 'onMemoryWarning', 'getNetworkType', diff --git a/src/core/helpers/promise.js b/src/core/helpers/promise.js index 5c1402e66..5b19b839b 100644 --- a/src/core/helpers/promise.js +++ b/src/core/helpers/promise.js @@ -8,7 +8,7 @@ import { } from './interceptor' const SYNC_API_RE = - /^\$|Window$|WindowStyle$|sendHostEvent|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale|invokePushCallback/ + /^\$|Window$|WindowStyle$|sendHostEvent|sendNativeEvent|restoreGlobal|requireGlobal|getCurrentSubNVue|getMenuButtonBoundingClientRect|^report|interceptors|Interceptor$|getSubNVueById|requireNativePlugin|upx2px|hideKeyboard|canIUse|^create|Sync$|Manager$|base64ToArrayBuffer|arrayBufferToBase64|getLocale|setLocale|invokePushCallback|getWindowInfo|getDeviceInfo|getAppBaseInfo/ const CONTEXT_API_RE = /^create|Manager$/ diff --git a/src/platforms/app-plus/service/api/device/get-window-info.js b/src/platforms/app-plus/service/api/device/get-window-info.js new file mode 100644 index 000000000..94d4a9354 --- /dev/null +++ b/src/platforms/app-plus/service/api/device/get-window-info.js @@ -0,0 +1,89 @@ +import { NAVBAR_HEIGHT } from 'uni-helpers/constants' + +import tabBar from '../../framework/tab-bar' + +import { getStatusbarHeight } from 'uni-platform/helpers/status-bar' + +import { + isTabBarPage, + getLastWebview, + getScreenInfo +} from '../util' + +export function getWindowInfo () { + const ios = plus.os.name.toLowerCase() === 'ios' + + const { + screenWidth, + screenHeight + } = getScreenInfo() + const statusBarHeight = getStatusbarHeight() + + let safeAreaInsets + const titleNView = { + height: 0, + cover: false + } + const webview = getLastWebview() + if (webview) { + let style = webview.getStyle() + style = style && style.titleNView + if (style && style.type && style.type !== 'none') { + titleNView.height = style.type === 'transparent' ? 0 : (statusBarHeight + NAVBAR_HEIGHT) + titleNView.cover = style.type === 'transparent' || style.type === 'float' + } + safeAreaInsets = webview.getSafeAreaInsets() + } else { + safeAreaInsets = plus.navigator.getSafeAreaInsets() + } + const tabBarView = { + height: 0, + cover: false + } + if (isTabBarPage()) { + tabBarView.height = tabBar.visible ? tabBar.height : 0 + tabBarView.cover = tabBar.cover + } + const windowTop = titleNView.cover ? titleNView.height : 0 + const windowBottom = tabBarView.cover ? tabBarView.height : 0 + let windowHeight = screenHeight - titleNView.height - tabBarView.height + let windowHeightReal = screenHeight - (titleNView.cover ? 0 : titleNView.height) - (tabBarView.cover ? 0 : tabBarView.height) + const windowWidth = screenWidth + if ((!tabBarView.height || tabBarView.cover) && !safeAreaInsets.bottom && safeAreaInsets.deviceBottom) { + windowHeight -= safeAreaInsets.deviceBottom + windowHeightReal -= safeAreaInsets.deviceBottom + } + safeAreaInsets = ios ? safeAreaInsets : { + left: 0, + right: 0, + top: titleNView.height && !titleNView.cover ? 0 : statusBarHeight, + bottom: 0 + } + const safeArea = { + left: safeAreaInsets.left, + right: windowWidth - safeAreaInsets.right, + top: safeAreaInsets.top, + bottom: windowHeightReal - safeAreaInsets.bottom, + width: windowWidth - safeAreaInsets.left - safeAreaInsets.right, + height: windowHeightReal - safeAreaInsets.top - safeAreaInsets.bottom + } + + return { + pixelRatio: plus.screen.scale, + screenWidth, + screenHeight, + windowWidth, + windowHeight, + statusBarHeight, + safeArea, + safeAreaInsets: { + top: safeAreaInsets.top, + right: safeAreaInsets.right, + bottom: safeAreaInsets.bottom, + left: safeAreaInsets.left + }, + windowTop, + windowBottom, + screenTop: screenHeight - windowHeight + } +} diff --git a/src/platforms/app-plus/service/api/device/system.js b/src/platforms/app-plus/service/api/device/system.js index a31116b6a..ec4496a89 100644 --- a/src/platforms/app-plus/service/api/device/system.js +++ b/src/platforms/app-plus/service/api/device/system.js @@ -1,112 +1,112 @@ import { - callApiSync, - isTabBarPage, - getLastWebview, - getScreenInfo + callApiSync } from '../util' -import { NAVBAR_HEIGHT } from 'uni-helpers/constants' +import { getWindowInfo } from './get-window-info' -import tabBar from '../../framework/tab-bar' +import deviceId from 'uni-platform/helpers/uuid' -import { getStatusbarHeight } from 'uni-platform/helpers/status-bar' +let systemInfo = {} +let _initSystemInfo = true -import deviceId from 'uni-platform/helpers/uuid' +function weexGetSystemInfoSync () { + if (!_initSystemInfo) return + const { getSystemInfoSync } = weex.requireModule('plus') + systemInfo = getSystemInfoSync() +} + +export function getDeviceInfo () { + weexGetSystemInfoSync() + const { + deviceBrand, deviceModel, osName, + osVersion + } = systemInfo + + const brand = deviceBrand.toLowerCase() + + return { + deviceBrand: brand, + deviceModel, + brand, + model: deviceModel, + system: `${osName === 'ios' ? 'iOS' : 'Android'} ${osVersion}`, + platform: osName + } +} + +export function getAppBaseInfo () { + weexGetSystemInfoSync() + const { + hostPackageName, hostName, osLanguage, + hostVersion, hostLanguage, hostTheme, + appId, appName, appVersion, appVersionCode + } = systemInfo + + return { + SDKVersion: '', + hostSDKVersion: '', + enableDebug: false, + appId, + appName, + appVersion, + appVersionCode, + appLanguage: uni.getLocale(), + version: plus.runtime.innerVersion, + language: osLanguage, + theme: '', + hostPackageName, + hostName, + hostVersion, + hostLanguage, + hostTheme, + hostFontSizeSetting: undefined + } +} export function getSystemInfoSync () { return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync') } export function getSystemInfo () { - const { getSystemInfoSync } = weex.requireModule('plus') - const info = getSystemInfoSync() - const { deviceBrand, deviceModel, osName, osVersion, osLanguage } = info - const brand = deviceBrand.toLowerCase() - const _osName = osName.toLowerCase() - const ios = _osName === 'ios' - const { - screenWidth, - screenHeight - } = getScreenInfo() - const statusBarHeight = getStatusbarHeight() - - let safeAreaInsets - const titleNView = { - height: 0, - cover: false - } - const webview = getLastWebview() - if (webview) { - let style = webview.getStyle() - style = style && style.titleNView - if (style && style.type && style.type !== 'none') { - titleNView.height = style.type === 'transparent' ? 0 : (statusBarHeight + NAVBAR_HEIGHT) - titleNView.cover = style.type === 'transparent' || style.type === 'float' - } - safeAreaInsets = webview.getSafeAreaInsets() - } else { - safeAreaInsets = plus.navigator.getSafeAreaInsets() - } - const tabBarView = { - height: 0, - cover: false - } - if (isTabBarPage()) { - tabBarView.height = tabBar.visible ? tabBar.height : 0 - tabBarView.cover = tabBar.cover - } - const windowTop = titleNView.cover ? titleNView.height : 0 - const windowBottom = tabBarView.cover ? tabBarView.height : 0 - let windowHeight = screenHeight - titleNView.height - tabBarView.height - let windowHeightReal = screenHeight - (titleNView.cover ? 0 : titleNView.height) - (tabBarView.cover ? 0 : tabBarView.height) - const windowWidth = screenWidth - if ((!tabBarView.height || tabBarView.cover) && !safeAreaInsets.bottom && safeAreaInsets.deviceBottom) { - windowHeight -= safeAreaInsets.deviceBottom - windowHeightReal -= safeAreaInsets.deviceBottom - } - safeAreaInsets = ios ? safeAreaInsets : { - left: 0, - right: 0, - top: titleNView.height && !titleNView.cover ? 0 : statusBarHeight, - bottom: 0 + _initSystemInfo = true + weexGetSystemInfoSync() + _initSystemInfo = false + const deviceInfo = getDeviceInfo() + const appBaseInfo = getAppBaseInfo() + _initSystemInfo = true + + const { osName, osLanguage, osVersion, pixelRatio } = systemInfo + const osLanguageSplit = osLanguage.split('-') + const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1] + let _osLanguage = `${osLanguageSplit[0]}${osLanguageSplitLast ? '-'+ osLanguageSplitLast : ''}` + + let extraData = { + errMsg: 'getSystemInfo:ok', + fontSizeSetting: appBaseInfo.hostFontSizeSetting, + devicePixelRatio: pixelRatio, + deviceId: deviceId(), + uniCompileVersion: __uniConfig.compilerVersion, + uniRuntimeVersion: __uniConfig.compilerVersion, + osLanguage: _osLanguage } - const safeArea = { - left: safeAreaInsets.left, - right: windowWidth - safeAreaInsets.right, - top: safeAreaInsets.top, - bottom: windowHeightReal - safeAreaInsets.bottom, - width: windowWidth - safeAreaInsets.left - safeAreaInsets.right, - height: windowHeightReal - safeAreaInsets.top - safeAreaInsets.bottom + + if (osName === 'ios') { + extraData.romName = osName + extraData.romVersion = osVersion } - return Object.assign({ - errMsg: 'getSystemInfo:ok', - brand: brand, - model: deviceModel, - pixelRatio: plus.screen.scale, - screenWidth, - screenHeight, - windowWidth, - windowHeight, - statusBarHeight, - language: osLanguage, - system: `${osName} ${osVersion}`, - version: plus.runtime.innerVersion, - fontSizeSetting: '', - platform: _osName, - SDKVersion: '', - windowTop, - windowBottom, - safeArea, - safeAreaInsets: { - top: safeAreaInsets.top, - right: safeAreaInsets.right, - bottom: safeAreaInsets.bottom, - left: safeAreaInsets.left - }, - deviceId: deviceId() - }, info, { - deviceBrand: brand, - osName: _osName - }) + const _systemInfo = Object.assign( + {}, + systemInfo, + getWindowInfo(), + deviceInfo, + appBaseInfo, + extraData + ) + + delete _systemInfo.screenTop + delete _systemInfo.enableDebug + delete _systemInfo.theme + + return _systemInfo } diff --git a/src/platforms/app-plus/service/api/index.js b/src/platforms/app-plus/service/api/index.js index 9a145112b..287510074 100644 --- a/src/platforms/app-plus/service/api/index.js +++ b/src/platforms/app-plus/service/api/index.js @@ -18,6 +18,7 @@ export * from './device/scan-code' export * from './device/soter-authentication' export * from './device/system' export * from './device/vibrate' +export * from './device/get-window-info' export * from './file/file' export * from './file/open-document' diff --git a/src/platforms/h5/service/api/base/get-browser-info.js b/src/platforms/h5/service/api/base/get-browser-info.js new file mode 100644 index 000000000..dce2d2f0c --- /dev/null +++ b/src/platforms/h5/service/api/base/get-browser-info.js @@ -0,0 +1,220 @@ +function IEVersion () { + const userAgent = navigator.userAgent + const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 + const isEdge = userAgent.indexOf('Edge') > -1 && !isIE + const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1 + if (isIE) { + const reIE = new RegExp('MSIE (\\d+\\.\\d+);') + reIE.test(userAgent) + const fIEVersion = parseFloat(RegExp.$1) + if (fIEVersion > 6) { + return fIEVersion + } else { + return 6 + } + } else if (isEdge) { + return -1 + } else if (isIE11) { + return 11 + } else { + return -1 + } +} + +function getDeviceBrand (model) { + if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' } + if (/windows/gi.test(model)) { return 'microsoft' } +} + +const ua = navigator.userAgent +/** + * 是否安卓设备 + */ +const isAndroid = /android/i.test(ua) +/** + * 是否iOS设备 + */ +const isIOS = /iphone|ipad|ipod/i.test(ua) +/** + * 是否是Windows设备 + */ +const isWindows = ua.match(/Windows NT ([\d|\d.\d]*)/i) +/** + * 是否是Mac设备 + */ +const isMac = /Macintosh|Mac/i.test(ua) +/** + * 是否是Linux设备 + */ +const isLinux = /Linux|X11/i.test(ua) +/** + * 是否是iPadOS + */ +const isIPadOS = isMac && navigator.maxTouchPoints > 0 +/** + * 获取系统信息-同步 + */ +export function getBrowserInfo () { + var language = navigator.language + var osname + var osversion + var model + let deviceType = 'phone' + + if (isIOS) { + osname = 'iOS' + const osversionFind = ua.match(/OS\s([\w_]+)\slike/) + if (osversionFind) { + osversion = osversionFind[1].replace(/_/g, '.') + } + const modelFind = ua.match(/\(([a-zA-Z]+);/) + if (modelFind) { + model = modelFind[1] + } + } else if (isAndroid) { + osname = 'Android' + // eslint-disable-next-line no-useless-escape + const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/) + if (osversionFind) { + osversion = osversionFind[1] + } + const infoFind = ua.match(/\((.+?)\)/) + const infos = infoFind ? infoFind[1].split(';') : ua.split(' ') + // eslint-disable-next-line no-useless-escape + const otherInfo = [/\bAndroid\b/i, /\bLinux\b/i, /\bU\b/i, /^\s?[a-z][a-z]$/i, /^\s?[a-z][a-z]-[a-z][a-z]$/i, /\bwv\b/i, /\/[\d\.,]+$/, /^\s?[\d\.,]+$/, /\bBrowser\b/i, /\bMobile\b/i] + for (let i = 0; i < infos.length; i++) { + const info = infos[i] + if (info.indexOf('Build') > 0) { + model = info.split('Build')[0].trim() + break + } + let other + for (let o = 0; o < otherInfo.length; o++) { + if (otherInfo[o].test(info)) { + other = true + break + } + } + if (!other) { + model = info.trim() + break + } + } + } else if (isIPadOS) { + model = 'iPad' + osname = 'iOS' + osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0' + deviceType = 'pad' + } else if (isWindows || isMac || isLinux) { + model = 'PC' + osname = 'PC' + deviceType = 'pc' + const osversionFind = ua.match(/\((.+?)\)/)[1] + + if (isWindows) { + osname = 'Windows' + osversion = '' + switch (isWindows[1]) { + case '5.1': + osversion = 'XP' + break + case '6.0': + osversion = 'Vista' + break + case '6.1': + osversion = '7' + break + case '6.2': + osversion = '8' + break + case '6.3': + osversion = '8.1' + break + case '10.0': + osversion = '10' + break + } + + const framework = osversionFind.match(/[Win|WOW]([\d]+)/) + if (framework) { + osversion += ` x${framework[1]}` + } + } else if (isMac) { + osname = 'macOS' + osversion = osversionFind.match(/Mac OS X (.+)/) || '' + + if (osversion) { + osversion = osversion[1].replace(/_/g, '.') + // '10_15_7' or '10.16; rv:86.0' + if (osversion.indexOf(';') !== -1) { + osversion = osversion.split(';')[0] + } + } + } else if (isLinux) { + osname = 'Linux' + osversion = osversionFind.match(/Linux (.*)/) || '' + + if (osversion) { + osversion = osversion[1] + // 'x86_64' or 'x86_64; rv:79.0' + if (osversion.indexOf(';') !== -1) { + osversion = osversion.split(';')[0] + } + } + } + } else { + osname = 'Other' + osversion = '0' + deviceType = 'other' + } + + var system = `${osname} ${osversion}` + var platform = osname.toLocaleLowerCase() + + let browserName + let browseVersion = String(IEVersion()) + if (browseVersion !== '-1') { browserName = 'IE' } else { + const browseVendors = ['Version', 'Firefox', 'Chrome', 'Edge{0,1}'] + const vendors = ['Safari', 'Firefox', 'Chrome', 'Edge'] + for (let index = 0; index < browseVendors.length; index++) { + const vendor = browseVendors[index] + const reg = new RegExp(`(${vendor})/(\\S*)\\b`) + if (reg.test(ua)) { + browserName = vendors[index] + browseVersion = ua.match(reg)[2] + } + } + } + + // deviceBrand + let deviceBrand = '' + if (model) { + const _model = model.toLocaleLowerCase() + deviceBrand = getDeviceBrand(_model) || + getDeviceBrand(osname.toLocaleLowerCase()) || + _model.split(' ')[0] + } + + // deviceOrientation + let deviceOrientation = 'portrait' + const orientation = typeof window.screen.orientation === 'undefined' ? window.orientation : window.screen.orientation.angle + deviceOrientation = Math.abs(orientation) === 90 ? 'landscape' : 'portrait' + + return { + deviceBrand, + deviceModel: model, + deviceOrientation, + brand: deviceBrand, + model, + system, + platform, + browserName: browserName.toLocaleLowerCase(), + browseVersion, + language, + deviceType, + ua, + osname, + osversion, + theme: '' + } +} diff --git a/src/platforms/h5/service/api/device/get-system-info.js b/src/platforms/h5/service/api/device/get-system-info.js index 332538d3a..f2fa6b1c5 100644 --- a/src/platforms/h5/service/api/device/get-system-info.js +++ b/src/platforms/h5/service/api/device/get-system-info.js @@ -1,277 +1,111 @@ -import getWindowOffset from 'uni-platform/helpers/get-window-offset' +import { getWindowInfo } from './get-window-info' import deviceId from 'uni-platform/helpers/uuid' -import safeAreaInsets from 'safe-area-insets' +import { getBrowserInfo } from '../base/get-browser-info' -function IEVersion () { - const userAgent = navigator.userAgent - const isIE = userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 - const isEdge = userAgent.indexOf('Edge') > -1 && !isIE - const isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1 - if (isIE) { - const reIE = new RegExp('MSIE (\\d+\\.\\d+);') - reIE.test(userAgent) - const fIEVersion = parseFloat(RegExp.$1) - if (fIEVersion > 6) { - return fIEVersion - } else { - return 6 - } - } else if (isEdge) { - return -1 - } else if (isIE11) { - return 11 - } else { - return -1 - } -} +let browserInfo = {} +let _initBrowserInfo = true -function getDeviceBrand (model) { - if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' } - if (/windows/gi.test(model)) { return 'microsoft' } +function initBrowserInfo () { + if (!_initBrowserInfo) return + browserInfo = getBrowserInfo() } -const ua = navigator.userAgent -/** - * 是否安卓设备 - */ -const isAndroid = /android/i.test(ua) -/** - * 是否iOS设备 - */ -const isIOS = /iphone|ipad|ipod/i.test(ua) -/** - * 是否是Windows设备 - */ -const isWindows = ua.match(/Windows NT ([\d|\d.\d]*)/i) -/** - * 是否是Mac设备 - */ -const isMac = /Macintosh|Mac/i.test(ua) -/** - * 是否是Linux设备 - */ -const isLinux = /Linux|X11/i.test(ua) -/** - * 是否是iPadOS - */ -const isIPadOS = isMac && navigator.maxTouchPoints > 0 -/** - * 获取系统信息-同步 - */ -export function getSystemInfoSync () { - var screen = window.screen - var pixelRatio = window.devicePixelRatio - // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正 - const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === 'number' - const landscape = screenFix && Math.abs(window.orientation) === 90 - var screenWidth = screenFix ? Math[landscape ? 'max' : 'min'](screen.width, screen.height) : screen.width - var screenHeight = screenFix ? Math[landscape ? 'min' : 'max'](screen.height, screen.width) : screen.height - var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth - var windowHeight = window.innerHeight - var language = navigator.language - var statusBarHeight = safeAreaInsets.top - var osname - var osversion - var model - let deviceType = 'phone' - - if (isIOS) { - osname = 'iOS' - const osversionFind = ua.match(/OS\s([\w_]+)\slike/) - if (osversionFind) { - osversion = osversionFind[1].replace(/_/g, '.') - } - const modelFind = ua.match(/\(([a-zA-Z]+);/) - if (modelFind) { - model = modelFind[1] - } - } else if (isAndroid) { - osname = 'Android' - // eslint-disable-next-line no-useless-escape - const osversionFind = ua.match(/Android[\s/]([\w\.]+)[;\s]/) - if (osversionFind) { - osversion = osversionFind[1] - } - const infoFind = ua.match(/\((.+?)\)/) - const infos = infoFind ? infoFind[1].split(';') : ua.split(' ') - // eslint-disable-next-line no-useless-escape - const otherInfo = [/\bAndroid\b/i, /\bLinux\b/i, /\bU\b/i, /^\s?[a-z][a-z]$/i, /^\s?[a-z][a-z]-[a-z][a-z]$/i, /\bwv\b/i, /\/[\d\.,]+$/, /^\s?[\d\.,]+$/, /\bBrowser\b/i, /\bMobile\b/i] - for (let i = 0; i < infos.length; i++) { - const info = infos[i] - if (info.indexOf('Build') > 0) { - model = info.split('Build')[0].trim() - break - } - let other - for (let o = 0; o < otherInfo.length; o++) { - if (otherInfo[o].test(info)) { - other = true - break - } - } - if (!other) { - model = info.trim() - break - } - } - } else if (isIPadOS) { - model = 'iPad' - osname = 'iOS' - osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0' - deviceType = 'pad' - } else if (isWindows || isMac || isLinux) { - model = 'PC' - osname = 'PC' - deviceType = 'pc' - const osversionFind = ua.match(/\((.+?)\)/)[1] - - if (isWindows) { - osname = 'Windows' - osversion = '' - switch (isWindows[1]) { - case '5.1': - osversion = 'XP' - break - case '6.0': - osversion = 'Vista' - break - case '6.1': - osversion = '7' - break - case '6.2': - osversion = '8' - break - case '6.3': - osversion = '8.1' - break - case '10.0': - osversion = '10' - break - } - - const framework = osversionFind.match(/[Win|WOW]([\d]+)/) - if (framework) { - osversion += ` x${framework[1]}` - } - } else if (isMac) { - osname = 'Mac' - osversion = osversionFind.match(/Mac OS X (.+)/) || '' - - if (osversion) { - osversion = osversion[1].replace(/_/g, '.') - // '10_15_7' or '10.16; rv:86.0' - if (osversion.indexOf(';') !== -1) { - osversion = osversion.split(';')[0] - } - } - } else if (isLinux) { - osname = 'Linux' - osversion = osversionFind.match(/Linux (.*)/) || '' - - if (osversion) { - osversion = osversion[1] - // 'x86_64' or 'x86_64; rv:79.0' - if (osversion.indexOf(';') !== -1) { - osversion = osversion.split(';')[0] - } - } - } - } else { - osname = 'Other' - osversion = '0' - deviceType = 'other' - } - - var system = `${osname} ${osversion}` - var platform = osname.toLocaleLowerCase() - var safeArea = { - left: safeAreaInsets.left, - right: windowWidth - safeAreaInsets.right, - top: safeAreaInsets.top, - bottom: windowHeight - safeAreaInsets.bottom, - width: windowWidth - safeAreaInsets.left - safeAreaInsets.right, - height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom - } - +export function getDeviceInfo () { + initBrowserInfo() const { - top: windowTop, - bottom: windowBottom - } = getWindowOffset() - - windowHeight -= windowTop - windowHeight -= windowBottom - - let browserName - let browseVersion = String(IEVersion()) - if (browseVersion !== '-1') { browserName = 'IE' } else { - const browseVendors = ['Version', 'Firefox', 'Chrome', 'Edge{0,1}'] - const vendors = ['Safari', 'Firefox', 'Chrome', 'Edge'] - for (let index = 0; index < browseVendors.length; index++) { - const vendor = browseVendors[index] - const reg = new RegExp(`(${vendor})/(\\S*)\\b`) - if (reg.test(ua)) { - browserName = vendors[index] - browseVersion = ua.match(reg)[2] - } - } - } - - // deviceBrand - let deviceBrand = '' - if (model) { - const _model = model.toLocaleLowerCase() - deviceBrand = getDeviceBrand(_model) || - getDeviceBrand(osname.toLocaleLowerCase()) || - _model.split(' ')[0] - } + deviceBrand, + deviceModel, + brand, + model, + platform, + system + } = browserInfo return { - windowTop, - windowBottom, - windowWidth, - windowHeight, - pixelRatio, - screenWidth, - screenHeight, - language, - statusBarHeight, - system, - platform, deviceBrand, - deviceType, + deviceModel, + brand, model, - deviceModel: model, - safeArea, - safeAreaInsets: { - top: safeAreaInsets.top, - right: safeAreaInsets.right, - bottom: safeAreaInsets.bottom, - left: safeAreaInsets.left - }, - deviceId: deviceId(), - SDKVersion: '', - ua, - uniPlatform: 'web', + system, + platform + } +} + +export function getAppBaseInfo () { + initBrowserInfo() + const { + theme, browserName, browseVersion, - osLanguage: language, - osName: osname.toLocaleLowerCase(), - osVersion: osversion, + language + } = browserInfo + + return { + SDKVersion: '', + hostSDKVersion: '', + enableDebug: false, + hostPackageName: '', + hostFontSizeSetting: undefined, + language, + hostName: browserName, + hostVersion: browseVersion, + hostTheme: theme, hostLanguage: language, - version: __uniConfig.appVersion, - uniCompileVersion: __uniConfig.compilerVersion, - uniRuntimeVersion: __uniConfig.compilerVersion, + theme, appId: __uniConfig.appId, appName: __uniConfig.appName, appVersion: __uniConfig.appVersion, appVersionCode: __uniConfig.appVersionCode, - hostName: browserName, - hostVersion: browseVersion, - osTheme: '', - hostTheme: '', - hostPackageName: '' + appLanguage: uni.getLocale(), + version: __uniConfig.appVersion } } + +/** + * 获取系统信息-同步 + */ +export function getSystemInfoSync () { + _initBrowserInfo = true + initBrowserInfo() + _initBrowserInfo = false + const windowInfo = getWindowInfo() + const deviceInfo = getDeviceInfo() + const appBaseInfo = getAppBaseInfo() + _initBrowserInfo = true + + const { ua, deviceType, browserName, browseVersion, osname, osversion, deviceOrientation } = browserInfo + const { pixelRatio } = windowInfo + + const systemInfo = Object.assign( + {}, + windowInfo, + deviceInfo, + appBaseInfo, + { + ua, + deviceType, + browserName, + browseVersion, + deviceId: deviceId(), + devicePixelRatio: pixelRatio, + deviceOrientation, + uniPlatform: 'web', + uniCompileVersion: __uniConfig.compilerVersion, + uniRuntimeVersion: __uniConfig.compilerVersion, + fontSizeSetting: appBaseInfo.hostFontSizeSetting, + osName: osname.toLocaleLowerCase(), + osVersion: osversion, + osLanguage: undefined, + osTheme: undefined + } + ) + + delete systemInfo.screenTop + delete systemInfo.enableDebug + delete systemInfo.theme + + return systemInfo +} /** * 获取系统信息-异步 */ diff --git a/src/platforms/h5/service/api/device/get-window-info.js b/src/platforms/h5/service/api/device/get-window-info.js new file mode 100644 index 000000000..40e9db055 --- /dev/null +++ b/src/platforms/h5/service/api/device/get-window-info.js @@ -0,0 +1,51 @@ +import getWindowOffset from 'uni-platform/helpers/get-window-offset' +import safeAreaInsets from 'safe-area-insets' + +export function getWindowInfo () { + var screen = window.screen + var pixelRatio = window.devicePixelRatio + // 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正 + const screenFix = /^Apple/.test(navigator.vendor) && typeof window.orientation === 'number' + const landscape = screenFix && Math.abs(window.orientation) === 90 + var screenWidth = screenFix ? Math[landscape ? 'max' : 'min'](screen.width, screen.height) : screen.width + var screenHeight = screenFix ? Math[landscape ? 'min' : 'max'](screen.height, screen.width) : screen.height + var windowWidth = Math.min(window.innerWidth, document.documentElement.clientWidth, screenWidth) || screenWidth + var windowHeight = window.innerHeight + var statusBarHeight = safeAreaInsets.top + + var safeArea = { + left: safeAreaInsets.left, + right: windowWidth - safeAreaInsets.right, + top: safeAreaInsets.top, + bottom: windowHeight - safeAreaInsets.bottom, + width: windowWidth - safeAreaInsets.left - safeAreaInsets.right, + height: windowHeight - safeAreaInsets.top - safeAreaInsets.bottom + } + + const { + top: windowTop, + bottom: windowBottom + } = getWindowOffset() + + windowHeight -= windowTop + windowHeight -= windowBottom + + return { + windowTop, + windowBottom, + windowWidth, + windowHeight, + pixelRatio, + screenWidth, + screenHeight, + statusBarHeight, + safeArea, + safeAreaInsets: { + top: safeAreaInsets.top, + right: safeAreaInsets.right, + bottom: safeAreaInsets.bottom, + left: safeAreaInsets.left + }, + screenTop: screenHeight - windowHeight + } +} diff --git a/src/platforms/mp-weixin/helpers/enhance-system-info.js b/src/platforms/mp-weixin/helpers/enhance-system-info.js index 1faaa9b33..d85000eb5 100644 --- a/src/platforms/mp-weixin/helpers/enhance-system-info.js +++ b/src/platforms/mp-weixin/helpers/enhance-system-info.js @@ -30,7 +30,13 @@ export function addSafeAreaInsets (result) { } export function populateParameters (result) { - const { brand, model, system, language, theme, version, hostName = '', platform } = result + const { + brand, model, system, + language, theme, version, + hostName, platform, fontSizeSetting, + SDKVersion, pixelRatio, deviceOrientation, + environment + } = result const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1 // osName osVersion @@ -46,7 +52,10 @@ export function populateParameters (result) { let hostVersion = version // host 枚举值 https://smartprogram.baidu.com/docs/develop/api/device_sys/hostlist/ if (__PLATFORM__ === 'mp-baidu') { - hostVersion = result.swanNativeVersion || version + hostVersion = result.swanNativeVersion + } + if (__PLATFORM__ === 'mp-jd') { + hostVersion = result.hostVersionName } // deviceType @@ -77,13 +86,31 @@ export function populateParameters (result) { } // hostName - let _hostName = hostName // mp-jd - if (__PLATFORM__ === 'mp-weixin') _hostName = (result.host || {}).env + let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd + if (__PLATFORM__ === 'mp-weixin') { + if (environment) { + _hostName = environment + } else if (result.host) { + _hostName = result.host.env + } + } if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') { _hostName = result.host } if (__PLATFORM__ === 'mp-qq') _hostName = result.AppPlatform if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') { _hostName = result.appName } if (__PLATFORM__ === 'mp-alipay') _hostName = result.app + // deviceOrientation + let _deviceOrientation = deviceOrientation // 仅 微信 百度 支持 + if (__PLATFORM__ === 'mp-baidu') { _deviceOrientation = result.orientation } + + // devicePixelRatio + let _devicePixelRatio = pixelRatio + if (__PLATFORM__ === 'mp-baidu') { _devicePixelRatio = result.devicePixelRatio } + + // SDKVersion + let _SDKVersion = SDKVersion + if (__PLATFORM__ === 'mp-alipay') { _SDKVersion = my.SDKVersion } + // wx.getAccountInfoSync const parameters = { @@ -97,19 +124,25 @@ export function populateParameters (result) { deviceBrand, deviceModel: model, deviceType, + devicePixelRatio: _devicePixelRatio, + deviceOrientation: _deviceOrientation, osName: osName.toLocaleLowerCase(), osVersion, - osLanguage: language, - osTheme: theme, hostTheme: theme, hostVersion, - hostLanguage: language, + hostLanguage: language.split('_', '-'), hostName: _hostName, + hostSDKVersion: _SDKVersion, + hostFontSizeSetting: fontSizeSetting, + windowTop: 0, + windowBottom: 0, // TODO - ua: '', - hostPackageName: '', - browserName: '', - browseVersion: '' + osLanguage: undefined, + osTheme: undefined, + ua: undefined, + hostPackageName: undefined, + browserName: undefined, + browseVersion: undefined } Object.assign(result, parameters) -- GitLab