Commit 23400f2f by liimin

feat(报表管理): 为里程通用明细页面添加每日里程日历展示功能

新增报表服务模拟接口GET_JOURNEYS_DAILYMILEAGE生成里程统计数据
优化日期选择器,支持日期范围校验、禁用未来日期选择以及自定义单元格渲染
添加SCSS样式实现日历单元格的红点标记和里程标签展示
绑定日历头部翻页事件,防止跳转至当前月份之后的页面
1 parent 4069b271
This diff could not be displayed because it is too large.
......@@ -109,6 +109,42 @@ export const GET_JOURNEYS_DAILYEXPORT = params => {
return request(params, '/trialdrive/journeys/dailyExport')
}
// 报表管理-里程统计(通用)-里程日历
export const GET_JOURNEYS_DAILYMILEAGE = params => {
const list = []
const fixedDays = [1, 5, 9, 12, 15, 19, 24, 28]
let cursor = new Date(`${params.mileageStartTime}-01T00:00:00`)
const endCursor = new Date(`${params.mileageEndTime}-01T00:00:00`)
const seed = String(params.sn || '')
.split('')
.reduce((sum, char) => sum + char.charCodeAt(0), 0)
while (cursor <= endCursor) {
const year = cursor.getFullYear()
const month = cursor.getMonth() + 1
const maxDay = new Date(year, month, 0).getDate()
fixedDays.forEach((day, index) => {
if (day > maxDay) return
const mileage = ((seed + month * 17 + day * 13 + index * 7) % 180) + 1
list.push({
date: `${year}-${String(month).padStart(2, '0')}-${String(day).padStart(2, '0')}`,
totalMileage: mileage
})
})
cursor = new Date(year, month, 1)
}
return new Promise(resolve => {
setTimeout(() => {
resolve({ data: list })
}, 1240)
})
// return request(params, '/trialdrive/journeys/dailyMileage')
}
// 报表管理-假性离线报警-获取列表
export const GET_FAKEOFFLINE_LIST = params => {
return request(params, '/trialdrive/fakeoffline/search')
......
......@@ -8,7 +8,7 @@
<div class="search_box">
<el-form :model="searchForm" :inline="true" size="medium">
<el-form-item label="日期:">
<el-date-picker v-model="dateTime" type="daterange" unlink-panels range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" value-format="yyyy-MM-dd">
<el-date-picker ref="datePicker" v-model="dateTime" type="daterange" unlink-panels range-separator="~" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions" :default-value="datePickerDefaultValue" value-format="yyyy-MM-dd" popper-class="mileage-datepicker-popup" :editable="false" @focus="handleDatePickerTrigger" @change="handleDateRangeChange">
</el-date-picker>
</el-form-item>
<el-form-item label="设备号:">
......@@ -62,6 +62,7 @@ import {
GET_JOURNEYS_SEARCHDETAIL,
MILEAGE_GETSN,
GET_JOURNEYS_EXPORTDETAIL,
GET_JOURNEYS_DAILYMILEAGE,
} from "services/report";
import { deepClone, showToast } from "@/utils/util";
import { mileageDetailShortcuts } from "@/common/index";
......@@ -126,7 +127,26 @@ export default {
pageTotal: 0,
pickerOptions: {
shortcuts: mileageDetailShortcuts,
disabledDate(time) {
const now = new Date();
const endOfCurrentMonth = new Date(
now.getFullYear(),
now.getMonth() + 1,
0,
23,
59,
59,
999
);
return time.getTime() > endOfCurrentMonth.getTime();
},
},
// 自定义日期标注:redDot 显示红点,label 显示在日期下方
customDateData: {},
_datePickerHeaderHandlers: [],
_datePickerRenderTimer: null,
_isInjectingDateCells: false,
_dailyMileageRangeKey: "",
dloading: false,
aloding: false,
};
......@@ -134,14 +154,308 @@ export default {
components: {
GlsxTable,
},
computed: {
datePickerDefaultValue() {
const now = new Date();
return [
new Date(now.getFullYear(), now.getMonth() - 1, 1),
new Date(now.getFullYear(), now.getMonth(), 1),
];
},
},
created() {
//this.getSn();
},
mounted() {
this.getList();
//this.getTableList();
this.bindDatePickerHeaderEvents();
},
beforeDestroy() {
this.unbindDatePickerHeaderEvents();
},
methods: {
// 日期输入框 focus 后只负责触发渲染
handleDatePickerTrigger() {
this.$nextTick(() => {
this.scheduleCalendarRefresh();
});
},
// 兜底保证开始日期不大于结束日期
handleDateRangeChange(value) {
if (!Array.isArray(value) || value.length !== 2) return;
if (!value[0] || !value[1]) return;
if (value[0] <= value[1]) return;
this.dateTime = [value[1], value[0]];
},
// 多次延迟刷新,兼容弹层异步挂载和翻月后的 DOM 更新时间差
scheduleCalendarRefresh() {
[0, 80, 180].forEach((delay) => {
setTimeout(() => {
this.syncVisibleCalendarData();
this.injectDateCellContent();
}, delay);
});
},
// 解析当前面板可见的月份范围
getVisibleMonthRange() {
const popper = document.querySelector(".mileage-datepicker-popup");
if (!popper) return null;
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")}`;
})
.filter(Boolean)
.sort();
if (!monthList.length) return null;
return {
startMonth: monthList[0],
endMonth: monthList[monthList.length - 1],
rangeKey: `${this.searchForm.sn}_${monthList[0]}_${monthList[monthList.length - 1]}`,
};
},
// 按当前可见月份拉取里程日历数据
syncVisibleCalendarData() {
if (!this.searchForm.sn) return;
const visibleMonthRange = this.getVisibleMonthRange();
if (!visibleMonthRange) return;
const { startMonth, endMonth, rangeKey } = visibleMonthRange;
if (this._dailyMileageRangeKey === rangeKey) return;
const requestParams = {
sn: this.searchForm.sn,
mileageStartTime: startMonth,
mileageEndTime: endMonth,
};
this._dailyMileageRangeKey = rangeKey;
this.getDailyMileageCalendar(requestParams).then((res) => {
// 仅保留当前可见月份范围的数据,避免旧月份残留造成“固定位置”错觉
if (this._dailyMileageRangeKey !== rangeKey) return;
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.customDateData = calendarMap;
this.$nextTick(() => {
this.injectDateCellContent();
});
}).catch(() => {
if (this._dailyMileageRangeKey === rangeKey) {
this._dailyMileageRangeKey = "";
}
});
},
// 里程日历数据源
getDailyMileageCalendar(params) {
return GET_JOURNEYS_DAILYMILEAGE(params);
},
// 根据单元格所属月份计算真实日期,避免 prev-month / next-month 格子误判
getDateKeyByCell(year, month, day, cell) {
let realYear = Number(year);
let realMonth = Number(month);
if (cell.classList.contains("prev-month")) {
realMonth -= 1;
if (realMonth === 0) {
realMonth = 12;
realYear -= 1;
}
} else if (cell.classList.contains("next-month")) {
realMonth += 1;
if (realMonth === 13) {
realMonth = 1;
realYear += 1;
}
}
return `${realYear}-${String(realMonth).padStart(2, "0")}-${String(day).padStart(2, "0")}`;
},
getCurrentMonthKey() {
const now = new Date();
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}`;
},
getHeaderMonthByTarget(target) {
const header = target.closest(".el-date-range-picker__header");
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")}`;
},
// 当前月是最大月份,不能继续往后翻
shouldBlockNextMonth(target) {
const nextButton = target.closest(
".el-date-range-picker__header .el-icon-arrow-right, .el-date-range-picker__header .el-icon-d-arrow-right, .el-date-range-picker__header button"
);
if (!nextButton) return false;
const hasNextIconClass =
nextButton.classList.contains("el-icon-arrow-right") ||
nextButton.classList.contains("el-icon-d-arrow-right") ||
nextButton.querySelector(".el-icon-arrow-right, .el-icon-d-arrow-right");
if (!hasNextIconClass) return false;
const headerMonth = this.getHeaderMonthByTarget(target);
if (!headerMonth) return false;
return headerMonth >= this.getCurrentMonthKey();
},
// 监听 document 捕获阶段,稳定捕获日期弹层头部翻月按钮点击
bindDatePickerHeaderEvents() {
if (!Array.isArray(this._datePickerHeaderHandlers)) {
this._datePickerHeaderHandlers = [];
}
if (this._datePickerHeaderHandlers.length) return;
const handler = (event) => {
const target = event.target;
if (!target || !target.closest) return;
const isHeaderButton = target.closest(
".el-date-range-picker__header button, .el-date-range-picker__header .el-picker-panel__icon-btn"
);
if (!isHeaderButton) return;
const inDatePickerPopup = target.closest(".mileage-datepicker-popup");
if (!inDatePickerPopup) return;
if (this.shouldBlockNextMonth(target)) {
event.preventDefault();
event.stopPropagation();
if (typeof event.stopImmediatePropagation === "function") {
event.stopImmediatePropagation();
}
return;
}
this.scheduleCalendarRefresh();
};
document.addEventListener("mousedown", handler, true);
this._datePickerHeaderHandlers.push({
button: document,
handler,
capture: true,
});
},
// 关闭面板时移除 document 点击事件,避免重复绑定
unbindDatePickerHeaderEvents() {
const headerHandlers = Array.isArray(this._datePickerHeaderHandlers)
? this._datePickerHeaderHandlers
: [];
headerHandlers.forEach(({ button, handler, capture }) => {
button.removeEventListener("mousedown", handler, capture);
});
this._datePickerHeaderHandlers = [];
clearTimeout(this._datePickerRenderTimer);
this._datePickerRenderTimer = null;
},
// 给日期单元格打标记 class 和属性,再由 CSS 渲染红点和文案
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");
});
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');
// 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);
}
});
});
setTimeout(() => {
this._isInjectingDateCells = false;
}, 0);
},
// 获取查询参数
getParams() {
let oParams = deepClone(this.searchForm);
......@@ -241,4 +555,64 @@ export default {
align-items: center;
justify-content: space-between;
}
</style>>
\ No newline at end of file
</style>
<style lang="scss">
/* 日期选择器自定义内容样式 */
.mileage-datepicker-popup .el-date-table td div {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100%;
height: 38px;
box-sizing: border-box;
text-align: center;
padding-top: 2px;
padding-bottom: 2px;
overflow: visible;
}
.mileage-datepicker-popup .mileage-date-inner-custom {
text-align: center;
}
.mileage-datepicker-popup .mileage-date-inner-custom > span {
position: static !important;
left: auto !important;
right: auto !important;
top: auto !important;
bottom: auto !important;
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 {
display: block;
width: 6px;
height: 6px;
border-radius: 50%;
background: #f56c6c;
margin: 0 0 2px 0 !important;
flex: 0 0 auto;
}
.mileage-datepicker-popup .date-cell-label {
display: block;
width: auto !important;
height: auto !important;
font-size: 10px;
line-height: 1 !important;
white-space: nowrap;
color: #f56c6c;
margin: 2px 0 0 0 !important;
flex: 0 0 auto;
}
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!