mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-25 05:09:19 +08:00
refactor: 提取概览日期范围工具函数,共享日期范围 URL 同步与异常处理逻辑
- 新增 utils/overview-date-range.ts:统一的概览日期范围处理(从 URL 应用、同步至 URL、normalize 日期选择器变化) - store-overview / shop-overview / product-overview 的 TimeRangeBar 与 index.vue 均改用共享工具 - 修复同一天点击两次时日期范围 picker 值为空的异常处理
This commit is contained in:
@@ -21,6 +21,11 @@ import dayjs from 'dayjs';
|
||||
|
||||
import { getCompanyOverviewApi } from '#/api/ecommerce/store-overview/apis';
|
||||
import { exportByPost } from '#/utils/export';
|
||||
import {
|
||||
applyOverviewDateRangeFromUrl,
|
||||
normalizeOverviewRangePickerChange,
|
||||
syncOverviewDateRangeToUrl,
|
||||
} from '#/utils/overview-date-range';
|
||||
|
||||
import ComparisonSwitcher from './components/ComparisonSwitcher.vue';
|
||||
import KpiCards from './components/KpiCards.vue';
|
||||
@@ -161,23 +166,21 @@ const switchTimeRange = async (
|
||||
await refreshOverviewByCurrentParams();
|
||||
};
|
||||
|
||||
// 自定义日期范围变化
|
||||
// 自定义日期范围变化(同一天点两次时,antd 可能只给出开始日、结束为 null,dayjs(undefined) 会变成「今天」)
|
||||
const handleCustomDateRangeChange = async (
|
||||
value: [dayjs.Dayjs, dayjs.Dayjs] | [string, string] | null,
|
||||
_dateString?: [string, string],
|
||||
dateStrings?: [string, string],
|
||||
) => {
|
||||
if (value && Array.isArray(value) && value.length === 2) {
|
||||
const dates = [
|
||||
dayjs.isDayjs(value[0]) ? value[0] : dayjs(value[0]),
|
||||
dayjs.isDayjs(value[1]) ? value[1] : dayjs(value[1]),
|
||||
] as [dayjs.Dayjs, dayjs.Dayjs];
|
||||
customDateRange.value = dates;
|
||||
activeTimeRange.value = 'custom';
|
||||
dateRange.value = dates;
|
||||
lastRequestKey.value = '';
|
||||
syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
const dates = normalizeOverviewRangePickerChange(value, dateStrings);
|
||||
if (!dates) {
|
||||
return;
|
||||
}
|
||||
customDateRange.value = dates;
|
||||
activeTimeRange.value = 'custom';
|
||||
dateRange.value = dates;
|
||||
lastRequestKey.value = '';
|
||||
syncDateRangeToRouteQuery();
|
||||
await refreshOverviewByCurrentParams();
|
||||
};
|
||||
|
||||
const setTimeRangeTypeByDates = (
|
||||
@@ -233,44 +236,11 @@ const setTimeRangeTypeByDates = (
|
||||
customDateRange.value = [startDate, endDate];
|
||||
};
|
||||
|
||||
const applyDateRangeFromRouteQuery = () => {
|
||||
// 用 window.location 而非 route.query,因为 syncDateRangeToRouteQuery 用的是 history.replaceState
|
||||
const url = new URL(window.location.href);
|
||||
const startDate = url.searchParams.get('startDate');
|
||||
const endDate = url.searchParams.get('endDate');
|
||||
if (!startDate || !endDate) return false;
|
||||
const applyDateRangeFromRouteQuery = () =>
|
||||
applyOverviewDateRangeFromUrl(dateRange, setTimeRangeTypeByDates);
|
||||
|
||||
const parsedStartDate = dayjs(startDate);
|
||||
const parsedEndDate = dayjs(endDate);
|
||||
if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) return false;
|
||||
|
||||
const isSameRange =
|
||||
dateRange.value[0].isSame(parsedStartDate, 'day') &&
|
||||
dateRange.value[1].isSame(parsedEndDate, 'day');
|
||||
|
||||
if (!isSameRange) {
|
||||
dateRange.value = [parsedStartDate, parsedEndDate];
|
||||
}
|
||||
setTimeRangeTypeByDates(parsedStartDate, parsedEndDate);
|
||||
return true;
|
||||
};
|
||||
|
||||
const syncDateRangeToRouteQuery = async () => {
|
||||
const startDate = dateRange.value[0].format('YYYY-MM-DD');
|
||||
const endDate = dateRange.value[1].format('YYYY-MM-DD');
|
||||
|
||||
// 从 window.location 读取(history.replaceState 不会更新 route.query)
|
||||
const url = new URL(window.location.href);
|
||||
const curStart = url.searchParams.get('startDate');
|
||||
const curEnd = url.searchParams.get('endDate');
|
||||
if (curStart === startDate && curEnd === endDate) {
|
||||
return;
|
||||
}
|
||||
|
||||
const newUrl = new URL(window.location.href);
|
||||
newUrl.searchParams.set('startDate', startDate);
|
||||
newUrl.searchParams.set('endDate', endDate);
|
||||
window.history.replaceState({}, '', newUrl.toString());
|
||||
const syncDateRangeToRouteQuery = () => {
|
||||
syncOverviewDateRangeToUrl(dateRange.value);
|
||||
};
|
||||
|
||||
// 加载概览数据
|
||||
|
||||
Reference in New Issue
Block a user