/** * 获取数据类型 * @param {All} [o] 需要检测的数据 * @returns {String} */ export function getType(o) { return Object.prototype.toString.call(o).slice(8, -1); } /** * 判断是否是指定数据类型 * @param {All} [o] 需要检测的数据 * @param {String} [type] 数据类型 * @returns {Boolean} */ export function isKeyType(o, type) { return getType(o).toLowerCase() === type.toLowerCase(); } export function deepCopy(sth) { let copy; if (null == sth || "object" != typeof sth) return sth; if (isKeyType(sth, 'date')) { copy = new Date(); copy.setTime(sth.getTime()); return copy; } if (isKeyType(sth, 'array')) { copy = []; for (let i = 0, len = sth.length; i < len; i++) { copy[i] = deepCopy(sth[i]); } return copy; } if (isKeyType(sth, 'object')) { copy = {}; for (let attr in sth) { if (sth.hasOwnProperty(attr)) copy[attr] = deepCopy(sth[attr]); } return copy; } return null; } /** * 关键信息隐藏 * @param str 字符串 * @param frontLen 字符串前面保留位数 * @param endLen 字符串后面保留位数 * @returns {string} */ export function hideCode(str, frontLen, endLen = 0) { var len = str.length - frontLen - endLen; var xing = ''; for (var i = 0; i < len; i++) { xing += '*'; } return str.substring(0, frontLen) + xing + str.substring(str.length - endLen); }; // 数组去重 export function unique(arr) { var obj = {}; return arr.filter(function (item, index, arr) { return obj.hasOwnProperty(typeof item + item) ? false : (obj[typeof item + item] = true) }) } // 创造id export function getUuid(len, radix) { var chars = "0123456789abcdefghijklmnopqrstuvwxyz".split( "" ); var uuid = [], i; radix = radix || chars.length; if (len) { for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]; } else { var r; uuid[8] = uuid[13] = uuid[18] = uuid[23] = "-"; uuid[14] = "4"; for (i = 0; i < 36; i++) { if (!uuid[i]) { r = 0 | (Math.random() * 16); uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r]; } } } return uuid.join(""); } //js计算两个时间戳之间的时间差 (月) export function intervalTime(startTime, endTime) { // var timestamp=new Date().getTime(); //计算当前时间戳 var timestamp = (Date.parse(new Date())) / 1000;//计算当前时间戳 (毫秒级) var date1 = ""; //开始时间 if (timestamp < startTime) { date1 = startTime; } else { date1 = timestamp; //开始时间 } var date2 = endTime; //结束时间 // var date3 = date2.getTime() - date1.getTime(); //时间差的毫秒数 var date3 = (date2 - date1) * 1000; //时间差的毫秒数 //计算出相差天数 var mon = Math.floor(date3 / (30 * 24 * 3600 * 1000 * 1000)); // return days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒" return mon } // 日期转时间戳 export function js_strto_time(str_time) { var str = str_time.replace(/-/g, '/') // 将-替换成/,因为下面这个构造函数只支持/分隔的日期字符串 var date = new Date(str) // 构造一个日期型数据,值为传入的字符串 return date.getTime() } // 时间戳转日期 export function timestampToTime(timestamp) { var date = new Date(timestamp)//时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-' var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-' var D = date.getDate() > 10 ? date.getDate() : '0' + date.getDate() return Y + M + D }