提交 281b4d55 编写于 作者: D DCloud_LXH

feat: getWindowInfo、getDeviceInfo、getAppBaseInfo

上级 1dbe5dfc
import { NAVBAR_HEIGHT } from '@dcloudio/uni-shared'
import tabBar from '../../framework/app/tabBar'
import { getCurrentWebview } from '../../utils'
import { getStatusbarHeight } from '../../../helpers/statusBar'
import { isTabBarPage } from '../../../helpers/plus'
import { defineSyncApi } from '@dcloudio/uni-api'
type SafeAreaInsets = Required<PlusNavigatorSafeAreaInsets>
function getScreenInfo() {
// 好像开发时刷新,偶发的 plus.screen.getCurrentSize 为 undefined
const { resolutionWidth, resolutionHeight } =
plus.screen.getCurrentSize() || {
resolutionWidth: 0,
resolutionHeight: 0,
}
return {
screenWidth: Math.round(resolutionWidth),
screenHeight: Math.round(resolutionHeight),
}
}
export const getWindowInfo = defineSyncApi<typeof uni.getWindowInfo>(
'getWindowInfo',
() => {
const ios = plus.os.name!.toLowerCase() === 'ios'
const { screenWidth, screenHeight } = getScreenInfo()
const statusBarHeight = getStatusbarHeight()
let safeAreaInsets: SafeAreaInsets
const titleNView = {
height: 0,
cover: false,
}
const webview = getCurrentWebview()
if (webview) {
const webStyle = webview.getStyle()
const style = webStyle && webStyle.titleNView
if (style && style.type && (style as any).type !== 'none') {
titleNView.height =
style.type === 'transparent' ? 0 : statusBarHeight + NAVBAR_HEIGHT
titleNView.cover =
style.type === 'transparent' || style.type === 'float'
}
safeAreaInsets = webview.getSafeAreaInsets() as SafeAreaInsets
} else {
safeAreaInsets = plus.navigator.getSafeAreaInsets() as SafeAreaInsets
}
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,
} as SafeAreaInsets)
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,
}
}
)
import { NAVBAR_HEIGHT } from '@dcloudio/uni-shared'
import { defineAsyncApi, defineSyncApi } from '@dcloudio/uni-api' import { defineAsyncApi, defineSyncApi } from '@dcloudio/uni-api'
import tabBar from '../../framework/app/tabBar'
import { getCurrentWebview } from '../../utils'
import { getStatusbarHeight } from '../../../helpers/statusBar'
import { isTabBarPage } from '../../../helpers/plus'
import deviceId from '../../../helpers/uuid' import deviceId from '../../../helpers/uuid'
import { extend } from '@vue/shared' import { extend } from '@vue/shared'
import { getWindowInfo } from './getWindowInfo'
type SafeAreaInsets = Required<PlusNavigatorSafeAreaInsets> let systemInfo: any
let _initSystemInfo = true
function getScreenInfo() { function weexGetSystemInfoSync() {
// 好像开发时刷新,偶发的 plus.screen.getCurrentSize 为 undefined if (!_initSystemInfo) return
const { resolutionWidth, resolutionHeight } = const { getSystemInfoSync } = weex.requireModule('plus')
plus.screen.getCurrentSize() || { systemInfo = getSystemInfoSync()
resolutionWidth: 0, }
resolutionHeight: 0,
export const getDeviceInfo = defineSyncApi<typeof uni.getDeviceInfo>(
'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,
} }
return {
screenWidth: Math.round(resolutionWidth),
screenHeight: Math.round(resolutionHeight),
} }
} )
export const getAppBaseInfo = defineSyncApi<typeof uni.getAppBaseInfo>(
'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 const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>( export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
'getSystemInfoSync', 'getSystemInfoSync',
() => { () => {
const { getSystemInfoSync } = weex.requireModule('plus') _initSystemInfo = true
const info = getSystemInfoSync() weexGetSystemInfoSync()
const { deviceBrand, deviceModel, osName, osVersion, osLanguage } = info _initSystemInfo = false
const brand = deviceBrand.toLowerCase() const windowInfo = getWindowInfo()
const _osName = osName.toLowerCase() const deviceInfo = getDeviceInfo()
const ios = _osName === 'ios' const appBaseInfo = getAppBaseInfo()
_initSystemInfo = true
const { screenWidth, screenHeight } = getScreenInfo() const { osName, osLanguage, osVersion } = systemInfo
const statusBarHeight = getStatusbarHeight() const { pixelRatio } = windowInfo
const osLanguageSplit = osLanguage.split('-')
const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1]
let _osLanguage = `${osLanguageSplit[0]}${
osLanguageSplitLast ? '-' + osLanguageSplitLast : ''
}`
let safeAreaInsets: SafeAreaInsets let extraData = {
const titleNView = { errMsg: 'getSystemInfo:ok',
height: 0, fontSizeSetting: appBaseInfo.hostFontSizeSetting,
cover: false, devicePixelRatio: pixelRatio,
} deviceId: deviceId(),
const webview = getCurrentWebview() uniCompileVersion: __uniConfig.compilerVersion,
if (webview) { uniRuntimeVersion: __uniConfig.compilerVersion,
const webStyle = webview.getStyle() osLanguage: _osLanguage,
const style = webStyle && webStyle.titleNView
if (style && style.type && (style as any).type !== 'none') {
titleNView.height =
style.type === 'transparent' ? 0 : statusBarHeight + NAVBAR_HEIGHT
titleNView.cover =
style.type === 'transparent' || style.type === 'float'
}
safeAreaInsets = webview.getSafeAreaInsets() as SafeAreaInsets
} else {
safeAreaInsets = plus.navigator.getSafeAreaInsets() as SafeAreaInsets
}
const tabBarView = {
height: 0,
cover: false,
} }
if (isTabBarPage()) {
tabBarView.height = tabBar.visible ? tabBar.height : 0 if (osName === 'ios') {
tabBarView.cover = tabBar.cover ;(extraData as any).romName = osName
} ;(extraData as any).romVersion = osVersion
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,
} as SafeAreaInsets)
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 extend( const _systemInfo: UniApp.GetSystemInfoResult = extend(
{ windowInfo,
brand: brand, systemInfo,
model: deviceModel, deviceInfo,
pixelRatio: plus.screen.scale!, appBaseInfo,
screenWidth, extraData
screenHeight,
windowWidth,
windowHeight,
statusBarHeight,
language: osLanguage,
system: `${osName} ${osVersion}`,
version: plus.runtime.innerVersion!,
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,
}
) )
delete (_systemInfo as any).screenTop
delete (_systemInfo as any).enableDebug
delete (_systemInfo as any).theme
return _systemInfo
} }
) )
......
...@@ -23,6 +23,7 @@ export * from './device/soterAuthentication' ...@@ -23,6 +23,7 @@ export * from './device/soterAuthentication'
export * from './device/scanCode' export * from './device/scanCode'
export * from './device/theme' export * from './device/theme'
export * from './device/brightness' export * from './device/brightness'
export * from './device/getWindowInfo'
export * from './media/getImageInfo' export * from './media/getImageInfo'
export * from './media/getVideoInfo' export * from './media/getVideoInfo'
......
...@@ -32,7 +32,9 @@ ...@@ -32,7 +32,9 @@
"createSelectorQuery", "createSelectorQuery",
"createVideoContext", "createVideoContext",
"downloadFile", "downloadFile",
"getAppBaseInfo",
"getClipboardData", "getClipboardData",
"getDeviceInfo",
"getEnterOptionsSync", "getEnterOptionsSync",
"getFileInfo", "getFileInfo",
"getImageInfo", "getImageInfo",
...@@ -58,6 +60,7 @@ ...@@ -58,6 +60,7 @@
"getSystemInfoSync", "getSystemInfoSync",
"getTopWindowStyle", "getTopWindowStyle",
"getVideoInfo", "getVideoInfo",
"getWindowInfo",
"hideKeyboard", "hideKeyboard",
"hideLeftWindow", "hideLeftWindow",
"hideLoading", "hideLoading",
......
import {
ua,
isIOS,
isAndroid,
isWindows,
isMac,
isLinux,
isIPadOS,
} from '../base/getBaseSystemInfo'
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: string) {
if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) {
return 'apple'
}
if (/windows/gi.test(model)) {
return 'microsoft'
}
}
export function getBrowserInfo() {
let osname
let osversion = '0'
let model = ''
let deviceType = 'phone'
const language = navigator.language
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'
deviceType = 'pad'
osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0'
} else if (isWindows || isMac || isLinux) {
model = 'PC'
osname = 'PC'
deviceType = 'pc'
osversion = '0'
let osversionFind = ua.match(/\((.+?)\)/)![1]
if (isWindows) {
osname = 'Windows'
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 && osversionFind.match(/[Win|WOW]([\d]+)/)
if (framework) {
osversion += ` x${framework[1]}`
}
} else if (isMac) {
osname = 'Mac'
const _osversion =
(osversionFind && 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'
const _osversion =
(osversionFind && 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'
}
const system = `${osname} ${osversion}`
const 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' | 'landscape' = '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: '',
}
}
import safeAreaInsets from 'safe-area-insets'
import { defineSyncApi } from '@dcloudio/uni-api' import { defineSyncApi } from '@dcloudio/uni-api'
import deviceId from '../../../helpers/uuid'
import { getBrowserInfo } from '../base/getBrowserInfo'
import { getWindowInfo } from './getWindowInfo'
import { extend } from '@vue/shared'
import { getWindowOffset } from '@dcloudio/uni-core' let browserInfo: ReturnType<typeof getBrowserInfo>
let _initBrowserInfo = true
import {
ua,
isIOS,
isAndroid,
isWindows,
isMac,
isLinux,
isIPadOS,
isLandscape,
getScreenFix,
getScreenWidth,
getWindowWidth,
getScreenHeight,
} from '../base/getBaseSystemInfo'
import deviceId from '../../../helpers/uuid' function initBrowserInfo() {
if (!_initBrowserInfo) return
browserInfo = getBrowserInfo()
}
export const getDeviceInfo = defineSyncApi<typeof uni.getDeviceInfo>(
'getDeviceInfo',
() => {
initBrowserInfo()
const { deviceBrand, deviceModel, brand, model, platform, system } =
browserInfo
function IEVersion() { return {
const userAgent = navigator.userAgent deviceBrand,
const isIE = deviceModel,
userAgent.indexOf('compatible') > -1 && userAgent.indexOf('MSIE') > -1 brand,
const isEdge = userAgent.indexOf('Edge') > -1 && !isIE model,
const isIE11 = system,
userAgent.indexOf('Trident') > -1 && userAgent.indexOf('rv:11.0') > -1 platform,
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
} }
} )
export const getAppBaseInfo = defineSyncApi<typeof uni.getAppBaseInfo>(
'getAppBaseInfo',
() => {
initBrowserInfo()
const { theme, browserName, browseVersion, language } = browserInfo
function getDeviceBrand(model: string) { return {
if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { SDKVersion: '',
return 'apple' hostSDKVersion: '',
} enableDebug: false,
if (/windows/gi.test(model)) { hostPackageName: '',
return 'microsoft' hostFontSizeSetting: undefined,
language,
hostName: browserName,
hostVersion: browseVersion,
hostTheme: theme,
hostLanguage: language,
theme,
appId: __uniConfig.appId,
appName: __uniConfig.appName,
appVersion: __uniConfig.appVersion,
appVersionCode: __uniConfig.appVersionCode,
appLanguage: uni.getLocale(),
version: __uniConfig.appVersion,
}
} }
} )
/** /**
* 获取系统信息-同步 * 获取系统信息-同步
...@@ -68,229 +69,53 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>( ...@@ -68,229 +69,53 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
platform: 'nodejs', platform: 'nodejs',
} as unknown as UniApp.GetSystemInfoResult } as unknown as UniApp.GetSystemInfoResult
} }
const pixelRatio = window.devicePixelRatio
// 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
const screenFix = getScreenFix()
const landscape = isLandscape(screenFix)
const screenWidth = getScreenWidth(screenFix, landscape)
const screenHeight = getScreenHeight(screenFix, landscape)
const windowWidth = getWindowWidth(screenWidth)
let windowHeight = window.innerHeight
const language = navigator.language
const statusBarHeight = safeAreaInsets.top
let osname
let osversion
let 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'
deviceType = 'pad'
osversion = typeof window.BigInt === 'function' ? '14.0' : '13.0'
} else if (isWindows || isMac || isLinux) {
model = 'PC'
osname = 'PC'
deviceType = 'pc'
osversion = '0'
let osversionFind = ua.match(/\((.+?)\)/)![1]
if (isWindows) { _initBrowserInfo = true
osname = 'Windows' initBrowserInfo()
switch (isWindows[1]) { _initBrowserInfo = false
case '5.1': const windowInfo = getWindowInfo()
osversion = 'XP' const deviceInfo = getDeviceInfo()
break const appBaseInfo = getAppBaseInfo()
case '6.0': _initBrowserInfo = true
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 = const {
osversionFind && osversionFind.match(/[Win|WOW]([\d]+)/)
if (framework) {
osversion += ` x${framework[1]}`
}
} else if (isMac) {
osname = 'Mac'
osversion =
(osversionFind && 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 && 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'
}
const system = `${osname} ${osversion}`
const platform = osname.toLocaleLowerCase()
const 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
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]
}
return {
windowTop,
windowBottom,
windowWidth,
windowHeight,
pixelRatio,
screenWidth,
screenHeight,
language,
statusBarHeight,
system,
platform,
deviceBrand,
deviceType,
model,
safeArea,
safeAreaInsets: {
top: safeAreaInsets.top,
right: safeAreaInsets.right,
bottom: safeAreaInsets.bottom,
left: safeAreaInsets.left,
},
version: __uniConfig.appVersion,
SDKVersion: '',
deviceId: deviceId(),
ua, ua,
uniPlatform: 'web', deviceType,
browserName, browserName,
browseVersion, browseVersion,
osLanguage: language, osname,
osName: osname.toLocaleLowerCase(), osversion,
osVersion: osversion, deviceOrientation,
hostLanguage: language, } = browserInfo
uniCompileVersion: __uniConfig.compilerVersion, const { pixelRatio } = windowInfo
uniRuntimeVersion: __uniConfig.compilerVersion,
appId: __uniConfig.appId, const systemInfo: UniApp.GetSystemInfoResult = extend(
appName: __uniConfig.appName, windowInfo,
appVersion: __uniConfig.appVersion, deviceInfo,
appVersionCode: __uniConfig.appVersionCode, appBaseInfo,
hostName: browserName, {
hostVersion: browseVersion, ua,
osTheme: '', deviceType,
hostTheme: '', browserName,
hostPackageName: '', browseVersion,
} as UniApp.GetSystemInfoResult 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 as any).screenTop
delete (systemInfo as any).enableDebug
delete (systemInfo as any).theme
return systemInfo
} }
) )
import safeAreaInsets from 'safe-area-insets'
import { getWindowOffset } from '@dcloudio/uni-core'
import {
isLandscape,
getScreenFix,
getScreenWidth,
getWindowWidth,
getScreenHeight,
} from '../base/getBaseSystemInfo'
import { defineSyncApi } from '@dcloudio/uni-api'
export const getWindowInfo = defineSyncApi<typeof uni.getWindowInfo>(
'getWindowInfo',
() => {
const pixelRatio = window.devicePixelRatio
// 横屏时 iOS 获取的屏幕宽高颠倒,进行纠正
const screenFix = getScreenFix()
const landscape = isLandscape(screenFix)
const screenWidth = getScreenWidth(screenFix, landscape)
const screenHeight = getScreenHeight(screenFix, landscape)
const windowWidth = getWindowWidth(screenWidth)
let windowHeight = window.innerHeight
const statusBarHeight = safeAreaInsets.top
const 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,
}
}
)
...@@ -21,6 +21,7 @@ export * from './device/accelerometer' ...@@ -21,6 +21,7 @@ export * from './device/accelerometer'
export * from './device/compass' export * from './device/compass'
export * from './device/vibrate' export * from './device/vibrate'
export * from './device/clipboard' export * from './device/clipboard'
export * from './device/getWindowInfo'
export * from './storage/storage' export * from './storage/storage'
......
...@@ -54,8 +54,13 @@ export function populateParameters( ...@@ -54,8 +54,13 @@ export function populateParameters(
language, language,
theme, theme,
version, version,
hostName = '', hostName,
platform, platform,
fontSizeSetting,
SDKVersion,
pixelRatio,
deviceOrientation,
environment,
} = fromRes } = fromRes
const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1 const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1
...@@ -72,7 +77,10 @@ export function populateParameters( ...@@ -72,7 +77,10 @@ export function populateParameters(
let hostVersion = version let hostVersion = version
// host 枚举值 https://smartprogram.baidu.com/docs/develop/api/device_sys/hostlist/ // host 枚举值 https://smartprogram.baidu.com/docs/develop/api/device_sys/hostlist/
if (__PLATFORM__ === 'mp-baidu') { if (__PLATFORM__ === 'mp-baidu') {
hostVersion = fromRes.swanNativeVersion || version hostVersion = fromRes.swanNativeVersion
}
if (__PLATFORM__ === 'mp-jd') {
hostVersion = fromRes.hostVersionName
} }
// deviceType // deviceType
...@@ -110,15 +118,41 @@ export function populateParameters( ...@@ -110,15 +118,41 @@ export function populateParameters(
} }
// hostName // hostName
let _hostName = hostName // mp-jd let _hostName = hostName || __PLATFORM__.split('-')[1] // mp-jd
if (__PLATFORM__ === 'mp-weixin') _hostName = (fromRes.host || {}).env if (__PLATFORM__ === 'mp-weixin') {
if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') if (environment) {
_hostName = environment
} else if (fromRes.host) {
_hostName = fromRes.host.env
}
}
if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou') {
_hostName = fromRes.host _hostName = fromRes.host
}
if (__PLATFORM__ === 'mp-qq') _hostName = fromRes.AppPlatform if (__PLATFORM__ === 'mp-qq') _hostName = fromRes.AppPlatform
if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') {
_hostName = fromRes.appName _hostName = fromRes.appName
}
if (__PLATFORM__ === 'mp-alipay') _hostName = fromRes.app if (__PLATFORM__ === 'mp-alipay') _hostName = fromRes.app
// deviceOrientation
let _deviceOrientation = deviceOrientation // 仅 微信 百度 支持
if (__PLATFORM__ === 'mp-baidu') {
_deviceOrientation = fromRes.orientation
}
// devicePixelRatio
let _devicePixelRatio = pixelRatio
if (__PLATFORM__ === 'mp-baidu') {
_devicePixelRatio = fromRes.devicePixelRatio
}
// SDKVersion
let _SDKVersion = SDKVersion
if (__PLATFORM__ === 'mp-alipay') {
_SDKVersion = my.SDKVersion
}
// wx.getAccountInfoSync // wx.getAccountInfoSync
const parameters = { const parameters = {
...@@ -132,19 +166,25 @@ export function populateParameters( ...@@ -132,19 +166,25 @@ export function populateParameters(
deviceBrand, deviceBrand,
deviceModel: model, deviceModel: model,
deviceType, deviceType,
devicePixelRatio: _devicePixelRatio,
deviceOrientation: _deviceOrientation,
osName: osName.toLocaleLowerCase(), osName: osName.toLocaleLowerCase(),
osVersion, osVersion,
osLanguage: language,
osTheme: theme,
hostTheme: theme, hostTheme: theme,
hostVersion, hostVersion,
hostLanguage: language, hostLanguage: language.split('_', '-'),
hostName: _hostName, hostName: _hostName,
hostSDKVersion: _SDKVersion,
hostFontSizeSetting: fontSizeSetting,
windowTop: 0,
windowBottom: 0,
// TODO // TODO
ua: '', osLanguage: undefined,
hostPackageName: '', osTheme: undefined,
browserName: '', ua: undefined,
browseVersion: '', hostPackageName: undefined,
browserName: undefined,
browseVersion: undefined,
} }
extend(toRes, parameters) extend(toRes, parameters)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册