system.js 3.2 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1
import {
fxy060608's avatar
fxy060608 已提交
2
  callApiSync,
fxy060608's avatar
fxy060608 已提交
3
  isTabBarPage,
Q
qiang 已提交
4 5
  getLastWebview,
  getScreenInfo
fxy060608's avatar
fxy060608 已提交
6 7
} from '../util'

8
import { NAVBAR_HEIGHT } from 'uni-helpers/constants'
fxy060608's avatar
fxy060608 已提交
9

fxy060608's avatar
fxy060608 已提交
10
import tabBar from '../../framework/tab-bar'
fxy060608's avatar
fxy060608 已提交
11

12 13
import { getStatusbarHeight } from 'uni-platform/helpers/status-bar'

Q
qiang 已提交
14
import deviceId from 'uni-platform/helpers/uuid'
Q
qiang 已提交
15

16
export function getSystemInfoSync() {
fxy060608's avatar
fxy060608 已提交
17 18
  return callApiSync(getSystemInfo, Object.create(null), 'getSystemInfo', 'getSystemInfoSync')
}
fxy060608's avatar
fxy060608 已提交
19

20
export function getSystemInfo() {
fxy060608's avatar
fxy060608 已提交
21 22
  const platform = plus.os.name.toLowerCase()
  const ios = platform === 'ios'
23
  const isAndroid = platform === 'android'
Q
qiang 已提交
24 25 26 27 28 29 30 31 32 33
  const {
    screenWidth,
    screenHeight
  } = getScreenInfo()
  const statusBarHeight = getStatusbarHeight()

  let safeAreaInsets
  const titleNView = {
    height: 0,
    cover: false
34
  }
Q
qiang 已提交
35
  const webview = getLastWebview()
fxy060608's avatar
fxy060608 已提交
36 37
  if (webview) {
    let style = webview.getStyle()
Q
qiang 已提交
38 39
    style = style && style.titleNView
    if (style && style.type && style.type !== 'none') {
40
      titleNView.height = style.type === 'transparent' ? 0 : (statusBarHeight + NAVBAR_HEIGHT)
Q
qiang 已提交
41
      titleNView.cover = style.type === 'transparent' || style.type === 'float'
fxy060608's avatar
fxy060608 已提交
42
    }
Q
qiang 已提交
43
    safeAreaInsets = webview.getSafeAreaInsets()
44
  } else {
Q
qiang 已提交
45
    safeAreaInsets = plus.navigator.getSafeAreaInsets()
46
  }
Q
qiang 已提交
47 48 49 50 51 52 53 54 55 56
  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
57 58
  let windowHeight = screenHeight - titleNView.height - tabBarView.height
  let windowHeightReal = screenHeight - (titleNView.cover ? 0 : titleNView.height) - (tabBarView.cover ? 0 : tabBarView.height)
Q
qiang 已提交
59
  const windowWidth = screenWidth
60 61 62 63
  if ((!tabBarView.height || tabBarView.cover) && !safeAreaInsets.bottom && safeAreaInsets.deviceBottom) {
    windowHeight -= safeAreaInsets.deviceBottom
    windowHeightReal -= safeAreaInsets.deviceBottom
  }
Q
qiang 已提交
64 65 66 67 68 69 70
  safeAreaInsets = ios ? safeAreaInsets : {
    left: 0,
    right: 0,
    top: titleNView.height && !titleNView.cover ? 0 : statusBarHeight,
    bottom: 0
  }
  const safeArea = {
71 72 73
    left: safeAreaInsets.left,
    right: windowWidth - safeAreaInsets.right,
    top: safeAreaInsets.top,
Q
qiang 已提交
74
    bottom: windowHeightReal - safeAreaInsets.bottom,
75
    width: windowWidth - safeAreaInsets.left - safeAreaInsets.right,
Q
qiang 已提交
76
    height: windowHeightReal - safeAreaInsets.top - safeAreaInsets.bottom
fxy060608's avatar
fxy060608 已提交
77
  }
78

fxy060608's avatar
fxy060608 已提交
79 80
  return {
    errMsg: 'getSystemInfo:ok',
81
    brand: plus.device.vendor,
fxy060608's avatar
fxy060608 已提交
82 83 84 85
    model: plus.device.model,
    pixelRatio: plus.screen.scale,
    screenWidth,
    screenHeight,
86 87
    windowWidth,
    windowHeight,
fxy060608's avatar
fxy060608 已提交
88 89
    statusBarHeight,
    language: plus.os.language,
90
    system: `${ios ? 'iOS' : isAndroid ? 'Android' : ''} ${plus.os.version}`,
fxy060608's avatar
fxy060608 已提交
91 92 93 94
    version: plus.runtime.innerVersion,
    fontSizeSetting: '',
    platform,
    SDKVersion: '',
Q
qiang 已提交
95
    windowTop,
Q
qiang 已提交
96
    windowBottom,
Q
qiang 已提交
97 98 99 100 101 102
    safeArea,
    safeAreaInsets: {
      top: safeAreaInsets.top,
      right: safeAreaInsets.right,
      bottom: safeAreaInsets.bottom,
      left: safeAreaInsets.left
103
    },
Q
qiang 已提交
104
    deviceId: deviceId()
fxy060608's avatar
fxy060608 已提交
105
  }
106
}