提交 0c59f2a4 编写于 作者: D DCloud_LXH

feat: getSystemInfo

上级 d4b24efc
......@@ -79,6 +79,11 @@ declare namespace UniApp {
locale: string
fallbackLocale: string
locales: Record<string, Record<string, string>>
compilerVersion: string
appId: string
appName: string
appVersion: string
appVersionCode: string
}
interface UniRoute {
......
......@@ -5,6 +5,7 @@ import { getCurrentWebview } from '../../utils'
import { getStatusbarHeight } from '../../../helpers/statusBar'
import { isTabBarPage } from '../../../helpers/plus'
import deviceId from '../../../helpers/uuid'
import { extend } from '@vue/shared'
type SafeAreaInsets = Required<PlusNavigatorSafeAreaInsets>
......@@ -24,9 +25,13 @@ function getScreenInfo() {
export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
'getSystemInfoSync',
() => {
const platform = plus.os.name!.toLowerCase()
const ios = platform === 'ios'
const isAndroid = platform === 'android'
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()
......@@ -90,31 +95,38 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
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,
return extend(
{
brand: brand,
model: deviceModel,
pixelRatio: plus.screen.scale!,
screenWidth,
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(),
},
deviceId: deviceId(),
}
info,
{
deviceBrand: brand,
osName: _osName,
}
)
}
)
......
......@@ -14,6 +14,12 @@ export function initDefine(stringifyBoolean: boolean = false) {
: isDebug,
'process.env.UNI_APP_ID': JSON.stringify(manifestJson.appid || ''),
'process.env.UNI_APP_NAME': JSON.stringify(manifestJson.name || ''),
'process.env.UNI_APP_VERSION_NAME': JSON.stringify(
manifestJson.versionName || ''
),
'process.env.UNI_APP_VERSION_CODE': JSON.stringify(
manifestJson.versionCode || ''
),
'process.env.UNI_PLATFORM': JSON.stringify(process.env.UNI_PLATFORM),
'process.env.UNI_MP_PLUGIN': JSON.stringify(process.env.UNI_MP_PLUGIN),
'process.env.UNI_SUBPACKAGE': JSON.stringify(process.env.UNI_SUBPACKAGE),
......
......@@ -247,6 +247,13 @@ function generateConfig(
pagesJson: Record<string, any>,
config: ResolvedConfig
) {
const extraParameters = {
appId: process.env.UNI_APP_ID,
appName: process.env.UNI_APP_NAME,
appVersion: process.env.UNI_APP_VERSION,
appVersionCode: process.env.UNI_APP_VERSION_CODE,
}
delete pagesJson.pages
delete pagesJson.subPackages
delete pagesJson.subpackages
......@@ -257,7 +264,9 @@ function generateConfig(
: `${globalName}['____'+appid+'____']=true
delete ${globalName}['____'+appid+'____']
`) +
`${globalName}.__uniConfig=extend(${JSON.stringify(pagesJson)},{
`${globalName}.__uniConfig=extend(
${JSON.stringify(extraParameters)},
${JSON.stringify(pagesJson)},{
async,
debug,
networkTimeout,
......
......@@ -3,6 +3,7 @@ import safeAreaInsets from 'safe-area-insets'
import { defineSyncApi } from '@dcloudio/uni-api'
import { getWindowOffset } from '@dcloudio/uni-core'
import { IEVersion, getDeviceBrand } from '@dcloudio/uni-shared'
import {
ua,
......@@ -47,6 +48,7 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
let osname
let osversion
let model = ''
let deviceType = 'phone'
if (isIOS) {
osname = 'iOS'
......@@ -101,10 +103,12 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
} 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]
......@@ -164,6 +168,7 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
} else {
osname = 'Other'
osversion = '0'
deviceType = 'other'
}
const system = `${osname} ${osversion}`
......@@ -182,6 +187,33 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
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,
......@@ -202,9 +234,25 @@ export const getSystemInfoSync = defineSyncApi<typeof uni.getSystemInfoSync>(
bottom: safeAreaInsets.bottom,
left: safeAreaInsets.left,
},
version: '',
version: __uniConfig.appVersion,
SDKVersion: '',
deviceId: deviceId(),
ua,
uniPlatform: 'web',
browserName,
browseVersion,
osLanguage: language,
osName: osname,
osVersion: osversion,
hostLanguage: language,
uniCompileVersion: __uniConfig.compilerVersion,
uniRuntimeVersion: __uniConfig.compilerVersion,
appId: __uniConfig.appId,
appName: __uniConfig.appName,
appVersion: __uniConfig.appVersion,
appVersionCode: __uniConfig.appVersionCode,
osTheme: '',
hostTheme: '',
}
}
)
import { isPlainObject, isArray, hasOwn } from '@vue/shared'
import { addSafeAreaInsets, useDeviceId } from '@dcloudio/uni-mp-core'
import {
addSafeAreaInsets,
useDeviceId,
populateParameters,
} from '@dcloudio/uni-mp-core'
import { getStorageSync } from './shims'
......@@ -33,6 +37,7 @@ function handleSystemInfo(
useDeviceId({
getStorageSync,
})(fromRes, toRes)
populateParameters(fromRes, toRes)
let platform = fromRes.platform ? fromRes.platform.toLowerCase() : 'devtools'
if (!~['android', 'ios'].indexOf(platform)) {
......
export function addSafeAreaInsets(
fromRes: any,
toRes: UniApp.GetSystemInfoResult
) {
if (fromRes.safeArea) {
const safeArea = fromRes.safeArea
toRes.safeAreaInsets = {
top: safeArea.top,
left: safeArea.left,
right: fromRes.windowWidth - safeArea.right,
bottom: Math.abs(fromRes.screenHeight - safeArea.bottom),
}
}
}
import { getDeviceBrand } from '@dcloudio/uni-shared'
const UUID_KEY = '__DC_STAT_UUID'
let deviceId: string
interface Global {
getStorageSync: UniApp.Uni['getStorageSync']
}
export function useDeviceId(global: Global = __GLOBAL__ as Global) {
return function addDeviceId(_: any, toRes: UniApp.GetSystemInfoResult) {
deviceId = deviceId || global.getStorageSync(UUID_KEY)
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7)
__GLOBAL__.setStorage({
key: UUID_KEY,
data: deviceId,
})
}
toRes.deviceId = deviceId
}
}
export function addSafeAreaInsets(
fromRes: any,
toRes: UniApp.GetSystemInfoResult
) {
if (fromRes.safeArea) {
const safeArea = fromRes.safeArea
toRes.safeAreaInsets = {
top: safeArea.top,
left: safeArea.left,
right: fromRes.windowWidth - safeArea.right,
bottom: fromRes.screenHeight - safeArea.bottom,
}
}
}
export function populateParameters(
fromRes: any,
toRes: UniApp.GetSystemInfoResult
) {
const { brand, model, system, language, theme, version, hostName, platform } =
fromRes
// osName osVersion
let osName = ''
let osVersion = ''
if (__PLATFORM__ === 'mp-alipay') {
osName = platform.toLocaleLowerCase()
osVersion = system
} else {
osName = system.split(' ')[0] || ''
osVersion = system.split(' ')[1] || ''
}
let hostVersion = version
// host 枚举值 https://smartprogram.baidu.com/docs/develop/api/device_sys/hostlist/
if (__PLATFORM__ === 'mp-baidu') {
hostVersion = fromRes.swanNativeVersion || version
}
// 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
}
}
}
// deviceModel
let deviceBrand = model.split(' ')[0].toLocaleLowerCase()
if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark') {
deviceBrand = brand.toLocaleLowerCase()
} else {
deviceBrand = getDeviceBrand(deviceBrand)
}
// hostName
let _hostName = hostName // mp-jd
if (__PLATFORM__ === 'mp-weixin') _hostName = (fromRes.host || {}).env
if (__PLATFORM__ === 'mp-baidu' || __PLATFORM__ === 'mp-kuaishou')
_hostName = fromRes.host
if (__PLATFORM__ === 'mp-qq') _hostName = fromRes.AppPlatform
if (__PLATFORM__ === 'mp-toutiao' || __PLATFORM__ === 'mp-lark')
_hostName = fromRes.appName
if (__PLATFORM__ === 'mp-alipay') _hostName = fromRes.app
// wx.getAccountInfoSync
const parameters = {
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,
uniCompileVersion: process.env.UNI_COMPILER_VERSION,
uniRuntimeVersion: process.env.UNI_COMPILER_VERSION,
uniPlatform: process.env.UNI_PLATFORM,
deviceBrand,
deviceModel: model,
deviceType,
osName,
osVersion,
osLanguage: language,
osTheme: theme,
hostVersion,
hostLanguage: language,
hostName: _hostName,
// TODO
hostTheme: '',
hostPackageName: '',
browserName: '',
browseVersion: '',
}
Object.assign(toRes, parameters)
}
import { addSafeAreaInsets } from './addSafeAreaInsets'
import { useDeviceId } from './useDeviceId'
import {
useDeviceId,
addSafeAreaInsets,
populateParameters,
} from './enhanceSystemInfo'
import { MPProtocol } from './types'
export const getSystemInfo: MPProtocol = {
returnValue: (fromRes, toRes) => {
addSafeAreaInsets(fromRes, toRes)
useDeviceId()(fromRes, toRes)
populateParameters(fromRes, toRes)
},
}
export { addSafeAreaInsets } from './addSafeAreaInsets'
export { useDeviceId } from './useDeviceId'
export {
useDeviceId,
addSafeAreaInsets,
populateParameters,
} from './enhanceSystemInfo'
export { getSystemInfo } from './getSystemInfo'
export { getSystemInfoSync } from './getSystemInfoSync'
export { redirectTo } from './redirectTo'
......
const UUID_KEY = '__DC_STAT_UUID'
let deviceId: string
interface Global {
getStorageSync: UniApp.Uni['getStorageSync']
}
export function useDeviceId(global: Global = __GLOBAL__ as Global) {
return function addDeviceId(_: any, toRes: UniApp.GetSystemInfoResult) {
deviceId = deviceId || global.getStorageSync(UUID_KEY)
if (!deviceId) {
deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7)
__GLOBAL__.setStorage({
key: UUID_KEY,
data: deviceId,
})
}
toRes.deviceId = deviceId
}
}
......@@ -52,6 +52,7 @@ export {
getSystemInfoSync,
addSafeAreaInsets,
useDeviceId,
populateParameters,
showActionSheet,
} from './api/protocols'
// types
......
export { redirectTo, navigateTo, previewImage } from '@dcloudio/uni-mp-core'
export {
redirectTo,
navigateTo,
previewImage,
getSystemInfo,
getSystemInfoSync,
} from '@dcloudio/uni-mp-core'
export const chooseImage = {
args: {
sizeType: false,
......
......@@ -129,3 +129,37 @@ export function getValueByDataPath(obj: any, path: string): unknown {
}
return getValueByDataPath(obj[key], parts.slice(1).join('.'))
}
export 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
}
}
export 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'
}
}
......@@ -11,6 +11,7 @@ import {
CopyOptions,
initModuleAlias,
initPreContext,
parseManifestJsonOnce,
} from '@dcloudio/uni-cli-shared'
import { createConfig } from './config'
......@@ -33,6 +34,8 @@ const debugUni = debug('uni:plugin')
const pkg = require(path.resolve(__dirname, '../package.json'))
const manifestJson = parseManifestJsonOnce(process.env.UNI_INPUT_DIR)
initModuleAlias()
rewriteCompilerSfcParse()
......@@ -42,6 +45,10 @@ process.env.UNI_COMPILER_VERSION_TYPE = pkg.version.includes('alpha')
? 'a'
: 'r'
process.env.UNI_APP_ID = manifestJson.appid || ''
process.env.UNI_APP_NAME = manifestJson.name || ''
process.env.UNI_APP_VERSION = manifestJson.versionName || ''
process.env.UNI_APP_VERSION_CODE = manifestJson.versionCode || ''
export interface VitePluginUniOptions {
vueOptions?: VueOptions
vueJsxOptions?: (VueJSXPluginOptions & { babelPlugins?: any[] }) | boolean
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册