/** * utils.js * 各类js工具函数 */ /* * 时间格式化函数(将时间格式化为,2019年08月12日,2019-08-12,2019/08/12的形式) * * * pattern参数(想要什么格式的数据就传入什么格式的数据) * · 'yyyy-MM-dd' ---> 输出如2019-09-20 * · 'yyyy-MM-dd hh:mm' ---> 输出如2019-09-20 08:20 * · 'yyyy-MM-dd HH:mm:ss' ---> 输出如2019-09-20 18:20:23 * · 'yyyy/MM/dd' ---> 输出如2019/09/20 * · 'yyyy年MM月dd日' ---> 输出如2019年09月20日 * · 'yyyy年MM月dd日 hh时mm分' ---> 输出如2019年09月20日 08时20分 * · 'yyyy年MM月dd日 hh时mm分ss秒' ---> 输出如2019年09月20日 08时20分23秒 * · 'yyyy年MM月dd日 hh时mm分ss秒 EE' ---> 输出如2019年09月20日 08时20分23秒 周二 * · 'yyyy年MM月dd日 hh时mm分ss秒 EEE' ---> 输出如2019年09月20日 08时20分23秒 星期二 * * 参考: https://www.cnblogs.com/mr-wuxiansheng/p/6296646.html */ Date.prototype.pattern = function (fmt) { if (!fmt) return '' var o = { 'y+': this.getFullYear(), 'M+': this.getMonth() + 1, // 月份 'd+': this.getDate(), // 日 // 12小时制 'h+': this.getHours() % 12 == 0 ? 12 : this.getHours() % 12, // 小时 // 24小时制 'H+': this.getHours(), // 小时 'm+': this.getMinutes(), // 分 's+': this.getSeconds(), // 秒 'q+': Math.floor((this.getMonth() + 3) / 3), // 季度 'S': this.getMilliseconds(), // 毫秒 'E+': this.getDay(), // 周 }; var week = { '0': '日', '1': '一', '2': '二', '3': '三', '4': '四', '5': '五', '6': '六' }; if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length)); } if (/(E+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length > 1) ? (RegExp.$1.length > 2 ? '星期' : '周') : '') + week[ this.getDay() + '']); } for (var k in o) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]) .length))); } } return fmt; } /* 挂载到Number原型上,格式化小数用tofixed会造成位数不够补零的情况(处理不让自动补零) 栗子: 不是number类型的需要转一下 Number('').fixed() Number('213412DDD').fixed() 1234.152354.fixed() 1234.152354.fixed(2) 1234.152354.fixed(10) */ Number.prototype.fixed = function(len){ len = isNaN(len)? 0 : len var num = Math.pow(10,len) return Math.round(this*num) / num } var popupTip = { setAlert: function (obj) { var html = `

试用功能数额已超限,请购买剑鱼大会员正式版

` obj.$dialog.alert({ className: 'shiyong_common', title: '温馨提醒', message: html, theme: 'round-button', confirmButtonText:'取消,暂不使用', confirmButtonColor:'#2ABED1' }).then(() => { // on close }); } } var versionChangeLog = { '0.0.1': ['init'], '0.0.2': ['utils.dateFromNow 函数逻辑修改,具体修改内容查看函数注释'], '0.0.3': ['utils.formatMoney 新增不使用,分割返回值的选项'], '0.0.4': ['utils.checkNowInAppTabbarPage 判断是否app tabbar 页面,返回对应的key'], '0.0.5': ['新增utils.$env, utils.$envs 等变量'], } if (!window.utilsEnv) { // $env, $envs的辅助函数 window.utilsEnv = { // 获取当前是android或者ios androidOrIOS: function () { var u = navigator.userAgent.toLowerCase() var agent = null if (/iphone|ipod|ipad|ios/.test(u)) { agent = 'ios' } else if (/(Android)/i.test(u)) { agent = 'android' } return agent }, getIsH5HostName: function () { var hostname = location.hostname.toLowerCase() return hostname.indexOf('h5') !== -1 }, // 是否是在app里面运行 getIsInTheAppContainer: function () { // 判断是否在app环境下 var inApp = false var u = navigator.userAgent.toLowerCase() if (u.indexOf('jianyuapp') !== -1) { inApp = true return inApp } try { if (this.androidOrIOS() === 'ios') { var iniOSApp = typeof window.webkit.messageHandlers.skipAppointTab.postMessage === 'function' inApp = iniOSApp } else { var inAndroidApp = typeof window.JyObj !== 'undefined' inApp = inAndroidApp } } catch (e) { console.warn(e) inApp = false } return inApp }, getPlatformEnvs: function () { var inWX = navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1 var inApp = this.getIsInTheAppContainer() var platformOS = this.androidOrIOS() var getPlatform = function () { var h5host = this.getIsH5HostName() if (inApp) { return 'app' } else if (h5host) { return 'h5' } else if (inWX) { return 'wx' } else { return 'h5' } } var platform = getPlatform() return { platformOS: platformOS, platform: platform, inApp: inApp, inWX: inWX } } } utilsEnv.platformEnvs = utilsEnv.getPlatformEnvs() } var utils = { // utils版本号 version: '0.0.5', $env: { appVersion: '', // app版本号 platformOS: window.utilsEnv.platformEnvs.platformOS, // android或者ios platform: window.utilsEnv.platformEnvs.platform, // wx/app/h5 }, $envs: { inWX: window.utilsEnv.platformEnvs.platform === 'wx', // 是否在微信中 inH5: window.utilsEnv.platformEnvs.platform === 'h5', // 是否在h5中 inApp: window.utilsEnv.platformEnvs.inApp, // 是否在app中 inAndroid: window.utilsEnv.platformEnvs.platformOS === 'android', // 是否在android中 inIOS: window.utilsEnv.platformEnvs.platformOS === 'ios', // 是否在android中 }, isWeiXinBrowser: navigator.userAgent.toLowerCase().indexOf('micromessenger') !== -1, isAndroid: navigator.userAgent.toLowerCase().indexOf('android') !== -1, isIos: /iphone|ipod|ipad|ios/.test(navigator.userAgent.toLowerCase()), // 传入你要获取的参数的名字 getParam: function (name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i'); var r = window.location.search.substr(1).match(reg); //获取url中'?'符后的字符串并正则匹配 var context = ''; if (r != null) context = r[2]; // 释放变量 reg = null; r = null; return context == null || context == '' || context == 'undefined' ? '' : context; }, // 取[m, n]随机数 getRandomNumber: function (min, max) { return Math.floor(Math.random() * (max - min + 1) + min); }, // 获取唯一的uuid // https://www.kancloud.cn/ifeng/js100/622666 getRandomUuid: function () { var s = []; var hexDigits = "0123456789abcdef"; for (var i = 0; i < 36; i++) { s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1); } s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010 s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01 s[8] = s[13] = s[18] = s[23] = "-"; var uuid = s.join(""); return uuid; }, // 获取随机字符串 // 不传参数则获取长度不固定的字符串 getRandomString: function (len) { var randomString = ''; if (len) { /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/ var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'; var maxPos = $chars.length; for (i = 0; i < len; i++) { randomString += $chars.charAt(Math.floor(Math.random() * maxPos)); } } else { // Math.random() 生成随机数字, eg: 0.123456 // .toString(36) 转化成36进制 : "0.4fzyo82mvyr" // .substring(2) 去掉前面两位 : "yo82mvyr" // .slice(-8) 截取最后八位 : "yo82mvyr" randomString = Math.random().toString(36).substring(2) } return randomString; }, // FROM: https://www.jianshu.com/p/90ed8b728975 // 比较两个对象是否相等 // 返回true为相等,返回false为不相等 deepCompare: function (x, y) { var i, l, leftChain, rightChain; function compare2Objects(x, y) { var p; // remember that NaN === NaN returns false // and isNaN(undefined) returns true if (isNaN(x) && isNaN(y) && typeof x === 'number' && typeof y === 'number') { return true; } // Compare primitives and functions. // Check if both arguments link to the same object. // Especially useful on the step where we compare prototypes if (x === y) { return true; } // Works in case when functions are created in constructor. // Comparing dates is a common scenario. Another built-ins? // We can even handle functions passed across iframes if ((typeof x === 'function' && typeof y === 'function') || (x instanceof Date && y instanceof Date) || (x instanceof RegExp && y instanceof RegExp) || (x instanceof String && y instanceof String) || (x instanceof Number && y instanceof Number)) { return x.toString() === y.toString(); } // At last checking prototypes as good as we can if (!(x instanceof Object && y instanceof Object)) { return false; } if (x.isPrototypeOf(y) || y.isPrototypeOf(x)) { return false; } if (x.constructor !== y.constructor) { return false; } if (x.prototype !== y.prototype) { return false; } // Check for infinitive linking loops if (leftChain.indexOf(x) > -1 || rightChain.indexOf(y) > -1) { return false; } // Quick checking of one object being a subset of another. // todo: cache the structure of arguments[0] for performance for (p in y) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } } for (p in x) { if (y.hasOwnProperty(p) !== x.hasOwnProperty(p)) { return false; } else if (typeof y[p] !== typeof x[p]) { return false; } switch (typeof (x[p])) { case 'object': case 'function': leftChain.push(x); rightChain.push(y); if (!compare2Objects(x[p], y[p])) { return false; } leftChain.pop(); rightChain.pop(); break; default: if (x[p] !== y[p]) { return false; } break; } } return true; } if (arguments.length < 1) { return true; //Die silently? Don't know how to handle such case, please help... // throw "Need two or more arguments to compare"; } for (i = 1, l = arguments.length; i < l; i++) { leftChain = []; //Todo: this can be cached rightChain = []; if (!compare2Objects(arguments[0], arguments[i])) { return false; } } return true; }, // 判断变量是否是数组 isArray: function (o){ return Object.prototype.toString.call(o) == '[object Array]'; }, // 数组对象根据某一个值进行从小到大冒泡排序 // arr 数组 // value 字符串 bSort: function (arr, value) { var len = arr.length; for (var i = 0; i < len - 1; i++) { for (var j = 0; j < len - 1 - i; j++) { // 相邻元素两两对比,元素交换,大的元素交换到后面 if (arr[j][value] > arr[j + 1][value]) { var temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } return arr; }, // 数组去重 unique: function (arr) { if (!this.isArray(arr)) { console.log('type error!') return } var array = []; for (var i = 0; i < arr.length; i++) { if (array .indexOf(arr[i]) === -1) { array .push(arr[i]) } } return array }, // 数组乱序 shuffle: function (arr) { var targetArr = JSON.parse(JSON.stringify(arr || [])) var hitArr = [] var len = targetArr.length for (var i = 0; i < len; i++) { // 获取随机下标 var index = Math.floor(Math.random() * targetArr.length) hitArr.push(targetArr[index]) targetArr.splice(index, 1) } return hitArr.concat(targetArr) }, // 解决ios返回不刷新页面的问题 iosBackRefresh: function () { var isPageHide = false; let superSearch_iosNoRefresh = sessionStorage.getItem('superSearch_iosNoRefresh') window.addEventListener('pageshow', function () { if (isPageHide) { // ios从三级页返回不刷新页面 if(!superSearch_iosNoRefresh) { location.reload(); } else { sessionStorage.removeItem('superSearch_iosNoRefresh') } } else { if(superSearch_iosNoRefresh) { sessionStorage.removeItem('superSearch_iosNoRefresh') } } }); window.addEventListener('pagehide', function () { isPageHide = true; }); }, // ios返回调用函数 iosBackInvoke: function (callback) { var isPageHide = false; window.addEventListener('pageshow', function () { if (isPageHide) { callback && callback(); } }); window.addEventListener('pagehide', function () { isPageHide = true; }); }, /** * 格式化金钱的函数 * @param {number} s 金额必传 * @param {int:0-100} n 保留小数的位数(int:0-100) * @param {Boolean} withoutComma 传true则表示不使用,分割返回值 */ formatMoney: function (s, n, withoutComma) { // 如果不传s或者s为空,则直接返回0 if (!s) return 0 if (n === undefined || n === null) { n = -1 } else { n = n > 0 && n <= 20 ? n : 2; } var intS = parseInt(s) var point = '.' var left; var right; s = parseFloat((s + '').replace(/[^\d\.-]/g, '')) // 没传n,默认(如果为整数,则不保留小数。如果为浮点数,则保留两位小数) if (n === -1) { if (s === intS) { n = 0 right = '' point = '' } else { n = 2 s = s.toFixed(n); right = s.split('.')[1]; } s = s + '' left = s.split('.')[0].split('').reverse(); } else { s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + ''; left = s.split('.')[0].split('').reverse(); right = s.split('.')[1]; } // 默认进行,拼接 if (!withoutComma) { t = ''; for (i = 0; i < left.length; i++) { t += left[i] + ((i + 1) % 3 == 0 && (i + 1) != left.length ? ',' : ''); } return t.split('').reverse().join('') + point + right; } return left.reverse().join('') + point + right;; }, // 金额大写,链接:https://juejin.im/post/5a2a7a5051882535cd4abfce // upDigit(1682) result:"人民币壹仟陆佰捌拾贰元整" // upDigit(-1693) result:"欠壹仟陆佰玖拾叁元整" upPrice: function (n) { var fraction = ['角', '分', '厘'] var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'] var unit = [ ['元', '万', '亿'], ['', '拾', '佰', '仟'] ] // var head = n < 0 ? '欠人民币' : '人民币' var head = '' n = Math.abs(n) var s = '' for (var i = 0; i < fraction.length; i++) { s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '') } s = s || '整' n = Math.floor(n) for (var i = 0; i < unit[0].length && n > 0; i++) { var p = '' for (var j = 0; j < unit[1].length && n > 0; j++) { p = digit[n % 10] + unit[1][j] + p n = Math.floor(n / 10) } s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s // s = p + unit[0][i] + s; } return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整') }, // 手机号中间4位加* addConfusionForTel: function (tel) { var reg = /^(\d{3})\d{4}(\d{4})$/ return tel.replace(reg, '$1****$2') }, // 手机号加空格 addSpaceForTel: function (tel) { var regMap = { isConfuse: /^(\d{3})\*{4}(\d{4})$/, addSpace: /^(\d{3})(\d{4})(\d{4})$/ } var confusion = regMap.isConfuse.test(tel) if (confusion) { return tel.replace(regMap.isConfuse, '$1 **** $2') } else { return tel.replace(regMap.addSpace, '$1 $2 $3') } }, /** * 时间戳转换 多少秒、多少分、多少小时前 昨天以及昨天之前的显示年月日(今年的时间不显示年份) * @param {Date} originTime 要转换的时间对象(或者时间戳) */ dateFromNow: function (originTime, useOld) { if (!originTime) return // 原始时间 - 传入的时间戳 var originTimeStamp = +new Date(originTime) // 当前时间戳 var nowTimeStamp = +new Date() // 时间戳相差多少 var diffTimeStamp = nowTimeStamp - originTimeStamp var postfix = diffTimeStamp > 0 ? '前' : '后' // 求绝对值 ms(毫秒) var diffTimeStampAbsMs = Math.abs(diffTimeStamp) var diffTimeStampAbsS = Math.round(diffTimeStampAbsMs / 1000) // 10天的秒数 var days11 = 11 * 24 * 60 * 60 // var dataMap = { // zh: ['年', '个月', '星期', '天', '小时', '分钟', '秒'], // number: [365*24*60*60, 30*24*60*60, 7*24*60*60, 24*60*60, 60*60, 60, 1] // } var dataMap = { zh: ['天', '小时', '分钟', '秒'], number: [24 * 60 * 60, 60 * 60, 60, 1] } var timeString = '' // 10天前 var tenDaysAgo = diffTimeStampAbsS > days11 // 是否是当天 var isCurrentDay = new Date(originTimeStamp).pattern('yyyy.MM.dd') === new Date().pattern('yyyy.MM.dd') var condition = !isCurrentDay if (useOld) { condition = tenDaysAgo } if (condition) { // 不是当天,则使用正常日期显示 var originDate = new Date(originTimeStamp) var nowDate = new Date() // 是否同年 var sameYear = originDate.getFullYear() === nowDate.getFullYear() // 如果是当年,则不显示年 var patternString = sameYear ? 'MM-dd' : 'yyyy-MM-dd' timeString = originDate.pattern(patternString) } else { for (var i = 0; i < dataMap.number.length; i++) { var inm = Math.floor(diffTimeStampAbsS / dataMap.number[i]) if (inm != 0) { timeString = inm + dataMap.zh[i] + postfix break } } } return timeString }, /** * 输入框获取焦点,隐藏底部固定按钮方法 * @param {Array} inputs 传入输入框的DOM对象 * @param {Object} footer 按键弹起要隐藏的footer的 DOM对象 */ inputFocusHideFooter: function (inputs, footer) { var isShowBtn = function (f) { if (f) { footer.style.display = '' } else { footer.style.display = 'none' } } // ios监听输入框的软键盘弹起和收起事件 if (this.isIos) { inputs.forEach(function (item) { item.addEventListener('focus', function () { console.log('IOS 键盘弹出') // IOS 键盘弹起后操作 isShowBtn(false) }, false) // IOS 键盘收起:IOS 点击输入框以外区域或点击收起按钮,输入框都会失去焦点,键盘会收起, item.addEventListener('blur', function () { console.log('IOS 键盘收起') // IOS 键盘收起后操作 isShowBtn(true) }) }) } // Android 键盘收起:Android 键盘弹起或收起页面高度会发生变化,以此为依据获知键盘收起 if (this.isAndroid) { var clientHeight = document.documentElement.clientHeight || document.body.clientHeight window.addEventListener('resize', function () { var nowClientHeight = document.documentElement.clientHeight || document.body.clientHeight if (clientHeight > nowClientHeight) { // 键盘弹出的事件处理 console.log('Android 键盘弹出') isShowBtn(false) } else { console.log('Android 键盘收起') // 键盘收起的事件处理 isShowBtn(true) } }, false) } }, /** * 通用关键字高亮替换 * @param {String} value 要高亮的字符串 * @param {String|Array} oldChar 要被替换的字符串(或数组) * @param {String|Array} newChar 要替换成的字符串(或数组) * * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` 高亮 * 则此时 value -> `剑鱼标讯工具函数` * oldChar -> `工具` * newChar -> `工具` * * 批量高亮----- * 比如:要将 - `剑鱼标讯工具函数` 字符串中的 `工具` `剑鱼` 高亮 * 则此时 value -> `剑鱼标讯工具函数批量高亮` * oldChar -> ['工具', '剑鱼'] * newChar -> ['', ''] * * 注意:此时newChar为一个长度为2的数组,数组中为高亮标签的起始标签和结束标签 * */ replaceKeyword: function (value, oldChar, newChar) { var _this = this if (!oldChar || !newChar) return value // oldChar的字符串数组,用来循环替换 var oldCharArr = [] if (this.isArray(oldChar)) { oldCharArr = oldChar.concat() } else { oldCharArr.push(oldChar) } // 数组去重 oldCharArr = this.unique(oldCharArr) try { oldCharArr.forEach(function (item) { // 去空格之后为空字符串,则直接跳过当前替换 if (item.replace(/\s+/g, '')) { var oc = item oc = oc.replace(/\$/g, '\\$') .replace(/\(/g, '\\(') .replace(/\)/g, '\\)') .replace(/\*/g, '\\*') .replace(/\+/g, '\\+') .replace(/\./g, '\\.') .replace(/\[/g, '\\[') .replace(/\]/g, '\\]') .replace(/\?/g, '\\?') .replace(/\\/g, '\\') .replace(/\//g, '\\/') .replace(/\^/g, '\\^') .replace(/\{/g, '\\{') .replace(/\}/g, '\\}') .replace(/\|/g, '\\|') if (_this.isArray(newChar)) { // 批量高亮 var tempV = value value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar[0] + oc + newChar[1]) if (value === tempV && oc.indexOf('+') !== -1) { var splitReg = oc.split('\\+') splitReg.map(function (v) { value = value.replace(new RegExp('('+v+')', 'gmi'), newChar[0] + v + newChar[1]) }) } } else { // 普通单个高亮 value = value.replace(new RegExp('(' + oc + ')', 'gmi'), newChar) } } }) } catch (e) { console.log(e) return value } return value }, /** * 批量删除sessionStorage中的数据 * @param {RegExp} reg 传入一个要匹配的正则表达式 */ clearBatchSessionStorage: function (reg) { if (sessionStorage) { var sessKeys = Object.keys(sessionStorage) sessKeys.forEach(function (item) { if (reg.test(item)) { sessionStorage.removeItem(item) } }) } }, /** * 获取图片原始宽高 * @param {Image/Object} img * @param {Function} callback */ getImgNaturalDimensions: function (img, callback) { var naturalSize = {} if (img.naturalWidth && img.naturalHeight) { // 现代浏览器 naturalSize.with = img.naturalWidth naturalSize.height = img.naturalHeight } else { // IE6/7/8 var image = new Image() image.src = img.src image.onload = function() { naturalSize.with = image.width naturalSize.height = image.height callback && callback(naturalSize) } } return naturalSize }, goToCustomerPage: function () { if (this.isWeiXinBrowser) { location.href = '/big/wx/page/customer' } else { location.href = '/jyapp/free/customer' } }, moneyUnit: function (num, type, lv) { if (num === '' || num == null || num == undefined || isNaN(num)) return '' var levelArr = ['元', '万元', '亿元', '万亿元'] if (type === void 0) { type = 'string'; } if (lv === void 0) { lv = 0; } if (num === 0) { if (type === 'string') { return '0'; } if (type === 'lv') { return levelArr[lv]; } if (type === 'number') { return 0; } if (type === 'index') { return lv; } } // 需要传入固定的lv(此时lv为 levelArr 中的一个) function transfer (num, lvString) { var index = levelArr.indexOf(lvString) if (index === -1 || index === 0) { return num } else { return (num / Math.pow(10000, index)).toFixed(2) + lvString } } if (type === 'transfer') { return transfer(num, lv) } var result = num / Math.pow(10000, lv); if (result > 10000 && lv < 2) { return this.moneyUnit(num, type, lv + 1) } else { if (type === 'string') { return String(Math.floor(result * 100) / 100).replace('.00', '') + levelArr[lv]; } if (type === 'lv') { return levelArr[lv]; } if (type === 'index') { return lv; } if (type === 'number') { return String(Math.floor(result * 100) / 100).replace('.00', ''); } } }, /** * 防抖 * @param {Function} func 函数 * @param {Number} delay 延时间隔 默认 200ms * @param {Boolean} immediate 是否首次执行 */ debounce: function (func, delay, immediate){ var timer = null; delay = delay || 200 return function() { var context = this; var args = arguments; if(timer) clearTimeout(timer); if(immediate){ var doNow = !timer; timer = setTimeout(function(){ timer = null; },delay); if(doNow){ func.apply(context,args); } }else{ timer = setTimeout(function(){ func.apply(context,args); },delay); } } }, /** * 检查是否 app tabbar页面,并返回对应的key || '' * @param link - 需要检查的地址 || location.href * @returns {string} - 对应的key */ checkNowInAppTabbarPage: function (link) { const appTabbars = { search: ['/jyapp/jylab/mainSearch', '/jy_mobile/tabbar/home'], subscribe: ['/jyapp/swordfish/historypush', '/jy_mobile/tabbar/subscribe'], message: ['/jyapp/frontPage/messageCenter/sess/index'], box: ['/jyapp/jylab/index', '/jy_mobile/tabbar/box'], me: ['/jyapp/free/me'] } let appTabbarName = '' for (const appTabbarsKey in appTabbars) { const hasTabbar = appTabbars[appTabbarsKey].some(function (v) { return (link || location.href).indexOf(v) > -1 }) if (hasTabbar) { appTabbarName = appTabbarsKey break } } return appTabbarName } } Object.defineProperty(utils.$env, 'appVersion', { get () { var version try { version = JyObj.getVersion() } catch (e) { console.warn('error: get app version failed') } return version } })