diff --git a/src/platforms/app-plus/service/api/device/system.js b/src/platforms/app-plus/service/api/device/system.js index f183ddde4fc7404538ac62044b7856d96c2c1b3c..31cf2819bde1248941520b436e3ace5ed83aa979 100644 --- a/src/platforms/app-plus/service/api/device/system.js +++ b/src/platforms/app-plus/service/api/device/system.js @@ -1,10 +1,7 @@ -import { - callApiSync -} from '../util' - +import { callApiSync } from '../util' import { getWindowInfo } from './get-window-info' - import deviceId from 'uni-platform/helpers/uuid' +import { sortObject } from 'uni-shared' let systemInfo = {} let _initSystemInfo = true @@ -26,16 +23,16 @@ export function getDeviceInfo () { const _osName = osName.toLowerCase() return { + brand, deviceBrand: brand, deviceModel, devicePixelRatio: plus.screen.scale, deviceId: deviceId(), deviceOrientation, deviceType, - brand, model: deviceModel, - system: `${_osName === 'ios' ? 'iOS' : 'Android'} ${osVersion}`, - platform: _osName + platform: _osName, + system: `${_osName === 'ios' ? 'iOS' : 'Android'} ${osVersion}` } } @@ -54,23 +51,23 @@ export function getAppBaseInfo () { : hostLanguage return { - SDKVersion: '', - hostSDKVersion: '', - enableDebug: false, appId, appName, appVersion, appVersionCode, appLanguage, - version: plus.runtime.innerVersion, - language: osLanguage, - theme: '', + enableDebug: false, + hostSDKVersion: undefined, hostPackageName, hostName, hostVersion, hostLanguage, hostTheme, - hostFontSizeSetting: undefined + hostFontSizeSetting: undefined, + language: osLanguage, + SDKVersion: '', + theme: undefined, + version: plus.runtime.innerVersion } } @@ -87,24 +84,14 @@ export function getSystemInfo () { const appBaseInfo = getAppBaseInfo() _initSystemInfo = true - const { osName, osLanguage, osVersion } = systemInfo - const _osName = osName.toLowerCase() - const osLanguageSplit = osLanguage.split('-') - const osLanguageSplitLast = osLanguageSplit[osLanguageSplit.length - 1] - const _osLanguage = `${osLanguageSplit[0]}${osLanguageSplitLast ? '-' + osLanguageSplitLast : ''}` - const extraData = { errMsg: 'getSystemInfo:ok', fontSizeSetting: appBaseInfo.hostFontSizeSetting, - uniCompileVersion: __uniConfig.compilerVersion, - uniRuntimeVersion: __uniConfig.compilerVersion, - osLanguage: _osLanguage, - osName: _osName + osName: systemInfo.osName.toLowerCase() } - if (_osName === 'ios') { - extraData.romName = _osName - extraData.romVersion = osVersion + if (systemInfo.hostName) { + extraData.hostSDKVersion = systemInfo.uniRuntimeVersion } const _systemInfo = Object.assign( @@ -120,5 +107,5 @@ export function getSystemInfo () { delete _systemInfo.enableDebug delete _systemInfo.theme - return _systemInfo + return sortObject(_systemInfo) } diff --git a/src/platforms/h5/service/api/base/get-browser-info.js b/src/platforms/h5/service/api/base/get-browser-info.js index 3e0e2e51fa4bb99f29cb9a0e25b0f1bf395b8e4c..496df1e547ffc2f0a7beacbe4d0bf9a80f1739eb 100644 --- a/src/platforms/h5/service/api/base/get-browser-info.js +++ b/src/platforms/h5/service/api/base/get-browser-info.js @@ -21,11 +21,6 @@ function IEVersion () { } } -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' } -} - const ua = navigator.userAgent /** * 是否安卓设备 @@ -165,15 +160,15 @@ export function getBrowserInfo () { } else { osname = 'Other' osversion = '0' - deviceType = 'other' + deviceType = 'unknown' } var system = `${osname} ${osversion}` var platform = osname.toLocaleLowerCase() let browserName = '' - let browseVersion = String(IEVersion()) - if (browseVersion !== '-1') { browserName = 'IE' } else { + let browserVersion = String(IEVersion()) + if (browserVersion !== '-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++) { @@ -181,40 +176,31 @@ export function getBrowserInfo () { const reg = new RegExp(`(${vendor})/(\\S*)\\b`) if (reg.test(ua)) { browserName = vendors[index] - browseVersion = ua.match(reg)[2] + browserVersion = 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' const orientation = typeof window.screen.orientation === 'undefined' ? window.orientation : window.screen.orientation.angle deviceOrientation = Math.abs(orientation) === 90 ? 'landscape' : 'portrait' return { - deviceBrand, + deviceBrand: undefined, + brand: undefined, deviceModel: model, deviceOrientation, - brand: deviceBrand, model, system, platform, browserName: browserName.toLocaleLowerCase(), - browseVersion, + browserVersion, language, deviceType, ua, osname, osversion, - theme: '' + theme: undefined } } 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 50f6a43f449c1bfb5429c37f1e5e1158997e926b..25b5581dfcbe61ebf3d1a491e353cb1567594c5c 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 { getWindowInfo } from './get-window-info' import deviceId from 'uni-platform/helpers/uuid' import { getBrowserInfo } from '../base/get-browser-info' +import { sortObject } from 'uni-shared' let browserInfo = {} let _initBrowserInfo = true @@ -24,27 +25,22 @@ export function getDeviceInfo () { } = browserInfo return { + brand, deviceBrand, deviceModel, devicePixelRatio: window.devicePixelRatio, deviceId: deviceId(), deviceOrientation, deviceType, - brand, model, - system, - platform + platform, + system } } export function getAppBaseInfo () { initBrowserInfo() - const { - theme, - browserName, - browseVersion, - language - } = browserInfo + const { theme, language } = browserInfo const appLanguage = uni ? uni.getLocale @@ -53,23 +49,23 @@ export function getAppBaseInfo () { : language return { - SDKVersion: '', - hostSDKVersion: '', - enableDebug: false, - hostPackageName: '', - 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, - version: __uniConfig.appVersion + enableDebug: false, + hostSDKVersion: undefined, + hostPackageName: undefined, + hostFontSizeSetting: undefined, + hostName: undefined, + hostVersion: undefined, + hostTheme: undefined, + hostLanguage: undefined, + language, + SDKVersion: '', + theme, + version: '' } } @@ -85,7 +81,7 @@ export function getSystemInfoSync () { const appBaseInfo = getAppBaseInfo() _initBrowserInfo = true - const { ua, browserName, browseVersion, osname, osversion } = browserInfo + const { ua, browserName, browserVersion, osname, osversion } = browserInfo const systemInfo = Object.assign( {}, @@ -93,17 +89,17 @@ export function getSystemInfoSync () { deviceInfo, appBaseInfo, { - ua, browserName, - browseVersion, - uniPlatform: 'web', - uniCompileVersion: __uniConfig.compilerVersion, - uniRuntimeVersion: __uniConfig.compilerVersion, + browserVersion, fontSizeSetting: appBaseInfo.hostFontSizeSetting, osName: osname.toLocaleLowerCase(), osVersion: osversion, osLanguage: undefined, - osTheme: undefined + osTheme: undefined, + uniPlatform: 'web', + uniCompileVersion: __uniConfig.compilerVersion, + uniRuntimeVersion: __uniConfig.compilerVersion, + ua } ) @@ -111,7 +107,7 @@ export function getSystemInfoSync () { delete systemInfo.enableDebug delete systemInfo.theme - return systemInfo + return sortObject(systemInfo) } /** * 获取系统信息-异步 diff --git a/src/platforms/mp-weixin/helpers/enhance-system-info.js b/src/platforms/mp-weixin/helpers/enhance-system-info.js index ec8db13595fc02f582738b83afcfbbd332d6c19c..dd25282387df41fc47b0c74a93c8d77f48c66b7a 100644 --- a/src/platforms/mp-weixin/helpers/enhance-system-info.js +++ b/src/platforms/mp-weixin/helpers/enhance-system-info.js @@ -1,8 +1,4 @@ -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' } - return '' -} +import { getLocale } from 'uni-core/runtime/locale' const UUID_KEY = '__DC_STAT_UUID' let deviceId @@ -34,11 +30,10 @@ export function populateParameters (result) { const { brand = '', model = '', system = '', language = '', theme, version, - hostName, platform, fontSizeSetting, - SDKVersion, pixelRatio, deviceOrientation, - environment + platform, fontSizeSetting, + SDKVersion, pixelRatio, deviceOrientation } = result - const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1 + // const isQuickApp = __PLATFORM__.indexOf('quickapp-webview') !== -1 // osName osVersion let osName = '' @@ -63,22 +58,10 @@ export function populateParameters (result) { const deviceType = getGetDeviceType(result, model) // deviceModel - const deviceBrand = getDeviceBrand(brand, model, isQuickApp) + const deviceBrand = getDeviceBrand(brand) // hostName - const _platform = __PLATFORM__ === 'mp-weixin' ? 'WeChat' : __PLATFORM__.split('-')[1] - let _hostName = hostName || _platform // mp-jd - if (__PLATFORM__ === 'mp-weixin') { - if (environment) { - _hostName = environment - } else if (result.host && result.host.env) { - _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 + const _hostName = getHostName(result) // deviceOrientation let _deviceOrientation = deviceOrientation // 仅 微信 百度 支持 @@ -92,6 +75,9 @@ export function populateParameters (result) { let _SDKVersion = SDKVersion if (__PLATFORM__ === 'mp-alipay') { _SDKVersion = my.SDKVersion } + // hostLanguage + const hostLanguage = language.replace(/_/g, '-') + // wx.getAccountInfoSync const parameters = { @@ -99,6 +85,7 @@ export function populateParameters (result) { appName: process.env.UNI_APP_NAME, appVersion: process.env.UNI_APP_VERSION_NAME, appVersionCode: process.env.UNI_APP_VERSION_CODE, + appLanguage: getAppLanguage(hostLanguage), uniCompileVersion: process.env.UNI_COMPILER_VERSION, uniRuntimeVersion: process.env.UNI_COMPILER_VERSION, uniPlatform: process.env.UNI_SUB_PLATFORM || process.env.UNI_PLATFORM, @@ -111,7 +98,7 @@ export function populateParameters (result) { osVersion, hostTheme: theme, hostVersion, - hostLanguage: language.replace('_', '-'), + hostLanguage, hostName: _hostName, hostSDKVersion: _SDKVersion, hostFontSizeSetting: fontSizeSetting, @@ -123,7 +110,7 @@ export function populateParameters (result) { ua: undefined, hostPackageName: undefined, browserName: undefined, - browseVersion: undefined + browserVersion: undefined } Object.assign(result, parameters) @@ -150,20 +137,34 @@ export function getGetDeviceType (result, model) { return deviceType } -export function getDeviceBrand ( - brand, - model, - isQuickApp = false -) { - let deviceBrand = model.split(' ')[0].toLocaleLowerCase() - if ( - __PLATFORM__ === 'mp-toutiao' || - __PLATFORM__ === 'mp-lark' || - isQuickApp - ) { +export function getDeviceBrand (brand) { + let deviceBrand = brand + if (deviceBrand) { deviceBrand = brand.toLocaleLowerCase() - } else { - deviceBrand = _getDeviceBrand(deviceBrand) } return deviceBrand } + +export function getAppLanguage (defaultLanguage) { + return getLocale + ? getLocale() + : defaultLanguage +} + +export function getHostName (result) { + const _platform = __PLATFORM__ === 'mp-weixin' ? 'WeChat' : __PLATFORM__.split('-')[1] + let _hostName = result.hostName || _platform // mp-jd + if (__PLATFORM__ === 'mp-weixin') { + if (result.environment) { + _hostName = result.environment + } else if (result.host && result.host.env) { + _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 + + return _hostName +} diff --git a/src/platforms/mp-weixin/helpers/get-app-base-info.js b/src/platforms/mp-weixin/helpers/get-app-base-info.js index cc3abbdec9c2f345f5da4ec393138c975beb30d8..14be62f1dec1b16c7194da07572000d98744ea94 100644 --- a/src/platforms/mp-weixin/helpers/get-app-base-info.js +++ b/src/platforms/mp-weixin/helpers/get-app-base-info.js @@ -1,24 +1,25 @@ +import { getAppLanguage, getHostName } from './enhance-system-info' +import { sortObject } from 'uni-shared' + export default { returnValue: function (result) { const { version, language, SDKVersion, theme } = result - let _hostName = __PLATFORM__.split('-')[1] // mp-jd - if (__PLATFORM__ === 'mp-weixin') { - if (result.host && result.host.env) { - _hostName = result.host.env - } - } + const _hostName = getHostName(result) - Object.assign(result, { - hostVersion: version, - hostLanguage: language.replace('_', '-'), - hostName: _hostName, - hostSDKVersion: SDKVersion, - hostTheme: theme, + const hostLanguage = language.replace('_', '-') + + result = sortObject(Object.assign(result, { 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 - }) + appVersionCode: process.env.UNI_APP_VERSION_CODE, + appLanguage: getAppLanguage(hostLanguage), + hostVersion: version, + hostLanguage, + hostName: _hostName, + hostSDKVersion: SDKVersion, + hostTheme: theme + })) } } diff --git a/src/platforms/mp-weixin/helpers/get-device-info.js b/src/platforms/mp-weixin/helpers/get-device-info.js index 96ee3b92bf48b9623af6efd3a1576b1be1d86c95..ad29e9b78f63260b0ffbef2a2224c39495ac9e60 100644 --- a/src/platforms/mp-weixin/helpers/get-device-info.js +++ b/src/platforms/mp-weixin/helpers/get-device-info.js @@ -1,16 +1,17 @@ import { useDeviceId, getGetDeviceType, getDeviceBrand } from './enhance-system-info' +import { sortObject } from 'uni-shared' export default { returnValue: function (result) { const { brand, model } = result const deviceType = getGetDeviceType(result, model) - const deviceBrand = getDeviceBrand(brand, model) + const deviceBrand = getDeviceBrand(brand) useDeviceId(result) - Object.assign(result, { + result = sortObject(Object.assign(result, { deviceType, deviceBrand, deviceModel: model - }) + })) } } diff --git a/src/platforms/mp-weixin/helpers/get-window-info.js b/src/platforms/mp-weixin/helpers/get-window-info.js index bda0ccb1c1515212598f59f689dbedbc91c5ad58..8830569555457b30ae32c02ae381aaed8400d607 100644 --- a/src/platforms/mp-weixin/helpers/get-window-info.js +++ b/src/platforms/mp-weixin/helpers/get-window-info.js @@ -1,12 +1,13 @@ import { addSafeAreaInsets } from './enhance-system-info' +import { sortObject } from 'uni-shared' export default { returnValue: function (result) { addSafeAreaInsets(result) - Object.assign(result, { + result = sortObject(Object.assign(result, { windowTop: 0, windowBottom: 0 - }) + })) } } diff --git a/src/shared/util.js b/src/shared/util.js index e62e68952c8a05eb156cd7dd8bdfb6601228d567..7737f64ae2ea2ff6f52947ac0b19cca5c6d4813c 100644 --- a/src/shared/util.js +++ b/src/shared/util.js @@ -25,7 +25,7 @@ export function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } -export function noop () {} +export function noop () { } export function toRawType (val) { return _toString.call(val).slice(8, -1) @@ -195,3 +195,13 @@ export function deepClone (vnodes, createElement) { } export * from './uni-id-mixin' + +export function sortObject (obj) { + const sortObj = {} + if (isPlainObject(obj)) { + Object.keys(obj).sort().forEach(key => { + sortObj[key] = obj[key] + }) + } + return !Object.keys(sortObj) ? obj : sortObj +}