mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
fix(workbench): 工具栏改为缩略+点击展开 Popover,优化窄屏布局
This commit is contained in:
@@ -5,7 +5,7 @@ import { useRouter } from 'vue-router';
|
||||
import { Page } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import { DatePicker, InputNumber, message, Select, Spin, Tabs } from 'ant-design-vue';
|
||||
import { DatePicker, InputNumber, message, Popover, Select, Spin, Tabs } from 'ant-design-vue';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { getPlatformPageApi } from '#/api/ecommerce/platform/apis';
|
||||
@@ -31,6 +31,16 @@ import WorkbenchSalesRankTab from './tabs/sales-rank/index.vue';
|
||||
|
||||
defineOptions({ name: 'PddWorkbench' });
|
||||
|
||||
const openPopover = ref<string | null>(null);
|
||||
|
||||
function handlePopoverOpen(name: string, open: boolean) {
|
||||
if (open) {
|
||||
openPopover.value = name;
|
||||
} else if (openPopover.value === name) {
|
||||
openPopover.value = null;
|
||||
}
|
||||
}
|
||||
|
||||
const EMPTY_MULTI: WorkbenchVO.MultiPanel = {
|
||||
platform: [],
|
||||
shop: [],
|
||||
@@ -79,6 +89,9 @@ const router = useRouter();
|
||||
|
||||
const ImportOrderIcon = createIconifyIcon('lucide:upload');
|
||||
const PromotionFeeIcon = createIconifyIcon('lucide:dollar-sign');
|
||||
const ToolbarToggleIcon = createIconifyIcon('lucide:sliders-horizontal');
|
||||
const ReceiptIcon = createIconifyIcon('lucide:receipt');
|
||||
const CalendarIcon = createIconifyIcon('lucide:calendar');
|
||||
|
||||
function goToImportOrder() {
|
||||
void router.push('/import/order');
|
||||
@@ -112,7 +125,7 @@ const toolbarActionClass =
|
||||
'inline-flex min-w-0 shrink-0 cursor-pointer items-center justify-center gap-1.5 rounded-xl border-0 bg-transparent px-3 py-2 text-sm font-bold text-slate-800 transition-colors hover:bg-slate-100 dark:text-slate-200 dark:hover:bg-[hsl(240deg_3.7%_22%)]';
|
||||
|
||||
const timeRangeBtnClass =
|
||||
'inline-flex min-w-0 cursor-pointer items-center justify-center rounded-xl border-0 px-3 py-2 text-sm font-bold transition-colors sm:px-5';
|
||||
'inline-flex min-w-0 shrink-0 cursor-pointer items-center justify-center rounded-xl border-0 px-3 py-2 text-sm font-bold whitespace-nowrap transition-colors sm:px-5';
|
||||
|
||||
function timeRangeBtnState(active: boolean) {
|
||||
return active
|
||||
@@ -621,187 +634,246 @@ onUnmounted(() => {
|
||||
<div
|
||||
class="workbench-header-extra flex w-full max-w-full flex-col gap-2 sm:w-auto sm:flex-row sm:items-center sm:gap-2"
|
||||
>
|
||||
<div :class="toolbarShellClass">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-0.5 overflow-x-auto p-1']"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
:class="toolbarActionClass"
|
||||
@click="goToImportOrder"
|
||||
<!-- 导入 -->
|
||||
<Popover
|
||||
trigger="click"
|
||||
:open="openPopover === 'import'"
|
||||
placement="bottom"
|
||||
@update:open="(val: boolean) => handlePopoverOpen('import', val)"
|
||||
overlay-class-name="workbench-toolbar-popover"
|
||||
>
|
||||
<template #content>
|
||||
<div class="flex items-center gap-0.5 whitespace-nowrap">
|
||||
<button
|
||||
type="button"
|
||||
:class="toolbarActionClass"
|
||||
@click="goToImportOrder"
|
||||
>
|
||||
<ImportOrderIcon class="h-4 w-4 shrink-0" />
|
||||
导入订单
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="toolbarActionClass"
|
||||
@click="goToImportPromotionFee"
|
||||
>
|
||||
<PromotionFeeIcon class="h-4 w-4 shrink-0" />
|
||||
导入推广费
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<div :class="[toolbarShellClass, 'cursor-pointer']">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-1 px-2 py-1']"
|
||||
>
|
||||
<ImportOrderIcon class="h-4 w-4 shrink-0" />
|
||||
导入订单
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="toolbarActionClass"
|
||||
@click="goToImportPromotionFee"
|
||||
>
|
||||
<PromotionFeeIcon class="h-4 w-4 shrink-0" />
|
||||
导入推广费
|
||||
</button>
|
||||
<span class="whitespace-nowrap text-xs font-bold text-slate-700 dark:text-slate-300">导入</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div :class="toolbarShellClass">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-2 px-2 py-1']"
|
||||
>
|
||||
<span class="shrink-0 text-xs font-bold text-slate-700 dark:text-slate-300">
|
||||
税率
|
||||
</span>
|
||||
<InputNumber
|
||||
v-model:value="taxRate"
|
||||
:min="0"
|
||||
:max="1"
|
||||
:step="0.01"
|
||||
size="small"
|
||||
class="tax-rate-input !h-8 !w-20 !rounded-lg !border-none !bg-transparent !text-center !text-sm !font-bold !text-slate-800 !shadow-none dark:!text-slate-100"
|
||||
:formatter="formatTaxRate"
|
||||
:parser="parseTaxRate"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
:class="[
|
||||
toolbarShellClass,
|
||||
'workbench-filter-platform-shop min-w-0 sm:max-w-[min(calc(100vw-2.5rem),56rem)]',
|
||||
]"
|
||||
</Popover>
|
||||
|
||||
<!-- 税率 -->
|
||||
<Popover
|
||||
trigger="click"
|
||||
:open="openPopover === 'taxRate'"
|
||||
placement="bottom"
|
||||
@update:open="(val: boolean) => handlePopoverOpen('taxRate', val)"
|
||||
overlay-class-name="workbench-toolbar-popover"
|
||||
>
|
||||
<div
|
||||
:class="[
|
||||
toolbarPanelClass,
|
||||
'flex min-w-0 flex-col gap-2 p-1 sm:flex-row sm:items-center sm:gap-0 sm:divide-x sm:divide-slate-200 dark:sm:divide-[hsl(240deg_3.7%_28%)]',
|
||||
]"
|
||||
>
|
||||
<div
|
||||
class="flex min-w-0 items-center gap-2 px-2 py-0.5 sm:w-[11.5rem] sm:flex-none sm:shrink-0 sm:py-0"
|
||||
>
|
||||
<span
|
||||
class="w-9 shrink-0 text-right text-xs font-bold text-slate-700 dark:text-slate-300"
|
||||
>
|
||||
平台
|
||||
<template #content>
|
||||
<div class="flex items-center gap-2 whitespace-nowrap">
|
||||
<span class="shrink-0 text-xs font-bold text-slate-700 dark:text-slate-300">
|
||||
税率
|
||||
</span>
|
||||
<Select
|
||||
v-model:value="selectedPlatformSlug"
|
||||
<InputNumber
|
||||
v-model:value="taxRate"
|
||||
:min="0"
|
||||
:max="1"
|
||||
:step="0.01"
|
||||
size="small"
|
||||
show-search
|
||||
allow-clear
|
||||
:filter-option="false"
|
||||
:loading="platformLoading"
|
||||
:options="platformSelectOptions"
|
||||
placeholder="全部平台"
|
||||
class="workbench-header-select min-w-0 flex-1"
|
||||
@search="handlePlatformSearch"
|
||||
@popup-scroll="handlePlatformPopupScroll"
|
||||
@dropdown-visible-change="handlePlatformDropdownVisibleChange"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="platformLoading" class="py-3 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-1 text-xs text-slate-600">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="py-3 text-center text-xs text-slate-600">
|
||||
暂无平台
|
||||
</div>
|
||||
</template>
|
||||
</Select>
|
||||
class="tax-rate-input !h-8 !w-20 !rounded-lg !border-none !bg-transparent !text-center !text-sm !font-bold !text-slate-800 !shadow-none dark:!text-slate-100"
|
||||
:formatter="formatTaxRate"
|
||||
:parser="parseTaxRate"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
<div :class="[toolbarShellClass, 'cursor-pointer']">
|
||||
<div
|
||||
class="flex min-w-0 flex-1 items-center gap-2 border-t border-slate-200/80 px-2 pt-2 sm:min-w-[16rem] sm:border-t-0 sm:py-0 sm:pt-0 dark:border-[hsl(240deg_3.7%_22%)]"
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-1 px-2 py-1']"
|
||||
>
|
||||
<span
|
||||
class="w-9 shrink-0 text-right text-xs font-bold text-slate-700 dark:text-slate-300"
|
||||
>
|
||||
店铺
|
||||
</span>
|
||||
<Select
|
||||
v-model:value="selectedShopSlug"
|
||||
size="small"
|
||||
show-search
|
||||
allow-clear
|
||||
:filter-option="false"
|
||||
:loading="shopLoading"
|
||||
:options="shopSelectOptions"
|
||||
placeholder="全部店铺"
|
||||
:dropdown-match-select-width="false"
|
||||
class="workbench-header-select workbench-shop-select min-w-0 flex-1"
|
||||
@search="handleShopSearch"
|
||||
@popup-scroll="handleShopPopupScroll"
|
||||
@dropdown-visible-change="handleShopDropdownVisibleChange"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="shopLoading" class="py-3 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-1 text-xs text-slate-600">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="py-3 text-center text-xs text-slate-600">
|
||||
暂无店铺
|
||||
</div>
|
||||
</template>
|
||||
</Select>
|
||||
<ReceiptIcon class="h-4 w-4 shrink-0" />
|
||||
<span class="whitespace-nowrap text-xs font-bold text-slate-700 dark:text-slate-300">税率{{ formatTaxRate(taxRate) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div :class="toolbarShellClass">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex w-full items-center gap-1 overflow-x-auto p-1']"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'today')]"
|
||||
@click="switchTimeRange('today')"
|
||||
</Popover>
|
||||
|
||||
<!-- 平台/店铺 -->
|
||||
<Popover
|
||||
trigger="click"
|
||||
:open="openPopover === 'filter'"
|
||||
placement="bottom"
|
||||
@update:open="(val: boolean) => handlePopoverOpen('filter', val)"
|
||||
overlay-class-name="workbench-toolbar-popover"
|
||||
>
|
||||
<template #content>
|
||||
<div
|
||||
class="flex flex-col gap-2 sm:flex-row sm:items-center sm:gap-0"
|
||||
>
|
||||
今日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'yesterday')]"
|
||||
@click="switchTimeRange('yesterday')"
|
||||
<div
|
||||
class="flex min-w-0 items-center gap-2 px-2 py-0.5 sm:w-[11.5rem] sm:flex-none sm:shrink-0 sm:py-0"
|
||||
>
|
||||
<span
|
||||
class="w-9 shrink-0 text-right text-xs font-bold text-slate-700 dark:text-slate-300"
|
||||
>
|
||||
平台
|
||||
</span>
|
||||
<Select
|
||||
v-model:value="selectedPlatformSlug"
|
||||
size="small"
|
||||
show-search
|
||||
allow-clear
|
||||
:filter-option="false"
|
||||
:loading="platformLoading"
|
||||
:options="platformSelectOptions"
|
||||
placeholder="全部平台"
|
||||
class="workbench-header-select min-w-0 flex-1"
|
||||
@search="handlePlatformSearch"
|
||||
@popup-scroll="handlePlatformPopupScroll"
|
||||
@dropdown-visible-change="handlePlatformDropdownVisibleChange"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="platformLoading" class="py-3 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-1 text-xs text-slate-600">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="py-3 text-center text-xs text-slate-600">
|
||||
暂无平台
|
||||
</div>
|
||||
</template>
|
||||
</Select>
|
||||
</div>
|
||||
<div
|
||||
class="flex min-w-0 flex-1 items-center gap-2 border-t border-slate-200/80 px-2 pt-2 sm:min-w-[16rem] sm:border-t-0 sm:py-0 sm:pt-0 dark:border-[hsl(240deg_3.7%_22%)]"
|
||||
>
|
||||
<span
|
||||
class="w-9 shrink-0 text-right text-xs font-bold text-slate-700 dark:text-slate-300"
|
||||
>
|
||||
店铺
|
||||
</span>
|
||||
<Select
|
||||
v-model:value="selectedShopSlug"
|
||||
size="small"
|
||||
show-search
|
||||
allow-clear
|
||||
:filter-option="false"
|
||||
:loading="shopLoading"
|
||||
:options="shopSelectOptions"
|
||||
placeholder="全部店铺"
|
||||
:dropdown-match-select-width="false"
|
||||
class="workbench-header-select workbench-shop-select min-w-0 flex-1"
|
||||
@search="handleShopSearch"
|
||||
@popup-scroll="handleShopPopupScroll"
|
||||
@dropdown-visible-change="handleShopDropdownVisibleChange"
|
||||
>
|
||||
<template #notFoundContent>
|
||||
<div v-if="shopLoading" class="py-3 text-center">
|
||||
<Spin size="small" />
|
||||
<div class="mt-1 text-xs text-slate-600">加载中...</div>
|
||||
</div>
|
||||
<div v-else class="py-3 text-center text-xs text-slate-600">
|
||||
暂无店铺
|
||||
</div>
|
||||
</template>
|
||||
</Select>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<div :class="[toolbarShellClass, 'cursor-pointer']">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-1 px-2 py-1']"
|
||||
>
|
||||
昨日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === '7days')]"
|
||||
@click="switchTimeRange('7days')"
|
||||
>
|
||||
近7日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === '30days')]"
|
||||
@click="switchTimeRange('30days')"
|
||||
>
|
||||
近30日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'thisMonth')]"
|
||||
@click="switchTimeRange('thisMonth')"
|
||||
>
|
||||
本月
|
||||
</button>
|
||||
<ToolbarToggleIcon class="h-4 w-4 shrink-0" />
|
||||
<span class="whitespace-nowrap text-xs font-bold text-slate-700 dark:text-slate-300">{{ selectedPlatformLabelCache || selectedShopLabelCache ? `${selectedPlatformLabelCache || '全部'}/${selectedShopLabelCache || '全部'}` : '平台/店铺' }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
class="flex w-full items-center justify-between gap-2 sm:w-auto sm:justify-start"
|
||||
>
|
||||
<span
|
||||
class="hidden whitespace-nowrap text-xs font-bold text-slate-700 sm:inline dark:text-slate-300"
|
||||
</Popover>
|
||||
|
||||
<!-- 时间范围 -->
|
||||
<Popover
|
||||
trigger="click"
|
||||
:open="openPopover === 'timeRange'"
|
||||
placement="bottom"
|
||||
@update:open="(val: boolean) => handlePopoverOpen('timeRange', val)"
|
||||
overlay-class-name="workbench-toolbar-popover"
|
||||
>
|
||||
<template #content>
|
||||
<div class="flex flex-col gap-2 px-1 py-0.5">
|
||||
<div class="flex items-center gap-1 overflow-x-auto whitespace-nowrap">
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'today')]"
|
||||
@click="switchTimeRange('today')"
|
||||
>
|
||||
今日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'yesterday')]"
|
||||
@click="switchTimeRange('yesterday')"
|
||||
>
|
||||
昨日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === '7days')]"
|
||||
@click="switchTimeRange('7days')"
|
||||
>
|
||||
近7日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === '30days')]"
|
||||
@click="switchTimeRange('30days')"
|
||||
>
|
||||
近30日
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
:class="[timeRangeBtnClass, timeRangeBtnState(activeTimeRange === 'thisMonth')]"
|
||||
@click="switchTimeRange('thisMonth')"
|
||||
>
|
||||
本月
|
||||
</button>
|
||||
</div>
|
||||
<div class="flex items-center gap-2">
|
||||
<span
|
||||
class="whitespace-nowrap text-xs font-bold text-slate-700 dark:text-slate-300"
|
||||
>
|
||||
自定义日期
|
||||
</span>
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="customDateRange"
|
||||
size="small"
|
||||
format="YYYY-MM-DD"
|
||||
:allow-clear="false"
|
||||
:placeholder="['开始', '结束']"
|
||||
class="workbench-header-range-picker !h-8 !flex-1 !border-none !bg-transparent !text-sm !font-bold !text-slate-800 dark:!text-slate-200"
|
||||
@change="handleCustomDateRangeChange"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
<div :class="[toolbarShellClass, 'cursor-pointer']">
|
||||
<div
|
||||
:class="[toolbarPanelClass, 'flex items-center gap-1 px-2 py-1']"
|
||||
>
|
||||
自定义日期
|
||||
</span>
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="customDateRange"
|
||||
size="small"
|
||||
format="YYYY-MM-DD"
|
||||
:allow-clear="false"
|
||||
:placeholder="['开始', '结束']"
|
||||
class="workbench-header-range-picker !h-8 !w-full !border-none !bg-transparent !text-sm !font-bold !text-slate-800 sm:!w-[220px] dark:!text-slate-200"
|
||||
@change="handleCustomDateRangeChange"
|
||||
/>
|
||||
<CalendarIcon class="h-4 w-4 shrink-0" />
|
||||
<span class="whitespace-nowrap text-xs font-bold text-slate-700 dark:text-slate-300">
|
||||
{{ activeTimeRange === 'today' ? '今日' : activeTimeRange === 'yesterday' ? '昨日' : activeTimeRange === '7days' ? '近7日' : activeTimeRange === '30days' ? '近30日' : activeTimeRange === 'thisMonth' ? '本月' : customDateRange ? `${customDateRange[0].format('M/D')}-${customDateRange[1].format('M/D')}` : '日期' }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Popover>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -1015,6 +1087,7 @@ onUnmounted(() => {
|
||||
:deep(.workbench-header-extra .tax-rate-input .ant-input-number-input) {
|
||||
height: 32px;
|
||||
line-height: 32px;
|
||||
text-align: center;
|
||||
padding-top: 0;
|
||||
padding-bottom: 0;
|
||||
color: rgb(15 23 42);
|
||||
@@ -1065,4 +1138,40 @@ html.dark .workbench-header-extra .workbench-header-range-picker .ant-picker-sep
|
||||
html.dark .workbench-header-extra .workbench-header-range-picker .ant-picker-suffix {
|
||||
color: hsl(240deg 5% 75%);
|
||||
}
|
||||
|
||||
.workbench-toolbar-popover .ant-popover-inner {
|
||||
padding: 4px !important;
|
||||
border-radius: 12px !important;
|
||||
}
|
||||
|
||||
html.dark .workbench-toolbar-popover .ant-popover-inner {
|
||||
background-color: hsl(222.34deg 10.43% 14%) !important;
|
||||
}
|
||||
|
||||
html.dark .workbench-toolbar-popover .ant-popover-arrow {
|
||||
--antd-arrow-background-color: hsl(222.34deg 10.43% 14%) !important;
|
||||
}
|
||||
|
||||
.workbench-toolbar-popover .tax-rate-input.ant-input-number {
|
||||
background: transparent !important;
|
||||
border: none !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
|
||||
.workbench-toolbar-popover .tax-rate-input .ant-input-number-input {
|
||||
height: 32px !important;
|
||||
line-height: 32px !important;
|
||||
text-align: center !important;
|
||||
padding-top: 0 !important;
|
||||
padding-bottom: 0 !important;
|
||||
background: transparent !important;
|
||||
}
|
||||
|
||||
.workbench-toolbar-popover .tax-rate-input:hover .ant-input-number-input {
|
||||
background-color: rgb(241 245 249) !important;
|
||||
}
|
||||
|
||||
html.dark .workbench-toolbar-popover .tax-rate-input .ant-input-number-input {
|
||||
color: hsl(0deg 0% 95%) !important;
|
||||
}
|
||||
</style>
|
||||
|
||||
+106
-65
@@ -2,7 +2,7 @@
|
||||
import type { EchartsUIType } from '@vben/plugins/echarts';
|
||||
import type { WorkbenchVO } from '#/api/ecommerce/workbench/types';
|
||||
|
||||
import { nextTick, onMounted, ref, watch } from 'vue';
|
||||
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue';
|
||||
|
||||
import { EchartsUI, useEcharts } from '@vben/plugins/echarts';
|
||||
|
||||
@@ -33,91 +33,131 @@ const chartOverviewShell =
|
||||
const pieChartRef = ref<EchartsUIType>();
|
||||
const { renderEcharts: renderPieChart } = useEcharts(pieChartRef);
|
||||
|
||||
let renderTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
let isUnmounted = false;
|
||||
|
||||
/** 与趋势图一致:有切片且存在非零销售额才绘制,避免空态卸载图表 DOM */
|
||||
const hasData = computed(
|
||||
() =>
|
||||
props.platformSalesShare.length > 0 &&
|
||||
props.platformSalesShare.some((row) => Number(row.value ?? 0) !== 0),
|
||||
);
|
||||
|
||||
function colorForIndex(index: number): string {
|
||||
return PLATFORM_PIE_COLORS[index % PLATFORM_PIE_COLORS.length] ?? '#6366f1';
|
||||
}
|
||||
|
||||
function drawPie() {
|
||||
if (!pieChartRef.value) return;
|
||||
const isDark = document.documentElement.classList.contains('dark');
|
||||
const labelColor = isDark ? 'hsl(0deg 0% 95%)' : '#334155';
|
||||
const legendColor = isDark ? 'hsl(240deg 5% 64.9%)' : '#64748b';
|
||||
const border = isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff';
|
||||
if (!pieChartRef.value || !hasData.value) return;
|
||||
try {
|
||||
const isDark = document.documentElement.classList.contains('dark');
|
||||
const labelColor = isDark ? 'hsl(0deg 0% 95%)' : '#334155';
|
||||
const legendColor = isDark ? 'hsl(240deg 5% 64.9%)' : '#64748b';
|
||||
const border = isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff';
|
||||
|
||||
renderPieChart({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff',
|
||||
textStyle: { color: isDark ? 'hsl(0deg 0% 95%)' : '#0f172a' },
|
||||
formatter: (params: any) => {
|
||||
const name = String(params.name ?? '');
|
||||
const val = Number(params.value ?? 0);
|
||||
const row = props.platformSalesShare.find((r) => r.name === name);
|
||||
const pctText =
|
||||
row?.pct != null ? `${row.pct}%(占实际销售额)` : '';
|
||||
return `${name}<br/>约 ${formatWanShort(val)}<br/>${formatYuanDetail(val)}<br/>${pctText}`;
|
||||
renderPieChart({
|
||||
tooltip: {
|
||||
trigger: 'item',
|
||||
backgroundColor: isDark ? 'hsl(222.34deg 10.43% 12.27%)' : '#fff',
|
||||
textStyle: { color: isDark ? 'hsl(0deg 0% 95%)' : '#0f172a' },
|
||||
formatter: (params: any) => {
|
||||
const name = String(params.name ?? '');
|
||||
const val = Number(params.value ?? 0);
|
||||
const row = props.platformSalesShare.find((r) => r.name === name);
|
||||
const pctText =
|
||||
row?.pct != null ? `${row.pct}%(占实际销售额)` : '';
|
||||
return `${name}<br/>约 ${formatWanShort(val)}<br/>${formatYuanDetail(val)}<br/>${pctText}`;
|
||||
},
|
||||
},
|
||||
},
|
||||
legend: {
|
||||
orient: 'vertical',
|
||||
right: '0',
|
||||
top: 'center',
|
||||
itemWidth: 10,
|
||||
itemHeight: 10,
|
||||
textStyle: { color: legendColor, fontSize: 11, fontWeight: 700 },
|
||||
},
|
||||
series: [
|
||||
{
|
||||
type: 'pie',
|
||||
radius: ['40%', '68%'],
|
||||
center: ['36%', '50%'],
|
||||
avoidLabelOverlap: true,
|
||||
itemStyle: {
|
||||
borderRadius: 6,
|
||||
borderColor: border,
|
||||
borderWidth: 2,
|
||||
},
|
||||
label: {
|
||||
show: true,
|
||||
formatter: '{b}\n{d}%',
|
||||
color: labelColor,
|
||||
fontSize: 11,
|
||||
fontWeight: 700,
|
||||
},
|
||||
labelLine: {
|
||||
length: 12,
|
||||
length2: 8,
|
||||
lineStyle: { color: isDark ? 'hsl(240deg 3.7% 35%)' : '#cbd5e1' },
|
||||
},
|
||||
data: props.platformSalesShare.map((row, index) => ({
|
||||
name: row.name,
|
||||
value: row.value ?? 0,
|
||||
legend: {
|
||||
orient: 'vertical',
|
||||
right: '0',
|
||||
top: 'center',
|
||||
itemWidth: 10,
|
||||
itemHeight: 10,
|
||||
textStyle: { color: legendColor, fontSize: 11, fontWeight: 700 },
|
||||
},
|
||||
series: [
|
||||
{
|
||||
type: 'pie',
|
||||
radius: ['40%', '68%'],
|
||||
center: ['36%', '50%'],
|
||||
avoidLabelOverlap: true,
|
||||
itemStyle: {
|
||||
color: colorForIndex(index),
|
||||
borderRadius: 6,
|
||||
borderColor: border,
|
||||
borderWidth: 2,
|
||||
},
|
||||
})),
|
||||
},
|
||||
],
|
||||
});
|
||||
label: {
|
||||
show: true,
|
||||
formatter: '{b}\n{d}%',
|
||||
color: labelColor,
|
||||
fontSize: 11,
|
||||
fontWeight: 700,
|
||||
},
|
||||
labelLine: {
|
||||
length: 12,
|
||||
length2: 8,
|
||||
lineStyle: { color: isDark ? 'hsl(240deg 3.7% 35%)' : '#cbd5e1' },
|
||||
},
|
||||
data: props.platformSalesShare.map((row, index) => ({
|
||||
name: row.name,
|
||||
value: row.value ?? 0,
|
||||
itemStyle: {
|
||||
color: colorForIndex(index),
|
||||
},
|
||||
})),
|
||||
},
|
||||
],
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('[WorkbenchFinancePlatformSalesPieChart] render failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
/** 延迟一帧再绘制,确保 v-show 切换后 ref 与容器尺寸已就绪 */
|
||||
async function scheduleRender() {
|
||||
if (renderTimer) {
|
||||
clearTimeout(renderTimer);
|
||||
renderTimer = null;
|
||||
}
|
||||
await nextTick();
|
||||
if (isUnmounted || !hasData.value) return;
|
||||
renderTimer = setTimeout(() => {
|
||||
if (isUnmounted) return;
|
||||
drawPie();
|
||||
}, 50);
|
||||
}
|
||||
|
||||
async function redraw() {
|
||||
await nextTick();
|
||||
drawPie();
|
||||
await scheduleRender();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
void redraw();
|
||||
isUnmounted = false;
|
||||
void scheduleRender();
|
||||
});
|
||||
|
||||
onUnmounted(() => {
|
||||
isUnmounted = true;
|
||||
if (renderTimer) {
|
||||
clearTimeout(renderTimer);
|
||||
renderTimer = null;
|
||||
}
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.platformSalesShare,
|
||||
() => {
|
||||
void redraw();
|
||||
void scheduleRender();
|
||||
},
|
||||
{ deep: true },
|
||||
{ deep: true, immediate: true },
|
||||
);
|
||||
|
||||
watch(hasData, (ok) => {
|
||||
if (ok) void scheduleRender();
|
||||
});
|
||||
|
||||
defineExpose({ redraw });
|
||||
</script>
|
||||
|
||||
@@ -137,12 +177,13 @@ defineExpose({ redraw });
|
||||
</div>
|
||||
<div class="px-6 py-5 md:px-8 md:py-6">
|
||||
<div
|
||||
v-if="!platformSalesShare.length"
|
||||
v-show="!hasData"
|
||||
class="flex h-[280px] items-center justify-center text-sm font-bold text-slate-400"
|
||||
>
|
||||
暂无销售额数据
|
||||
</div>
|
||||
<div v-else class="h-[280px] w-full">
|
||||
<!-- 用 v-show 保持 Echarts 实例挂载,避免切换日期时 v-if 销毁导致图表无法重绘 -->
|
||||
<div v-show="hasData" class="h-[280px] w-full">
|
||||
<EchartsUI ref="pieChartRef" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user