export const downloadFiles = function (filepath, filename) {
let a = document.createElement("a");
a.download = filename;
a.href = filepath;
a.target = "_blank";
a.click();
a.remove();
}
字典回显
export const selectDictLabel = function(data, value) {
if (value === undefined || value === null || value === "") {
return "";
}
let actions = [];
Object.keys(data).some((key) => {
if (key == value) {
actions.push(data[value]);
return true;
}
});
if (actions.length === 0) {
actions.push(value);
}
return actions.join("");
}
字典回显(字符串数组)
export const selectDictLabels = function(datas, value, separator) {
if (value === undefined) {
return "";
}
var actions = [];
var currentSeparator = undefined === separator ? "," : separator;
var temp = value.split(currentSeparator);
Object.keys(value.split(currentSeparator)).some((val) => {
var match = false;
Object.keys(datas).some((key) => {
if (datas[key].value == ('' + temp[val])) {
actions.push(datas[key].label + currentSeparator);
match = true;
}
})
if (!match) {
actions.push(temp[val] + currentSeparator);
}
})
return actions.join('').substring(0, actions.join('').length - 1);
}
时间戳转年月日时分秒
export const timestampToTime = function timestampToTime(time, type) {
var date = new Date(time * 1000);
var Y = date.getFullYear() + "-";
var M =
(date.getMonth() + 1 < 10
? "0" + (date.getMonth() + 1)
: date.getMonth() + 1) + "-";
var D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
var h =
(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var s =
":" +
(date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds());
if (type == "y-m-d h-m-s") {
return Y + M + D + h + m + s;
} else if (type == "m-d") {
return M + "月" + D + "日";
} else if (type == "y-m-d h-m") {
return Y + M + D + h + m;
}
};
昨天、前天、今天显示几点、前天之前显示yy/mm/dd
export const timeago = function timeago(time) {
if (typeof time === "number") {
time = timestampToTime(time, "y-m-d h-m-s");
time = time.substring(0, time.length - 3);
} else {
time = time.toString().substring(0, time.length - 3);
}
let str = time.split(" ")[0];
let ptime = new Date(time).getTime();
const twentyFourHours = 24 * 60 * 60 * 1000;
const fortyEightHours = 24 * 60 * 60 * 1000 * 2;
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const today = `${year}-${month}-${day}`;
const todayTime = new Date(today).getTime();
const yesterdayTime = new Date(todayTime - twentyFourHours).getTime();
const lastYesterdayTime = new Date(todayTime - fortyEightHours).getTime();
if (year == new Date(time).getFullYear()) {
if (ptime >= todayTime) {
return time.split(" ")[1];
} else if (ptime < todayTime && yesterdayTime <= ptime) {
return "昨天 " + time.split(" ")[1];
} else if (ptime < yesterdayTime && lastYesterdayTime <= ptime) {
return "前天 " + time.split(" ")[1];
} else {
return (
str.split("-")[1] + "-" + str.split("-")[2] + " " + time.split(" ")[1]
);
}
} else {
return time;
}
};
刚刚、分钟前、小时前、天前、周前、半个月前、月前
export const timeago = function timeago(datetime) {
var dateTimeStamp = new Date(datetime).getTime();
var minute = 1000 * 60;
var hour = minute * 60;
var day = hour * 24;
var week = day * 7;
var halfamonth = day * 15;
var month = day * 30;
var now = new Date().getTime();
var diffValue = Math.abs(now - dateTimeStamp); //时间差
var minC = Math.floor(diffValue / minute); //计算时间差的分时天周月
var hourC = Math.floor(diffValue / hour);
var dayC = Math.floor(diffValue / day);
var halfamonthC = Math.floor(diffValue / halfamonth);
var weekC = Math.floor(diffValue / week);
var monthC = Math.floor(diffValue / month);
if (monthC > 0) {
return monthC + "月前";
}
if (halfamonthC > 0) {
return "半个月前";
}
if (weekC > 0) {
return weekC + "周前";
}
if (dayC > 0) {
return dayC + "天前";
}
if (hourC > 0) {
return hourC + "小时前";
}
if (minC > 0) {
return minC + "分钟前";
}
return "刚刚";
};