Commit ceda797c by liimin

refactor(report-manage, alarm-center): 重构里程详情页日历逻辑并优化告警抽屉配置

重构里程统计详情页的日历数据加载、缓存与渲染流程,提取通用日期处理工具方法减少代码重复;为月度里程数据添加缓存与请求去重,降低接口调用开销;优化日期单元格样式与渲染逻辑,修复样式残留问题;同时新增告警中心详情抽屉的遮罩点击关闭配置,避免误触弹窗关闭。
1 parent c8cfdd2a
......@@ -4,6 +4,7 @@
:visible.sync="visible"
direction="rtl"
size="70%"
:wrapper-closable="false"
:before-close="handleClose"
@closed="handleClosed"
>
......
......@@ -260,14 +260,7 @@ export default {
const monthList = Array.from(
popper.querySelectorAll(".el-date-range-picker__header div"),
)
.map((header) => {
const headerText = (header.textContent || "").trim();
const yearMatch = headerText.match(/(\d{4})/);
const monthMatch = headerText.match(/(\d{1,2})\s*月/);
if (!yearMatch || !monthMatch) return "";
return `${yearMatch[1]}-${monthMatch[1].padStart(2, "0")}`;
})
.map((header) => this.parseHeaderYearMonth(header).monthKey)
.filter(Boolean)
.sort();
......@@ -282,94 +275,119 @@ export default {
};
},
// 按当前可见月份拉取里程日历数据
syncVisibleCalendarData() {
if (!this.searchForm.sn) return;
// 设备切换时清空月份缓存和当前面板上的里程标记。
resetDailyMileageCache() {
this._dailyMileageMonthCache = {};
this._dailyMileageMonthInFlight = {};
this.customDateData = {};
},
// 仅返回当前两个面板真正需要处理的月份,并过滤掉未来月份。
getVisibleCalendarMonths() {
const visibleMonthRange = this.getVisibleMonthRange();
if (!visibleMonthRange) return;
if (!visibleMonthRange) return [];
const { startMonth, endMonth } = visibleMonthRange;
const currentMonthKey = this.getCurrentMonthKey();
return (
startMonth === endMonth ? [startMonth] : [startMonth, endMonth]
).filter((month) => month && month <= currentMonthKey);
},
// 把接口返回的月度列表转换成日期索引,便于后续按日期直接渲染。
buildDailyMileageMonthMap(list) {
const calendarMap = {};
(Array.isArray(list) ? list : []).forEach((item) => {
if (!item || !item.date) return;
const mileageValue =
item.totalMileage === 0 || item.totalMileage
? item.totalMileage
: "--";
calendarMap[item.date] = {
redDot: true,
label: `${mileageValue}km`,
};
});
return calendarMap;
},
// 单个月份只发一次请求;已缓存直接跳过,请求中则复用同一个 Promise。
ensureDailyMileageMonthLoaded(month) {
if (this._dailyMileageMonthCache[month]) return null;
if (this._dailyMileageMonthInFlight[month]) {
return this._dailyMileageMonthInFlight[month];
}
const requestParams = {
sn: this.searchForm.sn,
mileageStartTime: month,
mileageEndTime: month,
};
const request = this.getDailyMileageCalendar(requestParams)
.then((res) => {
this._dailyMileageMonthCache[month] = this.buildDailyMileageMonthMap(
res.data,
);
})
.finally(() => {
delete this._dailyMileageMonthInFlight[month];
});
this._dailyMileageMonthInFlight[month] = request;
return request;
},
// 把当前可见月份的缓存数据合并成面板实际使用的 customDateData。
updateCustomDateDataByMonths(months) {
const calendarMap = {};
months.forEach((month) => {
const monthMap = this._dailyMileageMonthCache[month];
if (!monthMap) return;
Object.assign(calendarMap, monthMap);
});
this.customDateData = calendarMap;
},
// 注入依赖日期面板 DOM,统一放到 nextTick 里做一次刷新。
refreshCalendarDateCellContent() {
this.$nextTick(() => {
this.injectDateCellContent();
});
},
// 按当前可见月份拉取里程日历数据
syncVisibleCalendarData() {
if (!this.searchForm.sn) return;
if (this._dailyMileageCacheSn !== this.searchForm.sn) {
this._dailyMileageCacheSn = this.searchForm.sn;
this._dailyMileageMonthCache = {};
this._dailyMileageMonthInFlight = {};
this.customDateData = {};
this.resetDailyMileageCache();
}
const months = (
startMonth === endMonth ? [startMonth] : [startMonth, endMonth]
).filter((month) => month && month <= currentMonthKey);
const months = this.getVisibleCalendarMonths();
if (!months.length) {
this.customDateData = {};
this.$nextTick(() => {
this.injectDateCellContent();
});
this.refreshCalendarDateCellContent();
return;
}
const pending = [];
months.forEach((month) => {
if (this._dailyMileageMonthCache[month]) return;
if (this._dailyMileageMonthInFlight[month]) {
pending.push(this._dailyMileageMonthInFlight[month]);
return;
}
const requestParams = {
sn: this.searchForm.sn,
mileageStartTime: month,
mileageEndTime: month,
};
const p = this.getDailyMileageCalendar(requestParams)
.then((res) => {
const calendarMap = {};
const list = Array.isArray(res.data) ? res.data : [];
list.forEach((item) => {
if (!item || !item.date) return;
const mileageValue =
item.totalMileage === 0 || item.totalMileage
? item.totalMileage
: "--";
calendarMap[item.date] = {
redDot: true,
label: `${mileageValue}km`,
};
});
this._dailyMileageMonthCache[month] = calendarMap;
})
.finally(() => {
delete this._dailyMileageMonthInFlight[month];
});
this._dailyMileageMonthInFlight[month] = p;
pending.push(p);
});
const updateView = () => {
const calendarMap = {};
months.forEach((month) => {
const monthMap = this._dailyMileageMonthCache[month];
if (!monthMap) return;
Object.assign(calendarMap, monthMap);
});
this.customDateData = calendarMap;
};
const pending = months
.map((month) => this.ensureDailyMileageMonthLoaded(month))
.filter(Boolean);
if (!pending.length) {
updateView();
this.updateCustomDateDataByMonths(months);
return;
}
Promise.all(pending)
.then(() => {
updateView();
this.updateCustomDateDataByMonths(months);
})
.finally(() => {
this.$nextTick(() => {
this.injectDateCellContent();
});
this.refreshCalendarDateCellContent();
});
},
......@@ -378,6 +396,14 @@ export default {
return GET_JOURNEYS_DAILYMILEAGE(params);
},
// 统一生成 yyyy-MM-dd,避免不同地方手写日期格式。
formatDateKey(year, month, day) {
return `${this.formatMonthKey(year, month)}-${String(day).padStart(
2,
"0",
)}`;
},
// 根据单元格所属月份计算真实日期,避免 prev-month / next-month 格子误判
getDateKeyByCell(year, month, day, cell) {
let realYear = Number(year);
......@@ -397,17 +423,47 @@ export default {
}
}
return `${realYear}-${String(realMonth).padStart(2, "0")}-${String(
day,
).padStart(2, "0")}`;
return this.formatDateKey(realYear, realMonth, day);
},
// 统一生成 yyyy-MM,给缓存 key、月份比较和 header 解析复用。
formatMonthKey(year, month) {
return `${year}-${String(month).padStart(2, "0")}`;
},
getCurrentMonthKey() {
const now = new Date();
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(
2,
"0",
)}`;
return this.formatMonthKey(now.getFullYear(), now.getMonth() + 1);
},
// 从日期面板头部提取年、月和 monthKey,避免多处重复解析同一段文案。
parseHeaderYearMonth(headerEl) {
if (!headerEl) {
return {
year: "",
month: "",
monthKey: "",
};
}
const headerText = (headerEl.textContent || "").trim();
const yearMatch = headerText.match(/(\d{4})/);
const monthMatch = headerText.match(/(\d{1,2})\s*月/);
if (!yearMatch || !monthMatch) {
return {
year: "",
month: "",
monthKey: "",
};
}
const year = yearMatch[1];
const month = String(monthMatch[1]).padStart(2, "0");
return {
year,
month,
monthKey: this.formatMonthKey(year, month),
};
},
getHeaderMonthByTarget(target) {
......@@ -415,14 +471,7 @@ export default {
if (!header) return "";
const headerTitle = header.querySelector("div");
if (!headerTitle) return "";
const headerText = (headerTitle.textContent || "").trim();
const yearMatch = headerText.match(/(\d{4})/);
const monthMatch = headerText.match(/(\d{1,2})\s*月/);
if (!yearMatch || !monthMatch) return "";
return `${yearMatch[1]}-${monthMatch[1].padStart(2, "0")}`;
return this.parseHeaderYearMonth(headerTitle).monthKey;
},
// 当前月是最大月份,不能继续往后翻
......@@ -496,70 +545,70 @@ export default {
},
// 给日期单元格打标记 class 和属性,再由 CSS 渲染红点和文案
// 先清掉上一次注入的节点和 class,避免重复注入后样式残留。
resetDateCellContent(popper) {
popper
.querySelectorAll(
".mileage-date-inner-custom, .date-cell-day, .has-date-cell-dot, .has-date-cell-label",
)
.forEach((el) => {
el.classList.remove(
"mileage-date-inner-custom",
"date-cell-day",
"has-date-cell-dot",
"has-date-cell-label",
);
if (el.hasAttribute("data-mileage-label")) {
el.removeAttribute("data-mileage-label");
}
});
},
// 只处理单个日期格的渲染,让主循环保持简单。
renderDateCell(cell, year, month) {
const spanEl = cell.querySelector("div > span");
if (!spanEl) return;
const cellInner = cell.querySelector("div");
if (!cellInner) return;
const day = spanEl.textContent.trim();
const dateKey = this.getDateKeyByCell(year, month, day, cell);
const data = this.customDateData[dateKey];
if (!data) return;
cellInner.classList.add("mileage-date-inner-custom");
spanEl.classList.add("date-cell-day");
if (data.redDot) {
cellInner.classList.add("has-date-cell-dot");
}
if (data.label) {
cellInner.classList.add("has-date-cell-label");
cellInner.setAttribute("data-mileage-label", data.label);
}
},
// 按当前面板遍历所有日期格,并把缓存里的里程标记重新注入到 DOM 中。
injectDateCellContent() {
const popper = document.querySelector(".mileage-datepicker-popup");
if (!popper) return;
this._isInjectingDateCells = true;
// 清除之前的注入内容
popper
.querySelectorAll(".date-cell-dot, .date-cell-label")
.forEach((el) => {
el.remove();
});
popper
.querySelectorAll(".mileage-date-inner-custom")
.forEach((cellInner) => {
cellInner.classList.remove("mileage-date-inner-custom");
});
popper.querySelectorAll(".date-cell-day").forEach((el) => {
el.classList.remove("date-cell-day");
});
this.resetDateCellContent(popper);
const panels = popper.querySelectorAll(".el-date-range-picker__content");
let matchedCount = 0;
panels.forEach((panel) => {
const headerEl = panel.querySelector(
".el-date-range-picker__header div",
);
if (!headerEl) return;
const headerText = headerEl.textContent;
const yearMatch = headerText.match(/(\d{4})/);
const monthMatch = headerText.match(/(\d{1,2})\s*月/);
if (!yearMatch || !monthMatch) return;
const year = yearMatch[1];
const month = monthMatch[1].padStart(2, "0");
const { year, month } = this.parseHeaderYearMonth(headerEl);
if (!year || !month) return;
// Element UI 2.x 单元格结构:<td><div><span>日期</span></div></td>
const cells = panel.querySelectorAll("td.available");
cells.forEach((cell) => {
const spanEl = cell.querySelector("div > span");
if (!spanEl) return;
const cellInner = cell.querySelector("div");
if (!cellInner) return;
const day = spanEl.textContent.trim();
const dateKey = this.getDateKeyByCell(year, month, day, cell);
const data = this.customDateData[dateKey];
if (!data) return;
matchedCount += 1;
cellInner.classList.add("mileage-date-inner-custom");
spanEl.classList.add("date-cell-day");
if (data.redDot) {
const dot = document.createElement("span");
dot.className = "date-cell-dot";
cellInner.insertBefore(dot, spanEl);
}
if (data.label) {
const label = document.createElement("span");
label.className = "date-cell-label";
label.textContent = data.label;
cellInner.appendChild(label);
}
this.renderDateCell(cell, year, month);
});
});
setTimeout(() => {
......@@ -670,21 +719,26 @@ export default {
<style lang="scss">
/* 日期选择器自定义内容样式 */
.mileage-datepicker-popup .el-date-table td {
padding: 4px 0;
}
.mileage-datepicker-popup .el-date-table td div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 38px;
min-height: 40px;
box-sizing: border-box;
text-align: center;
padding-top: 2px;
padding-bottom: 2px;
padding-top: 1px;
padding-bottom: 1px;
overflow: visible;
}
.mileage-datepicker-popup .mileage-date-inner-custom {
position: relative;
text-align: center;
}
......@@ -697,32 +751,75 @@ export default {
transform: none !important;
}
.mileage-datepicker-popup .date-cell-day {
display: block;
width: auto !important;
height: auto !important;
line-height: 1.2 !important;
margin: 0 !important;
}
.mileage-datepicker-popup .date-cell-dot {
.mileage-datepicker-popup .mileage-date-inner-custom.has-date-cell-dot::before {
content: "";
display: block;
width: 6px;
height: 6px;
border-radius: 50%;
background: #f56c6c;
margin: 0 0 2px 0 !important;
margin: 0 0 0px 0;
flex: 0 0 auto;
}
.mileage-datepicker-popup .date-cell-label {
.mileage-datepicker-popup .mileage-date-inner-custom.has-date-cell-label::after {
content: attr(data-mileage-label);
display: block;
width: auto !important;
height: auto !important;
width: auto;
height: auto;
font-size: 10px;
line-height: 1 !important;
line-height: 1;
white-space: nowrap;
margin: 2px 0 0 0 !important;
margin: -4px 0 0 0;
flex: 0 0 auto;
color: #606266;
background: transparent;
}
.mileage-datepicker-popup .date-cell-day {
display: inline-flex;
align-items: center;
justify-content: center;
width: 24px !important;
min-width: 24px !important;
max-width: 24px !important;
height: 24px !important;
min-height: 24px !important;
max-height: 24px !important;
padding: 0 !important;
line-height: 1.2 !important;
margin: 0 !important;
border-radius: 50%;
box-sizing: border-box;
flex: 0 0 24px;
}
/* 选中态只高亮日期数字,不影响自定义红点和里程文案 */
.mileage-datepicker-popup .el-date-table td.start-date .date-cell-day,
.mileage-datepicker-popup .el-date-table td.end-date .date-cell-day {
background-color: #409eff !important;
color: #fff !important;
}
.mileage-datepicker-popup
.el-date-table
td.start-date
.mileage-date-inner-custom.has-date-cell-dot::before,
.mileage-datepicker-popup
.el-date-table
td.end-date
.mileage-date-inner-custom.has-date-cell-dot::before {
background: #f56c6c !important;
}
.mileage-datepicker-popup .el-date-table td.start-date .mileage-date-inner-custom.has-date-cell-label::after,
.mileage-datepicker-popup .el-date-table td.end-date .mileage-date-inner-custom.has-date-cell-label::after,
.mileage-datepicker-popup .el-date-table td.in-range .mileage-date-inner-custom.has-date-cell-label::after {
background: transparent !important;
color: #606266 !important;
}
.el-date-table td.start-date div{
margin-left: 0 !important;
}
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!