systemInfo.ts 3.9 KB
Newer Older
Q
qiang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
import { NAVBAR_HEIGHT } from '@dcloudio/uni-shared'
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'

type SafeAreaInsets = Required<PlusNavigatorSafeAreaInsets>

function getScreenInfo() {
  const { resolutionWidth, resolutionHeight } = plus.screen.getCurrentSize()
  return {
    screenWidth: Math.round(resolutionWidth),
    screenHeight: Math.round(resolutionHeight),
  }
}

export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
  'getSystemInfoSync',
  () => {
    const platform = plus.os.name!.toLowerCase()
    const ios = platform === 'ios'
    const isAndroid = platform === 'android'
    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 {
      brand: plus.device.vendor!,
      model: plus.device.model!,
      pixelRatio: plus.screen.scale!,
      screenWidth,
      screenHeight,
      windowWidth,
      windowHeight,
      statusBarHeight,
      language: plus.os.language!,
      system: `${ios ? 'iOS' : isAndroid ? 'Android' : ''} ${plus.os.version!}`,
      version: plus.runtime.innerVersion!,
      platform,
      SDKVersion: '',
      windowTop,
      windowBottom,
      safeArea,
      safeAreaInsets: {
        top: safeAreaInsets.top,
        right: safeAreaInsets.right,
        bottom: safeAreaInsets.bottom,
        left: safeAreaInsets.left,
      },
      deviceId: deviceId(),
    }
  }
)

export const getSystemInfo = defineAsyncApi<typeof uni.getSystemInfo>(
  'getSystemInfo',
  (_, { resolve }) => {
    return resolve(getSystemInfoSync())
  }
)