diff --git a/ecom-dmb-frontend/apps/web-antd/src/utils/overview-date-range.ts b/ecom-dmb-frontend/apps/web-antd/src/utils/overview-date-range.ts new file mode 100644 index 00000000..df58459a --- /dev/null +++ b/ecom-dmb-frontend/apps/web-antd/src/utils/overview-date-range.ts @@ -0,0 +1,108 @@ +/** + * 全店 / 店铺 / 商品概览共用:自定义日期范围解析与 URL 同步(与 history.replaceState 配合)。 + */ +import type { Ref } from 'vue'; + +import type { Dayjs } from 'dayjs'; +import dayjs from 'dayjs'; + +/** 解析 RangePicker 单端;缺省或非法时返回 null(勿对缺省值调 dayjs(),undefined 会被当成「当前时刻」) */ +export function parseOverviewRangePickerPart( + part: Dayjs | null | string | undefined, + dateStr?: string, +): Dayjs | null { + if (part != null && part !== '') { + const d = dayjs.isDayjs(part) ? part : dayjs(part); + return d.isValid() ? d.startOf('day') : null; + } + if (dateStr != null && String(dateStr).trim() !== '') { + const d = dayjs(dateStr); + return d.isValid() ? d.startOf('day') : null; + } + return null; +} + +/** + * 规范化 RangePicker 的 change 结果(同一天点两次时 antd 可能只给出开始、结束为 null)。 + */ +export function normalizeOverviewRangePickerChange( + value: [Dayjs, Dayjs] | [string, string] | null | undefined, + dateStrings?: [string, string], +): [Dayjs, Dayjs] | null { + if (!value || !Array.isArray(value) || value.length !== 2) { + return null; + } + const start = parseOverviewRangePickerPart(value[0], dateStrings?.[0]); + if (!start) { + return null; + } + let end = parseOverviewRangePickerPart(value[1], dateStrings?.[1]); + if (!end) { + end = start; + } + return [start, end]; +} + +const START_Q = 'startDate'; +const END_Q = 'endDate'; + +/** 从当前地址栏读取概览日期 query(含 replaceState 写入、未反映到 Vue Router 的情况) */ +export function getOverviewDateQueryFromLocation(): { + startDate: string; + endDate: string; +} | null { + const url = new URL(window.location.href); + const startDate = url.searchParams.get(START_Q); + const endDate = url.searchParams.get(END_Q); + if (startDate && endDate) { + return { startDate, endDate }; + } + return null; +} + +/** 将当前概览日期范围写入 URL query(replaceState,不触发 Vue Router 的 query 更新) */ +export function syncOverviewDateRangeToUrl(dateRange: [Dayjs, Dayjs]): void { + const startDate = dateRange[0].format('YYYY-MM-DD'); + const endDate = dateRange[1].format('YYYY-MM-DD'); + const url = new URL(window.location.href); + const curStart = url.searchParams.get(START_Q); + const curEnd = url.searchParams.get(END_Q); + if (curStart === startDate && curEnd === endDate) { + return; + } + const newUrl = new URL(window.location.href); + newUrl.searchParams.set(START_Q, startDate); + newUrl.searchParams.set(END_Q, endDate); + window.history.replaceState({}, '', newUrl.toString()); +} + +/** + * 从当前 window.location 读取 startDate/endDate,写回 dateRange 并回调推断快捷项(本月/今日等)。 + */ +export function applyOverviewDateRangeFromUrl( + dateRangeRef: Ref<[Dayjs, Dayjs]>, + onRangeFromUrl: (start: Dayjs, end: Dayjs) => void, +): boolean { + const url = new URL(window.location.href); + const startDate = url.searchParams.get(START_Q); + const endDate = url.searchParams.get(END_Q); + if (!startDate || !endDate) { + return false; + } + + const parsedStartDate = dayjs(startDate); + const parsedEndDate = dayjs(endDate); + if (!parsedStartDate.isValid() || !parsedEndDate.isValid()) { + return false; + } + + const isSameRange = + dateRangeRef.value[0].isSame(parsedStartDate, 'day') && + dateRangeRef.value[1].isSame(parsedEndDate, 'day'); + + if (!isSameRange) { + dateRangeRef.value = [parsedStartDate, parsedEndDate]; + } + onRangeFromUrl(parsedStartDate, parsedEndDate); + return true; +} diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/index.vue index 458db10e..ef41fe49 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/dashboard/store-overview/index.vue @@ -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); }; // 加载概览数据 diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/index.vue index 57c59fd1..eabddb32 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/index.vue @@ -20,6 +20,7 @@ import ProductFormModal from './components/ProductFormModal.vue'; defineOptions({ name: 'ProductManagement' }); const PlusIcon = createIconifyIcon('lucide:plus'); +const RefreshCwIcon = createIconifyIcon('lucide:refresh-cw'); const productFormModalRef = ref(); @@ -123,6 +124,7 @@ async function handleSearchFormReset() { } const loading = ref(false); +const refreshLoading = ref(false); const productList = ref([]); const total = ref(0); const current = ref(1); @@ -196,7 +198,7 @@ async function handleDelete(record: ProductVO.Info) { } } -function refreshTable(updatedProduct?: ProductVO.Info) { +async function refreshTable(updatedProduct?: ProductVO.Info) { if (updatedProduct) { const index = productList.value.findIndex((p) => p.id === updatedProduct.id); if (index !== -1) { @@ -207,7 +209,16 @@ function refreshTable(updatedProduct?: ProductVO.Info) { current.value = 1; productList.value = []; hasMore.value = true; - loadProductList(true); + await loadProductList(true); +} + +async function handleRefresh() { + refreshLoading.value = true; + try { + await refreshTable(); + } finally { + refreshLoading.value = false; + } } const handleEditingChange = async () => { @@ -278,10 +289,23 @@ onUnmounted(() => { 共 {{ total }} 个商品 - +
+ + +
{ background-color: transparent !important; } +/* 圆形图标按钮:内容垂直水平居中(抵消 iconify 行高/基线偏移) */ +.product-toolbar-refresh-btn.ant-btn { + display: inline-flex !important; + align-items: center; + justify-content: center; + padding: 0; +} + +.product-toolbar-refresh-btn.ant-btn :deep(svg) { + display: block; +} + @media (max-width: 640px) { :deep(.product-page-header) { flex-direction: column; diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/overview/components/TimeRangeBar.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/overview/components/TimeRangeBar.vue index e5088c0e..c6ebe17a 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/overview/components/TimeRangeBar.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/product/overview/components/TimeRangeBar.vue @@ -1,7 +1,9 @@ @@ -163,7 +255,7 @@ const pageTitle = computed(() => overviewData.value?.productName || '商品概 -
+
diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/components/ProductComparisonTable.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/components/ProductComparisonTable.vue index dda08d32..392533c2 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/components/ProductComparisonTable.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/components/ProductComparisonTable.vue @@ -4,6 +4,8 @@ import { useRouter } from 'vue-router'; import { Button } from 'ant-design-vue'; +import { getOverviewDateQueryFromLocation } from '#/utils/overview-date-range'; + interface Props { data: ShopVO.OverviewStatistics | null; taxRate: number; @@ -18,6 +20,15 @@ const props = defineProps(); const emit = defineEmits(); const router = useRouter(); +function goToProductOverview(productId: number) { + const q = getOverviewDateQueryFromLocation(); + router.push( + q + ? { path: `/ecom/product/overview/${productId}`, query: q } + : { path: `/ecom/product/overview/${productId}` }, + ); +} + const formatAmount = (amount: number) => `¥${amount.toLocaleString('zh-CN')}`; const formatPercentPlain = (value: number | undefined) => { @@ -134,7 +145,7 @@ const formatPercentPlain = (value: number | undefined) => {
{{ product.productName }}
import { Button, DatePicker, InputNumber } from 'ant-design-vue'; -import dayjs from 'dayjs'; import type { Dayjs } from 'dayjs'; +import dayjs from 'dayjs'; + +import { normalizeOverviewRangePickerChange } from '#/utils/overview-date-range'; interface Props { modelValue: { @@ -87,19 +89,16 @@ const switchTimeRange = ( const handleCustomDateRangeChange = ( value: [Dayjs, Dayjs] | [string, string] | null, + 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]; - emit('update:modelValue', { - ...props.modelValue, - activeTimeRange: 'custom', - dateRange: dates, - customDateRange: dates, - }); - } + const dates = normalizeOverviewRangePickerChange(value, dateStrings); + if (!dates) return; + emit('update:modelValue', { + ...props.modelValue, + activeTimeRange: 'custom', + dateRange: dates, + customDateRange: dates, + }); }; const handleTaxRateChange = (val: number | string | undefined) => { diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue index 243909f1..32190b84 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/ecommerce/shop/overview/index.vue @@ -17,6 +17,10 @@ import dayjs from 'dayjs'; import { getShopOverviewStatisticsApi } from '#/api/ecommerce/shop'; import { exportByPost } from '#/utils/export'; +import { + applyOverviewDateRangeFromUrl, + syncOverviewDateRangeToUrl, +} from '#/utils/overview-date-range'; import TimeRangeBar from './components/TimeRangeBar.vue'; import StatCards from './components/StatCards.vue'; @@ -150,44 +154,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); }; // 加载概览数据 diff --git a/ecom-dmb-frontend/apps/web-antd/src/views/system/profile/index.vue b/ecom-dmb-frontend/apps/web-antd/src/views/system/profile/index.vue index 53e9e40c..8af92b13 100644 --- a/ecom-dmb-frontend/apps/web-antd/src/views/system/profile/index.vue +++ b/ecom-dmb-frontend/apps/web-antd/src/views/system/profile/index.vue @@ -1,5 +1,5 @@