pnkx-admin-mongo/pnkx-uniapp/utils/filters.js
2024-01-13 13:29:20 +08:00

105 lines
3.5 KiB
JavaScript

/**
* 时间格式过滤器
* @param valueTime
* @returns {string}
*/
export function timeFilter(valueTime) {
if (valueTime) {
// let newData = Date.parse(new Date() + '')
// let diffTime = Math.abs(newData - valueTime)
let diffTime = Math.abs(new Date().getTime() - new Date(valueTime).getTime());
if (diffTime > 7 * 24 * 3600 * 1000) {
let date = new Date(valueTime);
// let y = date.getFullYear()
let m = date.getMonth() + 1;
m = m < 10 ? ('0' + m) : m;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let minute = date.getMinutes();
let second = date.getSeconds();
minute = minute < 10 ? ('1' + minute) : minute;
second = second < 10 ? ('0' + second) : second;
return m + '-' + d + ' ' + h + ':' + minute
} else if (diffTime < 7 * 24 * 3600 * 1000 && diffTime > 24 * 3600 * 1000) {
// //注释("一周之内");
// var time = newData - diffTime;
let dayNum = Math.floor(diffTime / (24 * 60 * 60 * 1000));
return dayNum + '天前'
} else if (diffTime < 24 * 3600 * 1000 && diffTime > 3600 * 1000) {
// //注释("一天之内");
// var time = newData - diffTime;
let dayNum = Math.floor(diffTime / (60 * 60 * 1000));
return dayNum + '小时前'
} else if (diffTime < 3600 * 1000 && diffTime > 60 * 1000) {
// //注释("一小时之内");
// var time = newData - diffTime;
let dayNum = Math.floor(diffTime / (60 * 1000));
return dayNum + '分钟前'
} else if (diffTime < 60 * 1000 && diffTime > 0) {
// //注释("一分钟之内");
// var time = newData - diffTime;
let dayNum = Math.floor(diffTime / (1000));
return dayNum + '秒前'
}
}
}
/**
* 钱数过滤器
* @param valueMoney
* @returns {string}
*/
export function moneyFilter(valueMoney) {
let result = '', counter = 0;
let negativeFlag = false;
if (valueMoney === undefined || valueMoney === null || valueMoney === '' || valueMoney === 'null') {
valueMoney = 0
}
if (typeof valueMoney === 'string') {
if (valueMoney.slice(0, 1) === '-') {
valueMoney = valueMoney.slice(1);
negativeFlag = true;
}
valueMoney = parseFloat(valueMoney).toFixed(2).toString();
}
if (typeof valueMoney === 'number') {
if (valueMoney < 0) {
valueMoney = valueMoney * (-1);
negativeFlag = true;
}
valueMoney = valueMoney.toFixed(2).toString();
}
let decimal = valueMoney.slice(valueMoney.indexOf('.'));
valueMoney = valueMoney.slice(0, valueMoney.indexOf('.'));
for (let i = valueMoney.length - 1; i >= 0; i--) {
counter++;
result = valueMoney.charAt(i) + result;
if (!(counter % 3) && i !== 0) {
result = ',' + result;
}
}
if (negativeFlag) {
result = '-' + result;
}
return result + decimal;
}
/**
* 文字超出样式过滤器
* @param value
* @returns {string|*}
*/
export function ellipsisFilter(value) {
if (!value) {
return
}
let len = value.length;
if (!value) return ''
if (value.length > 8) {
return value.substring(0, 4) + '···' + value.substring(len - 2, len)
}
return value
}