import CryptoJS from '@/common/crypto-js.js' function formatTime(time) { if (typeof time !== 'number' || time < 0) { return time } var hour = parseInt(time / 3600) time = time % 3600 var minute = parseInt(time / 60) time = time % 60 var second = time return ([hour, minute, second]).map(function(n) { n = n.toString() return n[1] ? n : '0' + n }).join(':') } // 回显数据字典 const selectDictLabel = (datas, value) => { var actions = []; if (!datas) { return; } Object.keys(datas).some((key) => { if (datas[key].dictValue && datas[key].dictValue == value) { actions.push(datas[key].dictLabel); return true; } else { if (datas[key].id == value) { actions.push(datas[key].typeName); return true; } } }) return actions.join(''); } function formatLocation(longitude, latitude) { if (typeof longitude === 'string' && typeof latitude === 'string') { longitude = parseFloat(longitude) latitude = parseFloat(latitude) } longitude = longitude.toFixed(2) latitude = latitude.toFixed(2) return { longitude: longitude.toString().split('.'), latitude: latitude.toString().split('.') } } //获取两日期之间日期列表函数 function getdiffdate(stime, etime) { //初始化日期列表,数组 var diffdate = new Array(); var i = 0; //开始日期小于等于结束日期,并循环 while (stime <= etime) { diffdate[i] = stime; //获取开始日期时间戳 var stime_ts = new Date(stime).getTime(); //增加一天时间戳后的日期 var next_date = stime_ts + (24 * 60 * 60 * 1000); //拼接年月日,这里的月份会返回(0-11),所以要+1 var next_dates_y = new Date(next_date).getFullYear() + '-'; var next_dates_m = (new Date(next_date).getMonth() + 1 < 10) ? '0' + (new Date(next_date).getMonth() + 1) + '-' : (new Date(next_date).getMonth() + 1) + '-'; var next_dates_d = (new Date(next_date).getDate() < 10) ? '0' + new Date(next_date).getDate() : new Date( next_date).getDate(); stime = next_dates_y + next_dates_m + next_dates_d; //增加数组key i++; } return diffdate; } /** * 计算两个日期之间的天数 * @param dateString1 开始日期 yyyy-MM-dd * @param dateString2 结束日期 yyyy-MM-dd * @returns {number} 如果日期相同 返回一天 开始日期大于结束日期,返回0 */ const getDaysBetween = (dateString1, dateString2) => { let startDate = Date.parse(dateString1); let endDate = Date.parse(dateString2); if (startDate > endDate) { return 0; } if (startDate == endDate) { return 1; } let days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000); return days.toFixed(0); } /** * 乘法函数,用来得到精确的乘法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accMul = (arg1, arg2) => { var m = 0; m += deal(arg1); m += deal(arg2); var r1 = Number(arg1.toString().replace('.', '')); var r2 = Number(arg2.toString().replace('.', '')); return (r1 * r2) / Math.pow(10, m); } /** * 加法函数,用来得到精确的加法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accAdd = (arg1, arg2) => { var r1 = deal(arg1); var r2 = deal(arg2); var m = Math.pow(10, Math.max(r1, r2)); return (arg1 * m + arg2 * m) / m; } const hasPermi = (permi) => { let list = uni.getStorageSync('permissions'); let has = list.filter(item => item == permi); return has.length > 0 ? true : false; } /** * 除法函数,用来得到精确的除法结果 * @param {Object} arg1 * @param {Object} arg2 */ const accDiv = (arg1, arg2) => { var t1 = deal(arg1); var t2 = deal(arg2); var r1 = Number(arg1.toString().replace(".", "")) var r2 = Number(arg2.toString().replace(".", "")) return (r1 / r2) * Math.pow(10, t2 - t1); } /** * 求小数点后的数据长度 */ const deal = (arg) => { var t = 0; try { t = arg.toString().split('.')[1].length; } catch (e) {} return t; } /** * 判断 有效日期不能小于今天 */ const dateCompare = (beginDate, endDate) => { var beginArrayDate = beginDate.split('-'); var endArrayDate = endDate.split('-') if (parseInt(endArrayDate[0], 10) > parseInt(beginArrayDate[0], 10)) return true; if (parseInt(endArrayDate[0], 10) == parseInt(beginArrayDate[0], 10)) { if (parseInt(endArrayDate[1], 10) > parseInt(beginArrayDate[1], 10)) return true; else { if (parseInt(endArrayDate[1], 10) == parseInt(beginArrayDate[1], 10)) { if (parseInt(endArrayDate[2], 10) >= parseInt(beginArrayDate[2], 10)) return true; } } } return false; } /** * 获取日期 * day 获取日期 * time 获取时间 */ const getDate = (obj = 'day') => { const date = new Date(); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); let Hours = date.getHours(); let Minutes = date.getMinutes(); month = month > 9 ? month : '0' + month; day = day > 9 ? day : '0' + day; Hours = Hours > 9 ? Hours : '0' + Hours; Minutes = Minutes > 9 ? Minutes : '0' + Minutes; if (obj == 'day') { return `${year}-${month}-${day}`; } if (obj == 'lm') { month = date.getMonth(); month = month > 9 ? month : '0' + month; return `${year}-${month}-${day}`; } if (obj == 'time') { return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes; } } /** * 格式化时间 */ const format = (obj) => { const date = new Date(obj); let year = date.getFullYear(); let month = date.getMonth() + 1; let day = date.getDate(); let Hours = date.getHours(); let Minutes = date.getMinutes(); month = month > 9 ? month : '0' + month; day = day > 9 ? day : '0' + day; Hours = Hours > 9 ? Hours : '0' + Hours; Minutes = Minutes > 9 ? Minutes : '0' + Minutes; return `${year}-${month}-${day}` + ' ' + Hours + ':' + Minutes; } // 将时间戳转化为指定时间 // way:方式(1=年月日,2=年月日时分秒)默认1, 也可以指定格式:yyyy-MM-dd HH:mm:ss const forDate = (inputTime, way) => { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; var ms = date.getMilliseconds(); way = way || 1; // way == 1 年月日 if (way === 1) { return y + '-' + m + '-' + d; } // way == 1 年月日时分秒 if (way === 2) { return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second; } // way == 具体格式 标准格式: yyyy-MM-dd HH:mm:ss if (typeof way == 'string') { return way.replace("yyyy", y).replace("MM", m).replace("dd", d).replace("HH", h).replace("mm", minute) .replace("ss", second).replace("ms", ms); } return y + '-' + m + '-' + d; } const encrypt = (word) => { let key = CryptoJS.enc.Utf8.parse("abcdefgabcdefg12"); let srcs = CryptoJS.enc.Utf8.parse(word); let encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return encrypted.toString(); } module.exports = { formatTime: formatTime, formatLocation: formatLocation, accMul: accMul, accAdd: accAdd, accDiv: accDiv, dateCompare: dateCompare, getDate: getDate, getDaysBetween: getDaysBetween, getdiffdate: getdiffdate, selectDictLabel: selectDictLabel, hasPermi: hasPermi, forDate:forDate, encrypt:encrypt }