export const getRandomKey = () => { let key = '' + new Date().getTime() + Math.floor(Math.random() * 1000000) return key } const browser = () => { var u = navigator.userAgent // app = navigator.appVersion; return { trident: u.indexOf('Trident') > -1, //IE内核 presto: u.indexOf('Presto') > -1, //opera内核 webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核 gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile.*/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 // ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf('DAYAAPPA') > -1 || u.indexOf('Adr') > -1, //android终端 iPhone: u.indexOf('DAYAAPPI') > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf('iPad') > -1, //是否iPad webApp: u.indexOf('Safari') == -1, //是否web应该程序,没有头部与底部 weixin: u.indexOf('MicroMessenger') > -1, //是否微信 (2015-01-22新增) qq: u.match(/\sQQ/i) == " qq" //是否QQ } } const calcMinute = (minute) => { if (minute <= 0) { return '0分钟' } let minutes = minute % 60 // 算出分钟 let hours = 0 // 小时 if (minute >= 60) { hours = (minute - minutes) / 60 } let text = '' if (hours) { text = hours + '小时' } if (minutes) { text += minutes + '分钟' } return text } /** * 小于10的数变成 (0x) * @param {数值} num */ const getSTD = (num) => { return Number(num) >= 10 ? num : '0' + num } /** * 获取格式化的年月日 * @param {日期} time * @param {不是IOS里使用(多用于接口参数)} noIos */ const getYMD = (time, noIos) => { let tempDate = time || new Date() if (typeof (tempDate) == 'string') { tempDate = new Date(time.replace(/-/ig, '/')) } let t = noIos ? '-' : '/' let month = getSTD(tempDate.getMonth() + 1) let day = getSTD(tempDate.getDate()) return tempDate.getFullYear() + t + month + t + day } const getDateList = () => { let getNowYear = new Date().getFullYear() let getNowMonth = new Date().getMonth() + 1 const getDateList = {} for (let i = 0; i < 12; i++) { let months = [] for (let j = 0; j < (i == 0 ? getNowMonth : 12); j++) { months.push((j + 1) + '月') } getDateList[(getNowYear - i) + '年'] = months // 组合数据 } return getDateList } // 防抖 const _debounce = (fn, delay) => { delay = delay || 200 let timer return function () { let th = this let args = arguments if (timer) { clearTimeout(timer) } timer = setTimeout(function () { timer = null fn.apply(th, args) }, delay) }; } // 节流 const _throttle = (fn, time) => { let canRun = true return function () { let _arguments = arguments if (!canRun) return canRun = false setTimeout(() => { fn.call(this, _arguments) canRun = true }, time); } } // const _throttle = (fn, interval) => { // let last // let timer // interval = interval || 200 // return function () { // let that = this // let args = arguments // let now = +new Date(); // if(last && last - now < 2000){ // clearTimeout(timer) // } // timer = setTimeout(function () { // fn.apply(that, args) // last = +new Date() // }, 200) // } // } // 学生地址 const validStudentUrl = () => { let url = window.location.href let returnUrl = '' if (/test/.test(url)) { // test环境 returnUrl = 'http://mstutest.dayaedu.com' } else if (/dev/.test(url)) { // dev 环境 returnUrl = 'http://mstudev.dayaedu.com' } else if (/online/.test(url)) { //线上 returnUrl = 'https://mstuonline.dayaedu.com' } else { // 默认dev环境 returnUrl = 'http://mstudev.dayaedu.com' } return returnUrl } /** * 转换金额为保留2位小数 * @param x * @returns 强制保留2位小数的金额 */ const changeTwoDecimal = (amt) => { let f_x = parseFloat(amt); if (isNaN(f_x)) { return "0.00"; } f_x = Math.round(f_x * 100) / 100; let s_x = f_x.toString(); let pos_decimal = s_x.indexOf('.'); if (pos_decimal < 0) { pos_decimal = s_x.length; s_x += '.'; } while (s_x.length <= pos_decimal + 2) { s_x += '0'; } return s_x; } export { browser, calcMinute, getSTD, getYMD, getDateList, _debounce, _throttle, validStudentUrl, changeTwoDecimal }