mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:59:19 +08:00
refactor: 将商品成本同步弹窗抽取为独立组件 ProductCostSyncToOrdersModal
- ProductCard.vue / ProductFormModal.vue: 移除成本同步弹窗逻辑,改为引用独立组件 - 新增 ProductCostSyncToOrdersModal.vue: 统一的成本同步确认弹窗组件 - 减少组件复杂度,提升代码可维护性
This commit is contained in:
+59
-380
@@ -6,13 +6,11 @@ import { computed, h, ref, watch } from 'vue';
|
||||
import type { DropdownRender } from 'ant-design-vue/es/vc-select/BaseSelect';
|
||||
import { useRouter } from 'vue-router';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Card,
|
||||
DatePicker,
|
||||
Image,
|
||||
Input,
|
||||
InputNumber,
|
||||
@@ -24,16 +22,11 @@ import {
|
||||
|
||||
import TextArea from 'ant-design-vue/es/input/TextArea';
|
||||
|
||||
import { updateProductApi } from '#/api/ecommerce/product';
|
||||
import {
|
||||
batchSyncCostsToOrdersApi,
|
||||
countOrdersForCostSyncApi,
|
||||
getProductSpecsByIdsApi,
|
||||
} from '#/api/ecommerce/product-spec/apis';
|
||||
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
|
||||
import { getCategoryTreeApi } from '#/api/ecommerce/category';
|
||||
import { updateProductApi } from '#/api/ecommerce/product';
|
||||
import { uploadFileToOss } from '#/utils/oss-upload';
|
||||
import CategoryFormModal from '#/views/ecommerce/config/category-config/components/CategoryFormModal.vue';
|
||||
import ProductCostSyncToOrdersModal from '#/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue';
|
||||
|
||||
const props = defineProps<Props>();
|
||||
const emit = defineEmits<Emits>();
|
||||
@@ -46,131 +39,26 @@ const ImageIcon = createIconifyIcon('lucide:image');
|
||||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
const CheckIcon = createIconifyIcon('lucide:check');
|
||||
const XIcon = createIconifyIcon('lucide:x');
|
||||
const SyncOrdersIcon = createIconifyIcon('lucide:refresh-cw');
|
||||
|
||||
// 成本同步弹窗
|
||||
const [SyncModal, syncModalApi] = useVbenModal({
|
||||
title: '成本变更同步确认',
|
||||
class: 'w-[500px]',
|
||||
draggable: true,
|
||||
contentClass: 'px-5 py-4',
|
||||
cancelText: '暂不同步',
|
||||
confirmText: '立即同步',
|
||||
onConfirm: handleSyncConfirm,
|
||||
onCancel: () => {
|
||||
syncModalApi.close();
|
||||
},
|
||||
});
|
||||
const productCostSyncModalRef = ref<InstanceType<
|
||||
typeof ProductCostSyncToOrdersModal
|
||||
> | null>(null);
|
||||
|
||||
const syncFormData = ref<[string, string] | undefined>(undefined);
|
||||
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
|
||||
const selectedSyncSpecIds = ref<number[]>([]);
|
||||
|
||||
/** 当前时间范围内将参与同步的订单数(null 表示未请求或条件不齐) */
|
||||
const costSyncOrderCount = ref<null | number>(null);
|
||||
const costSyncOrderCountLoading = ref(false);
|
||||
let costSyncCountTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
function scheduleFetchCostSyncOrderCount() {
|
||||
if (costSyncCountTimer) {
|
||||
clearTimeout(costSyncCountTimer);
|
||||
}
|
||||
costSyncCountTimer = setTimeout(() => {
|
||||
costSyncCountTimer = null;
|
||||
void fetchCostSyncOrderCount();
|
||||
}, 300);
|
||||
}
|
||||
|
||||
async function fetchCostSyncOrderCount() {
|
||||
if (
|
||||
!syncFormData.value ||
|
||||
syncFormData.value[0] == null ||
|
||||
syncFormData.value[1] == null
|
||||
) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
if (selectedSyncSpecIds.value.length === 0) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
costSyncOrderCountLoading.value = true;
|
||||
try {
|
||||
const [startTime, endTime] = syncFormData.value;
|
||||
costSyncOrderCount.value = await countOrdersForCostSyncApi({
|
||||
specIds: [...selectedSyncSpecIds.value],
|
||||
startTime,
|
||||
endTime,
|
||||
});
|
||||
} catch {
|
||||
costSyncOrderCount.value = null;
|
||||
} finally {
|
||||
costSyncOrderCountLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [...selectedSyncSpecIds.value],
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
const hasSyncableSpecs = computed(() =>
|
||||
(props.product.specs || []).some((s) => typeof s.id === 'number' && s.id > 0),
|
||||
);
|
||||
|
||||
watch(
|
||||
syncFormData,
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
async function openSyncModal(costChangedSpecIds: number[]) {
|
||||
try {
|
||||
const specs = await getProductSpecsByIdsApi(costChangedSpecIds);
|
||||
pendingCostChangedSpecs.value = specs ?? [];
|
||||
selectedSyncSpecIds.value = costChangedSpecIds;
|
||||
syncFormData.value = undefined;
|
||||
costSyncOrderCount.value = null;
|
||||
syncModalApi.open();
|
||||
} catch (error) {
|
||||
console.error('获取规格详情失败:', error);
|
||||
message.error('获取规格详情失败,无法进行成本同步');
|
||||
}
|
||||
}
|
||||
|
||||
function toggleSpec(specId: number) {
|
||||
const idx = selectedSyncSpecIds.value.indexOf(specId);
|
||||
if (idx === -1) {
|
||||
selectedSyncSpecIds.value.push(specId);
|
||||
} else {
|
||||
selectedSyncSpecIds.value.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSyncConfirm() {
|
||||
if (selectedSyncSpecIds.value.length === 0) {
|
||||
message.warning('请至少选择一个规格');
|
||||
/** 用户主动:将当前商品各规格成本按时间范围写入订单 */
|
||||
async function openManualCostSyncModal() {
|
||||
const specIds = (props.product.specs || [])
|
||||
.map((s) => s.id)
|
||||
.filter((id): id is number => typeof id === 'number' && id > 0);
|
||||
if (specIds.length === 0) {
|
||||
message.warning('当前商品没有可同步的规格');
|
||||
return;
|
||||
}
|
||||
if (!syncFormData.value || syncFormData.value[0] == null || syncFormData.value[1] == null) {
|
||||
message.warning('请选择同步时间范围');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
syncModalApi.setState?.({ confirmLoading: true });
|
||||
const [startDate, endDate] = syncFormData.value;
|
||||
const totalUpdated = await batchSyncCostsToOrdersApi({
|
||||
specIds: selectedSyncSpecIds.value,
|
||||
startTime: startDate,
|
||||
endTime: endDate,
|
||||
});
|
||||
message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`);
|
||||
syncModalApi.close?.();
|
||||
} catch (error) {
|
||||
console.error('成本同步失败:', error);
|
||||
message.error('成本同步失败');
|
||||
} finally {
|
||||
syncModalApi.setState?.({ confirmLoading: false });
|
||||
}
|
||||
await productCostSyncModalRef.value?.open(specIds, { entry: 'manual' });
|
||||
}
|
||||
|
||||
interface Props {
|
||||
@@ -498,7 +386,9 @@ const saveEdit = async () => {
|
||||
emit('editing-change', false);
|
||||
// 如果有规格成本变更,弹出同步确认框
|
||||
if (result.costChangedSpecIds?.length > 0) {
|
||||
openSyncModal(result.costChangedSpecIds);
|
||||
await productCostSyncModalRef.value?.open(result.costChangedSpecIds, {
|
||||
entry: 'after-save',
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存商品失败:', error);
|
||||
@@ -726,6 +616,15 @@ const saveEdit = async () => {
|
||||
<ChartIcon class="h-5 w-5" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
<Tooltip title="将当前规格成本同步到历史订单(按下单时间范围)">
|
||||
<Button
|
||||
class="footer-icon-btn footer-icon-btn--sync"
|
||||
:disabled="!hasSyncableSpecs"
|
||||
@click="openManualCostSyncModal"
|
||||
>
|
||||
<SyncOrdersIcon class="h-5 w-5" />
|
||||
</Button>
|
||||
</Tooltip>
|
||||
</template>
|
||||
<template v-else>
|
||||
<Tooltip title="取消编辑">
|
||||
@@ -758,80 +657,7 @@ const saveEdit = async () => {
|
||||
|
||||
<CategoryFormModal ref="categoryFormModalRef" @success="reloadCategoryTree" />
|
||||
|
||||
<SyncModal>
|
||||
<div class="product-cost-sync py-4">
|
||||
<!-- 顶部警告提示 -->
|
||||
<div class="sync-alert">
|
||||
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"></circle><line x1="12" y1="8" x2="12" y2="12"></line><line x1="12" y1="16" x2="12.01" y2="16"></line></svg>
|
||||
检测到 <strong>{{ pendingCostChangedSpecs.length }}</strong> 个规格的成本已更新,请勾选需要同步的规格:
|
||||
</div>
|
||||
|
||||
<!-- 规格选择卡片列表 -->
|
||||
<div class="spec-list mb-4 max-h-48 overflow-y-auto rounded-lg">
|
||||
<div
|
||||
v-for="spec in pendingCostChangedSpecs"
|
||||
:key="spec.id"
|
||||
class="spec-item-card"
|
||||
:class="{ active: selectedSyncSpecIds.includes(spec.id) }"
|
||||
@click="toggleSpec(spec.id)"
|
||||
>
|
||||
<div class="checkbox-wrapper">
|
||||
<div class="custom-checkbox">
|
||||
<span v-if="selectedSyncSpecIds.includes(spec.id)" class="check-icon">✓</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="spec-info">
|
||||
<div class="spec-title">{{ spec.name }}</div>
|
||||
<div class="spec-code">{{ spec.specCode || '无编码' }}</div>
|
||||
</div>
|
||||
<div class="cost-snapshot">
|
||||
<div class="cost-tag changed">
|
||||
<span>商品成本</span>
|
||||
<span>¥{{ Number(spec.productCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="cost-tag">
|
||||
<span>运费</span>
|
||||
<span>¥{{ Number(spec.shippingCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="cost-tag">
|
||||
<span>其他</span>
|
||||
<span>¥{{ Number(spec.otherCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 时间范围选择 -->
|
||||
<div class="time-range-section">
|
||||
<label class="section-label">同步时间范围</label>
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="syncFormData"
|
||||
class="w-full"
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
show-time
|
||||
/>
|
||||
<div class="sync-order-count mt-2 text-sm text-gray-600 dark:text-[hsl(240deg_5%_64.9%)]">
|
||||
<template v-if="costSyncOrderCountLoading">正在统计订单数…</template>
|
||||
<template
|
||||
v-else-if="
|
||||
costSyncOrderCount !== null &&
|
||||
syncFormData &&
|
||||
syncFormData[0] &&
|
||||
syncFormData[1] &&
|
||||
selectedSyncSpecIds.length > 0
|
||||
"
|
||||
>
|
||||
当前时间范围内共
|
||||
<strong class="text-gray-900 dark:text-[hsl(0deg_0%_95%)]">{{
|
||||
costSyncOrderCount
|
||||
}}</strong>
|
||||
条订单将更新成本
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</SyncModal>
|
||||
<ProductCostSyncToOrdersModal ref="productCostSyncModalRef" />
|
||||
</template>
|
||||
<style scoped lang="scss">
|
||||
.management-card {
|
||||
@@ -1510,6 +1336,23 @@ const saveEdit = async () => {
|
||||
color: #047857 !important;
|
||||
}
|
||||
|
||||
.footer-icon-btn--sync {
|
||||
background: #fff !important;
|
||||
border: 1px solid #bae6fd !important;
|
||||
color: #0284c7 !important;
|
||||
}
|
||||
|
||||
.footer-icon-btn--sync:hover:not(:disabled) {
|
||||
background: #f0f9ff !important;
|
||||
border-color: #7dd3fc !important;
|
||||
color: #0369a1 !important;
|
||||
}
|
||||
|
||||
.footer-icon-btn--sync:disabled {
|
||||
opacity: 0.45;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.footer-icon-btn--cancel {
|
||||
background: #fff !important;
|
||||
border: 1px solid #e2e8f0 !important;
|
||||
@@ -1534,137 +1377,6 @@ const saveEdit = async () => {
|
||||
color: #dc2626 !important;
|
||||
}
|
||||
|
||||
/* 成本同步弹窗 */
|
||||
.sync-alert {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
background: #fff8f1;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #ffe8d5;
|
||||
margin-bottom: 24px;
|
||||
color: #af6a2b;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.spec-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.spec-item-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 16px;
|
||||
border: 2px solid #eef0f2;
|
||||
border-radius: 10px;
|
||||
transition: all 0.2s;
|
||||
cursor: pointer;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.spec-item-card:hover {
|
||||
border-color: #d1d9ff;
|
||||
background: #fafbff;
|
||||
}
|
||||
|
||||
.spec-item-card.active {
|
||||
border-color: #5b6df5;
|
||||
background: #f0f2ff;
|
||||
}
|
||||
|
||||
.checkbox-wrapper {
|
||||
margin-right: 16px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.custom-checkbox {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 2px solid #cbd5e1;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #fff;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.active .custom-checkbox {
|
||||
background: #5b6df5;
|
||||
border-color: #5b6df5;
|
||||
}
|
||||
|
||||
.check-icon {
|
||||
color: #fff;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.spec-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.spec-title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #2d3436;
|
||||
margin-bottom: 4px;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 1;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.spec-code {
|
||||
font-size: 12px;
|
||||
color: #636e72;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.cost-snapshot {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
padding-left: 20px;
|
||||
border-left: 1px solid #e2e8f0;
|
||||
margin-left: 15px;
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.cost-tag {
|
||||
font-size: 12px;
|
||||
color: #636e72;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.cost-tag span:last-child {
|
||||
font-weight: 600;
|
||||
color: #2d3436;
|
||||
}
|
||||
|
||||
.cost-tag.changed span:last-child {
|
||||
color: #f2994a;
|
||||
}
|
||||
|
||||
.time-range-section {
|
||||
margin-top: 24px;
|
||||
}
|
||||
|
||||
.section-label {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #2d3436;
|
||||
margin-bottom: 12px;
|
||||
display: block;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
<!-- 深色:无 scoped,避免 Ant Card 根节点吃不到样式;颜色与 @vben design-tokens/dark.css 的 CSS 变量一致 -->
|
||||
@@ -1819,6 +1531,19 @@ html.dark {
|
||||
@include outline-btn(hsl(var(--success)), hsl(var(--success)), $accent-bg, hsl(var(--success-foreground)));
|
||||
}
|
||||
|
||||
.footer-icon-btn--sync {
|
||||
@include outline-btn(
|
||||
hsl(199deg 89% 48%),
|
||||
hsl(199deg 89% 58%),
|
||||
hsl(199deg 89% 48% / 12%),
|
||||
hsl(199deg 89% 72%)
|
||||
);
|
||||
}
|
||||
|
||||
.footer-icon-btn--sync:disabled {
|
||||
opacity: 0.45;
|
||||
}
|
||||
|
||||
.footer-icon-btn--cancel {
|
||||
@include outline-btn($border-base, $muted-text, $secondary-bg);
|
||||
}
|
||||
@@ -1946,51 +1671,5 @@ html.dark {
|
||||
}
|
||||
}
|
||||
|
||||
/* 成本同步弹窗(Teleport 到 body,不在 .management-card 内) */
|
||||
.product-cost-sync {
|
||||
.section-label,
|
||||
.spec-code,
|
||||
.cost-tag {
|
||||
color: $muted-text;
|
||||
}
|
||||
|
||||
.sync-alert {
|
||||
background: hsl(var(--warning) / 12%);
|
||||
border-color: hsl(var(--warning) / 35%);
|
||||
color: hsl(var(--warning));
|
||||
}
|
||||
|
||||
.spec-item-card {
|
||||
border-color: $border-base;
|
||||
background: hsl(var(--muted));
|
||||
|
||||
&:hover {
|
||||
border-color: hsl(var(--primary) / 50%);
|
||||
background: $accent-bg;
|
||||
}
|
||||
|
||||
&.active {
|
||||
border-color: $primary;
|
||||
background: hsl(var(--primary) / 10%);
|
||||
}
|
||||
}
|
||||
|
||||
.custom-checkbox {
|
||||
background: hsl(var(--muted));
|
||||
border-color: $border-base;
|
||||
}
|
||||
|
||||
.spec-title {
|
||||
color: $card-text;
|
||||
}
|
||||
|
||||
.cost-snapshot {
|
||||
border-left-color: $border-base;
|
||||
}
|
||||
|
||||
.cost-tag span:last-child {
|
||||
color: $card-text;
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
||||
+460
@@ -0,0 +1,460 @@
|
||||
<script lang="ts" setup>
|
||||
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
|
||||
|
||||
import { onUnmounted, ref, watch } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
|
||||
import { DatePicker, message } from 'ant-design-vue';
|
||||
|
||||
import {
|
||||
batchSyncCostsToOrdersApi,
|
||||
countOrdersForCostSyncApi,
|
||||
getProductSpecsByIdsApi,
|
||||
} from '#/api/ecommerce/product-spec/apis';
|
||||
|
||||
defineOptions({ name: 'ProductCostSyncToOrdersModal' });
|
||||
|
||||
type ProductCostSyncModalEntry = 'after-save' | 'manual';
|
||||
|
||||
interface ProductCostSyncOpenOptions {
|
||||
/** 保存后提示 / 用户主动同步 */
|
||||
entry?: ProductCostSyncModalEntry;
|
||||
/** 打开时预填时间范围;不传则清空由用户选择 */
|
||||
initialDateRange?: [string, string];
|
||||
}
|
||||
|
||||
const [SyncModal, syncModalApi] = useVbenModal({
|
||||
title: '成本变更同步确认',
|
||||
class: 'w-[720px] max-w-[calc(100vw-2rem)]',
|
||||
draggable: true,
|
||||
contentClass: 'px-5 py-4 min-h-[480px]',
|
||||
cancelText: '暂不同步',
|
||||
confirmText: '立即同步',
|
||||
onConfirm: handleSyncConfirm,
|
||||
onCancel: () => {
|
||||
syncModalApi.close();
|
||||
},
|
||||
});
|
||||
|
||||
const syncFormData = ref<[string, string] | undefined>(undefined);
|
||||
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
|
||||
const selectedSyncSpecIds = ref<number[]>([]);
|
||||
const costSyncModalEntry = ref<ProductCostSyncModalEntry>('after-save');
|
||||
|
||||
const costSyncOrderCount = ref<null | number>(null);
|
||||
const costSyncOrderCountLoading = ref(false);
|
||||
let costSyncCountTimer: null | ReturnType<typeof setTimeout> = null;
|
||||
|
||||
function scheduleFetchCostSyncOrderCount() {
|
||||
if (costSyncCountTimer) {
|
||||
clearTimeout(costSyncCountTimer);
|
||||
}
|
||||
costSyncCountTimer = setTimeout(() => {
|
||||
costSyncCountTimer = null;
|
||||
void fetchCostSyncOrderCount();
|
||||
}, 300);
|
||||
}
|
||||
|
||||
async function fetchCostSyncOrderCount() {
|
||||
const range = syncFormData.value;
|
||||
if (!range?.[0] || !range[1]) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
if (selectedSyncSpecIds.value.length === 0) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
costSyncOrderCountLoading.value = true;
|
||||
try {
|
||||
const [startTime, endTime] = range;
|
||||
costSyncOrderCount.value = await countOrdersForCostSyncApi({
|
||||
specIds: [...selectedSyncSpecIds.value],
|
||||
startTime,
|
||||
endTime,
|
||||
});
|
||||
} catch {
|
||||
costSyncOrderCount.value = null;
|
||||
} finally {
|
||||
costSyncOrderCountLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [...selectedSyncSpecIds.value],
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
syncFormData,
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
|
||||
/**
|
||||
* 打开成本同步弹窗(先拉取规格详情再展示)
|
||||
*/
|
||||
async function open(specIds: number[], options?: ProductCostSyncOpenOptions) {
|
||||
const entry = options?.entry ?? 'after-save';
|
||||
costSyncModalEntry.value = entry;
|
||||
syncModalApi.setState?.({
|
||||
title: entry === 'manual' ? '同步成本到订单' : '成本变更同步确认',
|
||||
});
|
||||
try {
|
||||
const specs = await getProductSpecsByIdsApi(specIds);
|
||||
pendingCostChangedSpecs.value = specs ?? [];
|
||||
selectedSyncSpecIds.value = [...specIds];
|
||||
syncFormData.value = options?.initialDateRange
|
||||
? [options.initialDateRange[0], options.initialDateRange[1]]
|
||||
: undefined;
|
||||
costSyncOrderCount.value = null;
|
||||
syncModalApi.open();
|
||||
if (options?.initialDateRange) {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取规格详情失败:', error);
|
||||
message.error('获取规格详情失败,无法进行成本同步');
|
||||
}
|
||||
}
|
||||
|
||||
function toggleSpec(specId: number) {
|
||||
const idx = selectedSyncSpecIds.value.indexOf(specId);
|
||||
if (idx === -1) {
|
||||
selectedSyncSpecIds.value.push(specId);
|
||||
} else {
|
||||
selectedSyncSpecIds.value.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSyncConfirm() {
|
||||
if (selectedSyncSpecIds.value.length === 0) {
|
||||
message.warning('请至少选择一个规格');
|
||||
return;
|
||||
}
|
||||
const range = syncFormData.value;
|
||||
if (!range?.[0] || !range[1]) {
|
||||
message.warning('请选择同步时间范围');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
syncModalApi.setState?.({ confirmLoading: true });
|
||||
const [startDate, endDate] = range;
|
||||
const totalUpdated = await batchSyncCostsToOrdersApi({
|
||||
specIds: selectedSyncSpecIds.value,
|
||||
startTime: startDate,
|
||||
endTime: endDate,
|
||||
});
|
||||
message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`);
|
||||
syncModalApi.close?.();
|
||||
} catch (error) {
|
||||
console.error('成本同步失败:', error);
|
||||
message.error('成本同步失败');
|
||||
} finally {
|
||||
syncModalApi.setState?.({ confirmLoading: false });
|
||||
}
|
||||
}
|
||||
|
||||
onUnmounted(() => {
|
||||
if (costSyncCountTimer) {
|
||||
clearTimeout(costSyncCountTimer);
|
||||
costSyncCountTimer = null;
|
||||
}
|
||||
});
|
||||
|
||||
defineExpose({ open });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<SyncModal>
|
||||
<div class="product-cost-sync py-4">
|
||||
<div class="sync-alert">
|
||||
<svg
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
>
|
||||
<circle cx="12" cy="12" r="10" />
|
||||
<line x1="12" y1="8" x2="12" y2="12" />
|
||||
<line x1="12" y1="16" x2="12.01" y2="16" />
|
||||
</svg>
|
||||
<template v-if="costSyncModalEntry === 'after-save'">
|
||||
检测到
|
||||
<strong>{{ pendingCostChangedSpecs.length }}</strong>
|
||||
个规格的成本已更新,请勾选需要同步的规格:
|
||||
</template>
|
||||
<template v-else>
|
||||
将所选规格<strong>当前</strong>的商品成本、运费、其他成本写入订单(会覆盖该时间范围内订单上的对应成本字段)。请勾选需要同步的规格,共
|
||||
<strong>{{ pendingCostChangedSpecs.length }}</strong> 个:
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="spec-list mb-4 max-h-72 min-h-[200px] overflow-y-auto rounded-lg"
|
||||
>
|
||||
<div
|
||||
v-for="spec in pendingCostChangedSpecs"
|
||||
:key="spec.id"
|
||||
class="spec-item-card"
|
||||
:class="{ active: selectedSyncSpecIds.includes(spec.id) }"
|
||||
@click="toggleSpec(spec.id)"
|
||||
>
|
||||
<div class="checkbox-wrapper">
|
||||
<div class="custom-checkbox">
|
||||
<span
|
||||
v-if="selectedSyncSpecIds.includes(spec.id)"
|
||||
class="check-icon"
|
||||
>
|
||||
✓
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="spec-info">
|
||||
<div class="spec-title">{{ spec.name }}</div>
|
||||
<div class="spec-code">{{ spec.specCode || '无编码' }}</div>
|
||||
</div>
|
||||
<div class="cost-snapshot">
|
||||
<div class="cost-tag changed">
|
||||
<span>商品成本</span>
|
||||
<span>¥{{ Number(spec.productCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="cost-tag">
|
||||
<span>运费</span>
|
||||
<span>¥{{ Number(spec.shippingCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
<div class="cost-tag">
|
||||
<span>其他</span>
|
||||
<span>¥{{ Number(spec.otherCost || 0).toFixed(2) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="time-range-section">
|
||||
<label class="section-label">同步时间范围</label>
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="syncFormData"
|
||||
class="w-full"
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
show-time
|
||||
/>
|
||||
<div
|
||||
class="sync-order-count mt-2 text-sm text-gray-600 dark:text-[hsl(240deg_5%_64.9%)]"
|
||||
>
|
||||
<template v-if="costSyncOrderCountLoading">正在统计订单数…</template>
|
||||
<template
|
||||
v-else-if="
|
||||
costSyncOrderCount !== null &&
|
||||
syncFormData &&
|
||||
syncFormData[0] &&
|
||||
syncFormData[1] &&
|
||||
selectedSyncSpecIds.length > 0
|
||||
"
|
||||
>
|
||||
当前时间范围内共
|
||||
<strong class="text-gray-900 dark:text-[hsl(0deg_0%_95%)]">{{
|
||||
costSyncOrderCount
|
||||
}}</strong>
|
||||
条订单将更新成本
|
||||
</template>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</SyncModal>
|
||||
</template>
|
||||
|
||||
<style scoped lang="scss">
|
||||
.sync-alert {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
background: #fff8f1;
|
||||
padding: 12px 16px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #ffe8d5;
|
||||
margin-bottom: 24px;
|
||||
color: #af6a2b;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.spec-list {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.spec-item-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
padding: 16px;
|
||||
border: 2px solid #eef0f2;
|
||||
border-radius: 10px;
|
||||
transition: all 0.2s;
|
||||
cursor: pointer;
|
||||
background: #fff;
|
||||
}
|
||||
|
||||
.spec-item-card:hover {
|
||||
border-color: #d1d9ff;
|
||||
background: #fafbff;
|
||||
}
|
||||
|
||||
.spec-item-card.active {
|
||||
border-color: #5b6df5;
|
||||
background: #f0f2ff;
|
||||
}
|
||||
|
||||
.checkbox-wrapper {
|
||||
margin-right: 16px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.custom-checkbox {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 2px solid #cbd5e1;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #fff;
|
||||
transition: all 0.2s;
|
||||
}
|
||||
|
||||
.active .custom-checkbox {
|
||||
background: #5b6df5;
|
||||
border-color: #5b6df5;
|
||||
}
|
||||
|
||||
.check-icon {
|
||||
color: #fff;
|
||||
font-size: 14px;
|
||||
font-weight: bold;
|
||||
line-height: 1;
|
||||
}
|
||||
|
||||
.spec-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.spec-title {
|
||||
font-size: 14px;
|
||||
font-weight: 500;
|
||||
color: #2d3436;
|
||||
margin-bottom: 4px;
|
||||
display: -webkit-box;
|
||||
-webkit-line-clamp: 1;
|
||||
line-clamp: 1;
|
||||
-webkit-box-orient: vertical;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.spec-code {
|
||||
font-size: 12px;
|
||||
color: #636e72;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
.cost-snapshot {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
padding-left: 20px;
|
||||
border-left: 1px solid #e2e8f0;
|
||||
margin-left: 15px;
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.cost-tag {
|
||||
font-size: 12px;
|
||||
color: #636e72;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.cost-tag span:last-child {
|
||||
font-weight: 600;
|
||||
color: #2d3436;
|
||||
}
|
||||
|
||||
.cost-tag.changed span:last-child {
|
||||
color: #f2994a;
|
||||
}
|
||||
|
||||
.time-range-section {
|
||||
margin-top: 24px;
|
||||
}
|
||||
|
||||
.section-label {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #2d3436;
|
||||
margin-bottom: 12px;
|
||||
display: block;
|
||||
}
|
||||
</style>
|
||||
|
||||
<style lang="scss">
|
||||
$card-text: hsl(var(--foreground));
|
||||
$muted-text: hsl(var(--muted-foreground));
|
||||
$accent-bg: hsl(var(--accent));
|
||||
$border-base: hsl(var(--border));
|
||||
$primary: hsl(var(--primary));
|
||||
|
||||
html.dark .product-cost-sync {
|
||||
.section-label,
|
||||
.spec-code,
|
||||
.cost-tag {
|
||||
color: $muted-text;
|
||||
}
|
||||
|
||||
.sync-alert {
|
||||
background: hsl(var(--warning) / 12%);
|
||||
border-color: hsl(var(--warning) / 35%);
|
||||
color: hsl(var(--warning));
|
||||
}
|
||||
|
||||
.spec-item-card {
|
||||
border-color: $border-base;
|
||||
background: hsl(var(--muted));
|
||||
|
||||
&:hover {
|
||||
border-color: hsl(var(--primary) / 50%);
|
||||
background: $accent-bg;
|
||||
}
|
||||
|
||||
&.active {
|
||||
border-color: $primary;
|
||||
background: hsl(var(--primary) / 10%);
|
||||
}
|
||||
}
|
||||
|
||||
.custom-checkbox {
|
||||
background: hsl(var(--muted));
|
||||
border-color: $border-base;
|
||||
}
|
||||
|
||||
.spec-title {
|
||||
color: $card-text;
|
||||
}
|
||||
|
||||
.cost-snapshot {
|
||||
border-left-color: $border-base;
|
||||
}
|
||||
|
||||
.cost-tag span:last-child {
|
||||
color: $card-text;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
+15
-177
@@ -2,14 +2,13 @@
|
||||
import type { ProductDTO, ProductVO } from '#/api/ecommerce/product/types';
|
||||
|
||||
import type { DropdownRender } from 'ant-design-vue/es/vc-select/BaseSelect';
|
||||
import { computed, h, onUnmounted, ref, watch } from 'vue';
|
||||
import { computed, h, onUnmounted, ref } from 'vue';
|
||||
|
||||
import { useVbenModal } from '@vben/common-ui';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
Button,
|
||||
DatePicker,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
@@ -20,16 +19,11 @@ import {
|
||||
} from 'ant-design-vue';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
import { createProductApi, updateProductApi } from '#/api/ecommerce/product';
|
||||
import {
|
||||
batchSyncCostsToOrdersApi,
|
||||
countOrdersForCostSyncApi,
|
||||
getProductSpecsByIdsApi,
|
||||
} from '#/api/ecommerce/product-spec/apis';
|
||||
import type { ProductSpecVO } from '#/api/ecommerce/product-spec/types';
|
||||
import { getCategoryTreeApi } from '#/api/ecommerce/category';
|
||||
import { createProductApi, updateProductApi } from '#/api/ecommerce/product';
|
||||
import { uploadFileToOss } from '#/utils/oss-upload';
|
||||
import CategoryFormModal from '#/views/ecommerce/config/category-config/components/CategoryFormModal.vue';
|
||||
import ProductCostSyncToOrdersModal from '#/views/ecommerce/product/components/ProductCostSyncToOrdersModal.vue';
|
||||
|
||||
interface Emits {
|
||||
(e: 'success'): void;
|
||||
@@ -63,117 +57,18 @@ const DownIcon = createIconifyIcon('lucide:chevron-down');
|
||||
const PlusIcon = createIconifyIcon('lucide:plus');
|
||||
const TrashIcon = createIconifyIcon('lucide:trash-2');
|
||||
|
||||
// 成本同步弹窗
|
||||
const [SyncModal, syncModalApi] = useVbenModal({
|
||||
title: '成本变更同步确认',
|
||||
class: 'w-[500px]',
|
||||
draggable: true,
|
||||
contentClass: 'px-5 py-4',
|
||||
cancelText: '暂不同步',
|
||||
confirmText: '立即同步',
|
||||
onConfirm: handleSyncConfirm,
|
||||
onCancel: () => {
|
||||
syncModalApi.close();
|
||||
},
|
||||
});
|
||||
const syncFormData = ref<[string, string] | undefined>(undefined);
|
||||
const pendingCostChangedSpecs = ref<ProductSpecVO.Info[]>([]);
|
||||
const selectedSyncSpecIds = ref<number[]>([]);
|
||||
|
||||
const costSyncOrderCount = ref<null | number>(null);
|
||||
const costSyncOrderCountLoading = ref(false);
|
||||
let costSyncCountTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
|
||||
function scheduleFetchCostSyncOrderCount() {
|
||||
if (costSyncCountTimer) {
|
||||
clearTimeout(costSyncCountTimer);
|
||||
}
|
||||
costSyncCountTimer = setTimeout(() => {
|
||||
costSyncCountTimer = null;
|
||||
void fetchCostSyncOrderCount();
|
||||
}, 300);
|
||||
}
|
||||
|
||||
async function fetchCostSyncOrderCount() {
|
||||
if (
|
||||
!syncFormData.value ||
|
||||
syncFormData.value[0] == null ||
|
||||
syncFormData.value[1] == null
|
||||
) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
if (selectedSyncSpecIds.value.length === 0) {
|
||||
costSyncOrderCount.value = null;
|
||||
return;
|
||||
}
|
||||
costSyncOrderCountLoading.value = true;
|
||||
try {
|
||||
const [startTime, endTime] = syncFormData.value;
|
||||
costSyncOrderCount.value = await countOrdersForCostSyncApi({
|
||||
specIds: [...selectedSyncSpecIds.value],
|
||||
startTime,
|
||||
endTime,
|
||||
});
|
||||
} catch {
|
||||
costSyncOrderCount.value = null;
|
||||
} finally {
|
||||
costSyncOrderCountLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [...selectedSyncSpecIds.value],
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
syncFormData,
|
||||
() => {
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
},
|
||||
{ deep: true },
|
||||
);
|
||||
const productCostSyncModalRef = ref<InstanceType<
|
||||
typeof ProductCostSyncToOrdersModal
|
||||
> | null>(null);
|
||||
|
||||
async function openSyncModal(costChangedSpecIds: number[]) {
|
||||
try {
|
||||
// 根据ID列表查询规格详情
|
||||
const specs = await getProductSpecsByIdsApi(costChangedSpecIds);
|
||||
pendingCostChangedSpecs.value = specs ?? [];
|
||||
selectedSyncSpecIds.value = costChangedSpecIds;
|
||||
syncFormData.value = [
|
||||
await productCostSyncModalRef.value?.open(costChangedSpecIds, {
|
||||
entry: 'after-save',
|
||||
initialDateRange: [
|
||||
dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||||
dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
|
||||
];
|
||||
costSyncOrderCount.value = null;
|
||||
syncModalApi.open();
|
||||
scheduleFetchCostSyncOrderCount();
|
||||
} catch (error) {
|
||||
console.error('获取规格详情失败:', error);
|
||||
message.error('获取规格详情失败,无法进行成本同步');
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSyncConfirm() {
|
||||
if (selectedSyncSpecIds.value.length === 0 || !syncFormData.value) return;
|
||||
try {
|
||||
syncModalApi.setState?.({ confirmLoading: true });
|
||||
const [startDate, endDate] = syncFormData.value;
|
||||
const totalUpdated = await batchSyncCostsToOrdersApi({
|
||||
specIds: selectedSyncSpecIds.value,
|
||||
startTime: startDate,
|
||||
endTime: endDate,
|
||||
});
|
||||
message.success(`成本同步完成,共更新 ${totalUpdated ?? 0} 条订单`);
|
||||
syncModalApi.close?.();
|
||||
} catch (error) {
|
||||
console.error('成本同步失败:', error);
|
||||
message.error('成本同步失败');
|
||||
} finally {
|
||||
syncModalApi.setState?.({ confirmLoading: false });
|
||||
}
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
// 内部状态管理
|
||||
@@ -253,9 +148,10 @@ const categoryTreeDropdownRender: DropdownRender = (opt) => {
|
||||
]);
|
||||
};
|
||||
|
||||
/** 表格行稳定 key(新增行无 id) */
|
||||
type SpecFormRow = (ProductDTO.Spec | ProductDTO.UpdateSpec) & {
|
||||
/** 表格行:创建规格无 id,编辑已有规格带 id;_clientKey 用于行 key */
|
||||
type SpecFormRow = ProductDTO.Spec & {
|
||||
_clientKey?: string;
|
||||
id?: number;
|
||||
};
|
||||
|
||||
// 规格数据 - 支持创建和更新
|
||||
@@ -917,65 +813,7 @@ onUnmounted(() => {
|
||||
</Form>
|
||||
</Modal>
|
||||
|
||||
<!-- 成本同步确认弹窗 -->
|
||||
<SyncModal>
|
||||
<div class="py-4">
|
||||
<p class="mb-4 text-gray-600">
|
||||
检测到 <strong>{{ pendingCostChangedSpecs.length }}</strong> 个规格的成本已更新,
|
||||
请勾选需要同步的规格:
|
||||
</p>
|
||||
<div class="mb-4 max-h-48 overflow-y-auto border border-gray-200 rounded">
|
||||
<div
|
||||
v-for="spec in pendingCostChangedSpecs"
|
||||
:key="spec.id"
|
||||
class="flex items-center gap-3 px-3 py-2 border-b border-gray-100 last:border-b-0"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
:value="spec.id"
|
||||
v-model="selectedSyncSpecIds"
|
||||
class="w-4 h-4 rounded border-gray-300 text-blue-500 focus:ring-blue-500"
|
||||
/>
|
||||
<div class="flex-1 min-w-0">
|
||||
<div class="text-sm font-medium text-gray-800 truncate">{{ spec.name }}</div>
|
||||
<div class="text-xs text-gray-400">{{ spec.specCode || '无编码' }}</div>
|
||||
</div>
|
||||
<div class="text-xs text-gray-500 text-right shrink-0">
|
||||
<div>商品成本: <span class="text-orange-500 font-medium">{{ spec.productCost }}</span></div>
|
||||
<div v-if="spec.shippingCost">运费: {{ spec.shippingCost }}</div>
|
||||
<div v-if="spec.otherCost">其他: {{ spec.otherCost }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Form layout="vertical" class="mt-4">
|
||||
<Form.Item label="同步时间范围">
|
||||
<DatePicker.RangePicker
|
||||
v-model:value="syncFormData"
|
||||
class="w-full"
|
||||
format="YYYY-MM-DD HH:mm:ss"
|
||||
value-format="YYYY-MM-DD HH:mm:ss"
|
||||
show-time
|
||||
/>
|
||||
<div class="mt-2 text-sm text-gray-500">
|
||||
<template v-if="costSyncOrderCountLoading">正在统计订单数…</template>
|
||||
<template
|
||||
v-else-if="
|
||||
costSyncOrderCount !== null &&
|
||||
syncFormData &&
|
||||
syncFormData[0] &&
|
||||
syncFormData[1] &&
|
||||
selectedSyncSpecIds.length > 0
|
||||
"
|
||||
>
|
||||
当前时间范围内共
|
||||
<strong class="text-gray-800">{{ costSyncOrderCount }}</strong>
|
||||
条订单将更新成本
|
||||
</template>
|
||||
</div>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</div>
|
||||
</SyncModal>
|
||||
<ProductCostSyncToOrdersModal ref="productCostSyncModalRef" />
|
||||
|
||||
<CategoryFormModal ref="categoryFormModalRef" @success="reloadCategoryTree" />
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user