From a121dc67f77a52982a2c14f96e171476093f1450 Mon Sep 17 00:00:00 2001 From: DCloud_LXH <283700113@qq.com> Date: Thu, 28 Apr 2022 16:25:03 +0800 Subject: [PATCH] feat: getSystemInfo --- .../vue-cli-plugin-uni/lib/chain-webpack.js | 5 +- packages/vue-cli-plugin-uni/lib/env.js | 13 +- .../lib/platforms/h5.js | 5 + src/core/service/api/context/canvas.js | 2 +- .../app-plus/service/api/device/system.js | 28 +++-- .../h5/service/api/device/get-system-info.js | 53 +++++++- .../mp-alipay/helpers/system-info.js | 14 +-- src/platforms/mp-jd/runtime/api/protocols.js | 6 +- .../mp-weixin/helpers/enhance-system-info.js | 114 ++++++++++++++++++ .../mp-weixin/helpers/system-info.js | 29 +---- src/platforms/mp-xhs/runtime/api/protocols.js | 6 +- src/shared/util.js | 28 +++++ 12 files changed, 240 insertions(+), 63 deletions(-) create mode 100644 src/platforms/mp-weixin/helpers/enhance-system-info.js diff --git a/packages/vue-cli-plugin-uni/lib/chain-webpack.js b/packages/vue-cli-plugin-uni/lib/chain-webpack.js index 6d7e5b120..7a5558c9a 100644 --- a/packages/vue-cli-plugin-uni/lib/chain-webpack.js +++ b/packages/vue-cli-plugin-uni/lib/chain-webpack.js @@ -104,7 +104,10 @@ module.exports = function chainWebpack (platformOptions, vueOptions, api) { 'process.env.RUN_BY_HBUILDERX': process.env.RUN_BY_HBUILDERX, 'process.env.UNI_AUTOMATOR_WS_ENDPOINT': JSON.stringify(process.env.UNI_AUTOMATOR_WS_ENDPOINT), 'process.env.UNI_STAT_UNI_CLOUD': process.env.UNI_STAT_UNI_CLOUD || '', - 'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG || '' + 'process.env.UNI_STAT_DEBUG': process.env.UNI_STAT_DEBUG || '', + 'process.env.UNI_COMPILER_VERSION': JSON.stringify(process.env.UNI_COMPILER_VERSION), + 'process.env.UNI_APP_VERSION_NAME': JSON.stringify(process.env.UNI_APP_VERSION_NAME), + 'process.env.UNI_APP_VERSION_CODE': JSON.stringify(process.env.UNI_APP_VERSION_CODE) } if (process.env.UNI_USING_VUE3) { Object.assign(defines, { diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 8cb879019..b0b22cdc9 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -31,6 +31,8 @@ const manifestJsonObj = getManifestJson() process.env.UNI_APP_ID = manifestJsonObj.appid || '' process.env.UNI_APP_NAME = manifestJsonObj.name || '' process.env.UNI_PLATFORM = process.env.UNI_PLATFORM || 'h5' +process.env.UNI_APP_VERSION_NAME = manifestJsonObj.versionName +process.env.UNI_APP_VERSION_CODE = manifestJsonObj.versionCode // 小程序 vue3 标记 if (process.env.UNI_PLATFORM.indexOf('mp-') === 0) { @@ -388,6 +390,11 @@ const warningMsg = const needWarning = !platformOptions.usingComponents || usingComponentsAbsent let hasNVue = false // 输出编译器版本等信息 +const pagesPkg = require('@dcloudio/webpack-uni-pages-loader/package.json') +process.env.UNI_COMPILER_VERSION = '' +if (pagesPkg) { + process.env.UNI_COMPILER_VERSION = pagesPkg['uni-app'].compilerVersion +} const compileModeUrl = 'https://ask.dcloud.net.cn/article/36074' if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) { const compileMode = (process.env.UNI_USING_V3_NATIVE ? '(v3)' : '') + ':' + (isNVueCompiler ? 'uni-app' : 'weex') @@ -397,12 +404,8 @@ if (process.env.UNI_USING_NATIVE || process.env.UNI_USING_V3_NATIVE) { })) } else if (process.env.UNI_PLATFORM !== 'h5' && process.env.UNI_PLATFORM !== 'quickapp-native') { try { - let info = '' + let info = process.env.UNI_COMPILER_VERSION if (process.env.UNI_PLATFORM === 'app-plus') { - const pagesPkg = require('@dcloudio/webpack-uni-pages-loader/package.json') - if (pagesPkg) { - info = uniI18n.__('compilerVersion') + ':' + pagesPkg['uni-app'].compilerVersion - } if (process.env.UNI_USING_V3) { console.log(info) } else { diff --git a/packages/webpack-uni-pages-loader/lib/platforms/h5.js b/packages/webpack-uni-pages-loader/lib/platforms/h5.js index 6c62e30fb..379683cc2 100644 --- a/packages/webpack-uni-pages-loader/lib/platforms/h5.js +++ b/packages/webpack-uni-pages-loader/lib/platforms/h5.js @@ -443,6 +443,11 @@ global['____${h5.appid}____'] = true; delete global['____${h5.appid}____']; global.__uniConfig = ${JSON.stringify(pagesJson)}; global.__uniConfig.compilerVersion = '${compilerVersion}'; +global.__uniConfig.uniPlatform = '${process.env.UNI_PLATFORM}'; +global.__uniConfig.appId = '${process.env.UNI_APP_ID}'; +global.__uniConfig.appName = '${process.env.UNI_APP_NAME}'; +global.__uniConfig.appVersion = '${process.env.UNI_APP_VERSION_NAME}'; +global.__uniConfig.appVersionCode = '${process.env.UNI_APP_VERSION_CODE}'; global.__uniConfig.router = ${JSON.stringify(h5.router)}; global.__uniConfig.publicPath = ${JSON.stringify(h5.publicPath)}; global.__uniConfig['async'] = ${JSON.stringify(h5.async)}; diff --git a/src/core/service/api/context/canvas.js b/src/core/service/api/context/canvas.js index aa72e15e5..88013779a 100644 --- a/src/core/service/api/context/canvas.js +++ b/src/core/service/api/context/canvas.js @@ -372,7 +372,7 @@ export class CanvasContext { this.path = [] this.subpath = [] this.path.push({ - method: "beginPath", + method: 'beginPath', data: [] }) } diff --git a/src/platforms/app-plus/service/api/device/system.js b/src/platforms/app-plus/service/api/device/system.js index 517aea22b..a31116b6a 100644 --- a/src/platforms/app-plus/service/api/device/system.js +++ b/src/platforms/app-plus/service/api/device/system.js @@ -18,9 +18,12 @@ export function getSystemInfoSync () { } export function getSystemInfo () { - 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 @@ -76,21 +79,21 @@ export function getSystemInfo () { height: windowHeightReal - safeAreaInsets.top - safeAreaInsets.bottom } - return { + return Object.assign({ errMsg: 'getSystemInfo:ok', - brand: plus.device.vendor, - model: plus.device.model, + brand: brand, + model: deviceModel, pixelRatio: plus.screen.scale, screenWidth, screenHeight, windowWidth, windowHeight, statusBarHeight, - language: plus.os.language, - system: `${ios ? 'iOS' : isAndroid ? 'Android' : ''} ${plus.os.version}`, + language: osLanguage, + system: `${osName} ${osVersion}`, version: plus.runtime.innerVersion, fontSizeSetting: '', - platform, + platform: _osName, SDKVersion: '', windowTop, windowBottom, @@ -102,5 +105,8 @@ export function getSystemInfo () { left: safeAreaInsets.left }, deviceId: deviceId() - } -} + }, info, { + deviceBrand: brand, + osName: _osName + }) +} 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 eb3f1953b..ba9d76115 100644 --- a/src/platforms/h5/service/api/device/get-system-info.js +++ b/src/platforms/h5/service/api/device/get-system-info.js @@ -1,6 +1,7 @@ import getWindowOffset from 'uni-platform/helpers/get-window-offset' import deviceId from 'uni-platform/helpers/uuid' import safeAreaInsets from 'safe-area-insets' +import { IEVersion, getDeviceBrand } from 'uni-shared' const ua = navigator.userAgent /** @@ -45,6 +46,7 @@ export function getSystemInfoSync () { var osname var osversion var model + let deviceType = 'phone' if (isIOS) { osname = 'iOS' @@ -89,8 +91,11 @@ export function getSystemInfoSync () { 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) { @@ -147,6 +152,7 @@ export function getSystemInfoSync () { } else { osname = 'Other' osversion = '0' + deviceType = 'other' } var system = `${osname} ${osversion}` @@ -168,6 +174,30 @@ export function 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, @@ -180,7 +210,10 @@ export function getSystemInfoSync () { statusBarHeight, system, platform, + deviceBrand, + deviceType, model, + deviceModel: model, safeArea, safeAreaInsets: { top: safeAreaInsets.top, @@ -188,7 +221,25 @@ export function getSystemInfoSync () { bottom: safeAreaInsets.bottom, left: safeAreaInsets.left }, - deviceId: deviceId() + deviceId: deviceId(), + SDKVersion: '', + ua, + uniPlatform: 'web', + browserName, + browseVersion, + osLanguage: language, + osName: osname, + osVersion: osversion, + hostLanguage: language, + version: __uniConfig.appVersion, + uniCompileVersion: __uniConfig.compilerVersion, + uniRuntimeVersion: __uniConfig.compilerVersion, + appId: __uniConfig.appId, + appName: __uniConfig.appName, + appVersion: __uniConfig.appVersion, + appVersionCode: __uniConfig.appVersionCode, + osTheme: '', + hostTheme: '' } } /** diff --git a/src/platforms/mp-alipay/helpers/system-info.js b/src/platforms/mp-alipay/helpers/system-info.js index 8cc27a216..ea3bd5a27 100644 --- a/src/platforms/mp-alipay/helpers/system-info.js +++ b/src/platforms/mp-alipay/helpers/system-info.js @@ -1,4 +1,5 @@ import { getStorageSync } from './storage' +import { addSafeAreaInsets, populateParameters } from '../../mp-weixin/helpers/enhance-system-info' const UUID_KEY = '__DC_STAT_UUID' let deviceId @@ -14,18 +15,6 @@ function addUuid (result) { result.deviceId = deviceId } -function addSafeAreaInsets (result) { - if (result.safeArea) { - const safeArea = result.safeArea - result.safeAreaInsets = { - top: safeArea.top, - left: safeArea.left, - right: result.windowWidth - safeArea.right, - bottom: result.windowHeight - safeArea.bottom - } - } -} - function normalizePlatform (result) { let platform = result.platform ? result.platform.toLowerCase() : 'devtools' if (!~['android', 'ios'].indexOf(platform)) { @@ -39,5 +28,6 @@ export default { addUuid(result) addSafeAreaInsets(result) normalizePlatform(result) + populateParameters(result) } } diff --git a/src/platforms/mp-jd/runtime/api/protocols.js b/src/platforms/mp-jd/runtime/api/protocols.js index be56514c1..45158ee05 100644 --- a/src/platforms/mp-jd/runtime/api/protocols.js +++ b/src/platforms/mp-jd/runtime/api/protocols.js @@ -1,7 +1,7 @@ // import navigateTo from 'uni-helpers/navigate-to' // import redirectTo from '../../../mp-weixin/helpers/redirect-to' // import previewImage from '../../../mp-weixin/helpers/normalize-preview-image' -// import getSystemInfo from '../../../mp-weixin/helpers/system-info' +import getSystemInfo from '../../../mp-weixin/helpers/system-info' // import getUserProfile from '../../../mp-weixin/helpers/get-user-profile' // 需要做转换的 API 列表 @@ -9,8 +9,8 @@ export const protocols = { // navigateTo, // redirectTo, // previewImage, - // getSystemInfo, - // getSystemInfoSync: getSystemInfo, + getSystemInfo, + getSystemInfoSync: getSystemInfo // getUserProfile } diff --git a/src/platforms/mp-weixin/helpers/enhance-system-info.js b/src/platforms/mp-weixin/helpers/enhance-system-info.js new file mode 100644 index 000000000..985021dd8 --- /dev/null +++ b/src/platforms/mp-weixin/helpers/enhance-system-info.js @@ -0,0 +1,114 @@ +import { getDeviceBrand } from 'uni-shared' + +const UUID_KEY = '__DC_STAT_UUID' +let deviceId +export function useDeviceId(result) { + deviceId = deviceId || __GLOBAL__.getStorageSync(UUID_KEY) + if (!deviceId) { + deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7) + __GLOBAL__.setStorage({ + key: UUID_KEY, + data: deviceId + }) + } + result.deviceId = deviceId +} + +export function addSafeAreaInsets(result) { + if (result.safeArea) { + const safeArea = result.safeArea + result.safeAreaInsets = { + top: safeArea.top, + left: safeArea.left, + right: result.windowWidth - safeArea.right, + bottom: result.screenHeight - safeArea.bottom + } + } +} + +export function populateParameters(result) { + const { brand, model, system, language, theme, version, hostName, platform } = result + + // 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 = result.swanNativeVersion || version + } + + // deviceType + let deviceType = result.deviceType || 'phone' + if (__PLATFORM__ !== 'mp-baidu') { + const deviceTypeMaps = { + ipad: 'pad', + windows: 'pc', + mac: 'pc' + } + const deviceTypeMapsKeys = Object.keys(deviceTypeMaps) + 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 = (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 + + // 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 + ua: '', + hostTheme: '', + hostPackageName: '', + browserName: '', + browseVersion: '' + } + + Object.assign(result, parameters) +} \ No newline at end of file diff --git a/src/platforms/mp-weixin/helpers/system-info.js b/src/platforms/mp-weixin/helpers/system-info.js index d50013a53..6b132dac8 100644 --- a/src/platforms/mp-weixin/helpers/system-info.js +++ b/src/platforms/mp-weixin/helpers/system-info.js @@ -1,32 +1,9 @@ -const UUID_KEY = '__DC_STAT_UUID' -let deviceId -function addUuid (result) { - deviceId = deviceId || __GLOBAL__.getStorageSync(UUID_KEY) - if (!deviceId) { - deviceId = Date.now() + '' + Math.floor(Math.random() * 1e7) - __GLOBAL__.setStorage({ - key: UUID_KEY, - data: deviceId - }) - } - result.deviceId = deviceId -} - -function addSafeAreaInsets (result) { - if (result.safeArea) { - const safeArea = result.safeArea - result.safeAreaInsets = { - top: safeArea.top, - left: safeArea.left, - right: result.windowWidth - safeArea.right, - bottom: Math.abs(result.screenHeight - safeArea.bottom) - } - } -} +import { useDeviceId, addSafeAreaInsets, populateParameters } from './enhance-system-info' export default { returnValue: function (result) { - addUuid(result) + useDeviceId(result) addSafeAreaInsets(result) + populateParameters(result) } } diff --git a/src/platforms/mp-xhs/runtime/api/protocols.js b/src/platforms/mp-xhs/runtime/api/protocols.js index d945b3d2d..6f060a904 100644 --- a/src/platforms/mp-xhs/runtime/api/protocols.js +++ b/src/platforms/mp-xhs/runtime/api/protocols.js @@ -1,7 +1,7 @@ // import navigateTo from 'uni-helpers/navigate-to' // import redirectTo from '../../../mp-weixin/helpers/redirect-to' // import previewImage from '../../../mp-weixin/helpers/normalize-preview-image' -// import getSystemInfo from '../../../mp-weixin/helpers/system-info' +import getSystemInfo from '../../../mp-weixin/helpers/system-info' // import getUserProfile from '../../../mp-weixin/helpers/get-user-profile' // 需要做转换的 API 列表 @@ -9,8 +9,8 @@ export const protocols = { // navigateTo, // redirectTo, // previewImage, - // getSystemInfo, - // getSystemInfoSync: getSystemInfo, + getSystemInfo, + getSystemInfoSync: getSystemInfo // getUserProfile } diff --git a/src/shared/util.js b/src/shared/util.js index e62e68952..5ce89a600 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -195,3 +195,31 @@ export function deepClone (vnodes, createElement) { } export * from './uni-id-mixin' + +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) { + if (/iphone/gi.test(model) || /ipad/gi.test(model) || /mac/gi.test(model)) { return 'apple' } + if (/windows/gi.test(model)) { return 'microsoft' } +} -- GitLab