123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- const _toString = Object.prototype.toString
- const hasOwnProperty = Object.prototype.hasOwnProperty
- function hasOwn(obj, key) {
- return hasOwnProperty.call(obj, key)
- }
- function isPlainObject(obj) {
- return _toString.call(obj) === '[object Object]'
- }
- function isFn(fn) {
- return typeof fn === 'function'
- }
- function deepClone(obj) {
- return JSON.parse(JSON.stringify(obj))
- }
- function parseUrlParams(primitiveParams, context) {
- if (!primitiveParams) {
- return primitiveParams
- }
- let params = {}
- if(typeof primitiveParams === 'string') {
- params = primitiveParams.split('&').reduce((res, cur) => {
- const arr = cur.split('=')
- return Object.assign({
- [arr[0]]: arr[1]
- }, res)
- }, {})
- } else {
-
- for(let key in primitiveParams) {
- if(typeof primitiveParams[key] === 'number') {
- params[key] = primitiveParams[key] + ''
- } else {
- params[key] = primitiveParams[key]
- }
- }
- }
-
- const convertParams = {
-
- ak: 'appId',
-
- uid: 'uid',
-
- did: 'deviceId',
-
- up: 'uniPlatform',
-
- p: 'osName',
-
- on: 'osName',
-
- ip: 'clientIP',
-
- ua: 'userAgent',
-
- spid: 'spaceId',
-
- sppd: 'provider',
-
- v: 'appVersion',
-
- rn: 'romName',
-
- rv: 'romVersion',
-
- sv: 'osVersion',
-
- lang: 'osLanguage',
-
- ot: 'osTheme',
-
- dtp: 'deviceType',
-
- brand: 'deviceBrand',
-
- md: 'deviceModel',
-
- pr: 'devicePixelRatio',
-
- ww: 'windowWidth',
-
- wh: 'windowHeight',
-
- sw: 'screenWidth',
-
- sh: 'screenHeight',
- }
- context = context ? context : {}
- for (let key in convertParams) {
- if (!params[key] && context[convertParams[key]]) {
- params[key] = context[convertParams[key]]
- }
- }
- return params
- }
- function parseUrl(url) {
- if (typeof url !== "string" || !url) {
- return false
- }
- const urlInfo = url.split('?')
- baseurl = urlInfo[0]
- if (baseurl !== '/' && baseurl.indexOf('/') === 0) {
- baseurl = baseurl.substr(1)
- }
- return {
- path: baseurl,
- query: urlInfo[1] ? decodeURI(urlInfo[1]) : ''
- }
- }
- let createConfig
- try {
- createConfig = require('uni-config-center')
- } catch (e) {}
- function getConfig(file, key) {
- if (!file) {
- return false
- }
- const uniConfig = createConfig && createConfig({
- pluginId: 'uni-stat'
- })
- if (!uniConfig || !uniConfig.hasFile(file + '.json')) {
- console.error('Not found the config file')
- return false
- }
- const config = uniConfig.requireFile(file)
- return key ? config[key] : config
- }
- function sleep(ms) {
- return new Promise(resolve => setTimeout(() => resolve(), ms))
- }
- module.exports = {
- hasOwn,
- isPlainObject,
- isFn,
- deepClone,
- parseUrlParams,
- parseUrl,
- getConfig,
- sleep
- }
|