/** * 格式化数字,数字每隔三位加个逗号 * params: num 需要处理的数字 * params: len 需要保留的小数点位数 */ export function formattedNumber(num, len = 2) { let result = "", counter = 0; let formatNum; if (num < 0) { formatNum = formatNub(Math.abs(num) || 0, len); } else { formatNum = formatNub(num || 0, len); } let stringNum = formatNum.toString().split("."); for (let i = stringNum[0].length - 1; i >= 0; i--) { counter++; result = stringNum[0].charAt(i) + result; if (!(counter % 3) && i != 0) { result = "," + result; } } result = num < 0 ? "-" + result : result; if (stringNum[1]) { return "¥" + result + "." + stringNum[1]; } else { return "¥" + result; } }; /** * 不四舍五入保留n位小数 * params: val 需要处理的数字 * params: len 需要保留的小数点位数 */ export function formatNub(num, n = 2) { if (typeof num != "number" && !Number(num)) { return "0.00"; } num = Number(num).toString(); let result = ""; let zeroResult = function(n) { let zero = ""; for (let i = 0; i < n; i++) { zero += "0"; } return zero; }; if (num % 1 == 0) { //整数 result = num + "." + zeroResult(n); } else { //小数 let num1 = num.split("."); if (num1[1].length < n) { result = num1[0] + "." + num1[1] + zeroResult(n - num1[1].length); } else { result = num1[0] + "." + num1[1].substring(0, n); } } return result; }; // 获取【加载更多】组件状态 export function loadStatus(page, pageSize, total) { if (total / pageSize > page) { return 'loadmore'; } else { return 'nomore'; } } /** * 获取zhi日期前后N天的日期 * n 获取自定天 负数指定前n天,正数,指定后n天 * */ export function funDate(n) { let date1 = new Date(); let time1 = date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate(); // time1表示当前时间 let date2 = new Date(date1); date2.setDate(date1.getDate() + n); let time2 = date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate(); return time2; } // 指定月最后一天 export function getCurrentMonthLast(time) { // console.log(time) let date = '' if (!time) { date = new Date(); } else { date = new Date(time + '-01'); } let currentMonth = date.getMonth(); let nextMonth = ++currentMonth; let nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1); let oneDay = 1000 * 60 * 60 * 24; let lastTime = new Date(nextMonthFirstDay - oneDay); let month = parseInt(lastTime.getMonth() + 1); let day = lastTime.getDate(); if (month < 10) { month = '0' + month } if (day < 10) { day = '0' + day } return date.getFullYear() + '-' + month + '-' + day; } /** * 时间转换为10位时间戳 * */ export function timeByTimestamp(time) { let date1 = new Date(time).getTime() / 1000; return date1; } /** * 本月第一天 * */ export function showMonthFirstDay() { var Nowdate = new Date(); var MonthFirstDay = new Date(Nowdate.getFullYear(), Nowdate.getMonth(), 1); const M = Number(MonthFirstDay.getMonth()) + 1; return ( MonthFirstDay.getFullYear() + "-" + M + "-" + MonthFirstDay.getDate() + " 00:00:00" ); } /** * 判断当前页面,是使用搜索引擎接口还是使用列表接口 */ export const isSerch = (obj) => { let isKey = false; for (let i in obj) { let item = obj[i]; if (Array.isArray(item)) { if (item.length > 0) { isKey = true; break; } } else if ( typeof item === "string" || typeof item === "number" || typeof item === "boolean" ) { if (item) { isKey = true; break; } } else if (typeof item === "object") { if (item && JSON.stringify(item) !== "{}") { isKey = true; break; } } } return isKey; }; /** * 数组去重 * 按数组对象的某一个属性或两个或三个属性去重 * params: arr 数组 * params: property 对象属性 */ export const unique = (arr = [], property = []) => { for (let i = 0, len = arr.length; i < len; i++) { for (let j = i + 1; j < len; j++) { if (property.length === 1) { if (arr[i][property[0]] === arr[j][property[0]]) { arr.splice(j, 1); // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一 len--; j--; } } else if (property.length === 2) { if ( arr[i][property[0]] === arr[j][property[0]] && arr[i][property[1]] === arr[j][property[1]] ) { arr.splice(j, 1); // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一 len--; j--; } } else if (property.length === 3) { if ( arr[i][property[0]] === arr[j][property[0]] && arr[i][property[1]] === arr[j][property[1]] && arr[i][property[2]] === arr[j][property[2]] ) { arr.splice(j, 1); // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一 len--; j--; } } else { if (arr[i] === arr[j]) { arr.splice(j, 1); // splice 会改变数组长度,所以要将数组长度 len 和下标 j 减一 len--; j--; } } } } return arr; }; /** * @description 时间格式化 时间戳转换为时间 * @param date * @param fmt * @return {*} */ export function formatDate(date, fmt) { if (!date) { return '' } // if (typeof date !== 'string') { // return '' // } date *= (date.toString().length === 10 ? 1000 : 1) let _date = new Date(date) let _fmt = fmt || 'yyyy-MM-dd hh:mm:ss' let o = { 'M+': _date.getMonth() + 1, 'd+': _date.getDate(), 'h+': _date.getHours(), 'm+': _date.getMinutes(), 's+': _date.getSeconds() } if (/(y+)/.test(_fmt)) { _fmt = _fmt.replace(RegExp.$1, (_date.getFullYear() + '').substr(4 - RegExp.$1.length)) } for (let 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 }