|
|
// utils/RequestParamUtil.js
|
|
|
class RequestParamUtil {
|
|
|
/**
|
|
|
* 判断日期是前天、昨天还是今天
|
|
|
* @param {string} cachedDate - 缓存的日期字符串,格式为 yyyy-MM-dd-HH-mm
|
|
|
* @returns {number} 前天返回2,昨天返回1,今天返回0,其他情况返回-1
|
|
|
*/
|
|
|
static judgeRecentDate(cachedDate) {
|
|
|
// 验证日期格式
|
|
|
if (!/^\d{4}-\d{2}-\d{2}-\d{2}-\d{2}$/.test(cachedDate)) {
|
|
|
console.error('日期格式不正确,应为 yyyy-MM-dd-HH-mm');
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// 提取日期部分(忽略时间)
|
|
|
const dateStr = cachedDate.split('-').slice(0, 3).join('-');
|
|
|
|
|
|
// 转换为Date对象
|
|
|
const cachedDateObj = new Date(dateStr);
|
|
|
if (isNaN(cachedDateObj.getTime())) {
|
|
|
console.error('无效的日期');
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
// 获取今天、昨天、前天的日期
|
|
|
const today = new Date();
|
|
|
today.setHours(0, 0, 0, 0);
|
|
|
|
|
|
const yesterday = new Date(today);
|
|
|
yesterday.setDate(yesterday.getDate() - 1);
|
|
|
|
|
|
const dayBeforeYesterday = new Date(today);
|
|
|
dayBeforeYesterday.setDate(dayBeforeYesterday.getDate() - 2);
|
|
|
|
|
|
// 比较日期
|
|
|
if (this.isSameDay(cachedDateObj, today)) {
|
|
|
return 0; // 今天
|
|
|
} else if (this.isSameDay(cachedDateObj, yesterday)) {
|
|
|
return 1; // 昨天
|
|
|
} else if (this.isSameDay(cachedDateObj, dayBeforeYesterday)) {
|
|
|
return 2; // 前天
|
|
|
} else {
|
|
|
return -1; // 更早的日期
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断两个Date对象是否是同一天
|
|
|
* @param {Date} date1
|
|
|
* @param {Date} date2
|
|
|
* @returns {boolean}
|
|
|
*/
|
|
|
static isSameDay(date1, date2) {
|
|
|
return date1.getFullYear() === date2.getFullYear() &&
|
|
|
date1.getMonth() === date2.getMonth() &&
|
|
|
date1.getDate() === date2.getDate();
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
export default RequestParamUtil; |