recycapp/common/util.js
2025-10-26 16:36:59 +08:00

70 lines
2.0 KiB
JavaScript

const util = {
delRepeat(arr,key){ //去重
let resArr = []
arr.some(function(item,index){
console.log(item);
let isRepeat = false;
resArr.some(function(item1,index1){
if(item.tn == item1.tn){
isRepeat = true;
}
});
if(!isRepeat){
resArr.push(item);
}
})
return resArr;
},
Format(date,fmt){
date = new Date(date);
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var 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;
},
formatDate(timestamp, format = 'YYYY-MM-DD HH:mm:ss') {
const date = new Date(timestamp);
const map = {
'YYYY': date.getFullYear(),
'MM': String(date.getMonth() + 1).padStart(2, '0'),
'DD': String(date.getDate()).padStart(2, '0'),
'HH': String(date.getHours()).padStart(2, '0'),
'mm': String(date.getMinutes()).padStart(2, '0'),
'ss': String(date.getSeconds()).padStart(2, '0')
};
return format.replace(/YYYY|MM|DD|HH|mm|ss/g, match => map[match]);
},
showMsg(msg,icon,duration = 1600) {
uni.showToast({
title: msg,
duration: duration,
icon: icon||'none'
});
},
getDate(time){
let timeStr = this.Format(time,'yyyy/MM/dd hh:mm');
let timeParse = Date.parse(new Date(timeStr))
let timer = {};
timer.timeStr = timeStr;
timer.timeParse = timeParse;
return timer
},
backPage(time){
setTimeout(function(){
uni.navigateBack({
delta:1
})
},time||3000)
}
}
export default util