mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 04:29:21 +08:00
feat: SKU 列表支持批量设置单买价/拼单价/库存
This commit is contained in:
@@ -379,6 +379,7 @@ const {
|
||||
bindInternalSku,
|
||||
applyInternalSkusInOrder,
|
||||
clearInternalSkuBindings,
|
||||
applySkuBatchFields,
|
||||
} = sku;
|
||||
|
||||
const {
|
||||
@@ -798,6 +799,7 @@ function onSkuTableWrapRef(el: HTMLElement | null) {
|
||||
@out-sku-sn-blur="onOutSkuSnBlur"
|
||||
@bind-internal-sku="bindInternalSku"
|
||||
@sku-thumb-file-list-change="onSkuThumbFileListChange"
|
||||
@apply-batch="applySkuBatchFields"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+285
@@ -2,7 +2,10 @@
|
||||
import type { UploadFile, UploadProps } from 'ant-design-vue';
|
||||
import type { SkuRow } from '../types';
|
||||
|
||||
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue';
|
||||
|
||||
import { PlusOutlined } from '@ant-design/icons-vue';
|
||||
import { createIconifyIcon } from '@vben/icons';
|
||||
|
||||
import {
|
||||
Alert,
|
||||
@@ -14,6 +17,7 @@ import {
|
||||
Select,
|
||||
Table,
|
||||
Upload,
|
||||
message,
|
||||
} from 'ant-design-vue';
|
||||
|
||||
defineOptions({ name: 'SkuTableCard' });
|
||||
@@ -47,11 +51,144 @@ const emit = defineEmits<{
|
||||
'out-sku-sn-blur': [rowKey: number];
|
||||
'bind-internal-sku': [rowKey: number, skuId?: number];
|
||||
'sku-thumb-file-list-change': [rowKey: number, list: UploadFile[]];
|
||||
'apply-batch': [
|
||||
patch: {
|
||||
priceYuan?: number;
|
||||
multiPriceYuan?: number;
|
||||
quantity?: number;
|
||||
},
|
||||
];
|
||||
}>();
|
||||
|
||||
const BatchIcon = createIconifyIcon('lucide:layers-2');
|
||||
|
||||
/** 列头批量:Teleport 浮层,避免被 table header overflow 裁切 */
|
||||
const colBatchPrice = ref<number | undefined>(undefined);
|
||||
const colBatchMulti = ref<number | undefined>(undefined);
|
||||
const colBatchQty = ref<number | undefined>(undefined);
|
||||
const colBatchOpen = ref<'price' | 'multi' | 'qty' | null>(null);
|
||||
const batchAnchorEl = ref<HTMLElement | null>(null);
|
||||
const batchPanelStyle = ref<Record<string, string>>({
|
||||
top: '0px',
|
||||
left: '0px',
|
||||
});
|
||||
/** 本次打开只自动聚焦一次,避免输入时被再次 select 覆盖 */
|
||||
let batchFocusedOnce = false;
|
||||
|
||||
function setTableWrapRef(el: unknown) {
|
||||
emit('set-table-wrap-ref', (el as HTMLElement | null) ?? null);
|
||||
}
|
||||
|
||||
function placeBatchPanel() {
|
||||
const anchor = batchAnchorEl.value;
|
||||
if (!anchor) return;
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
const panelWidth = 176;
|
||||
const left = Math.min(
|
||||
Math.max(8, rect.left),
|
||||
window.innerWidth - panelWidth - 8,
|
||||
);
|
||||
batchPanelStyle.value = {
|
||||
top: `${rect.bottom + 6}px`,
|
||||
left: `${left}px`,
|
||||
};
|
||||
}
|
||||
|
||||
function toggleColBatch(field: 'price' | 'multi' | 'qty', event: MouseEvent) {
|
||||
const anchor = event.currentTarget as HTMLElement;
|
||||
if (colBatchOpen.value === field) {
|
||||
closeBatchPanel();
|
||||
return;
|
||||
}
|
||||
batchFocusedOnce = false;
|
||||
batchAnchorEl.value = anchor;
|
||||
colBatchOpen.value = field;
|
||||
placeBatchPanel();
|
||||
void nextTick(placeBatchPanel);
|
||||
}
|
||||
|
||||
/** InputNumber 挂载后立刻聚焦;有旧值才全选,避免输入过程中被再次 select */
|
||||
function onBatchInputMounted(el: unknown) {
|
||||
if (!el) return;
|
||||
if (batchFocusedOnce) return;
|
||||
batchFocusedOnce = true;
|
||||
void nextTick(() => {
|
||||
const inst = el as { focus?: () => void; $el?: HTMLElement };
|
||||
inst.focus?.();
|
||||
const root = inst.$el instanceof HTMLElement ? inst.$el : null;
|
||||
const input = root?.querySelector<HTMLInputElement>(
|
||||
'input:not([type="hidden"])',
|
||||
);
|
||||
if (!input) return;
|
||||
input.focus();
|
||||
// 仅已有内容时全选,方便覆盖;空输入不要 select,否则易干扰后续按键
|
||||
if (input.value) input.select();
|
||||
});
|
||||
}
|
||||
|
||||
function closeBatchPanel() {
|
||||
colBatchOpen.value = null;
|
||||
batchAnchorEl.value = null;
|
||||
batchFocusedOnce = false;
|
||||
}
|
||||
|
||||
function onDocPointerDown(event: PointerEvent) {
|
||||
if (!colBatchOpen.value) return;
|
||||
const target = event.target;
|
||||
if (target instanceof Node) {
|
||||
const el = target as HTMLElement;
|
||||
if (el.closest?.('.sku-batch-header') || el.closest?.('.sku-batch-panel')) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
closeBatchPanel();
|
||||
}
|
||||
|
||||
function onWinReposition() {
|
||||
if (colBatchOpen.value) placeBatchPanel();
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('pointerdown', onDocPointerDown, true);
|
||||
window.addEventListener('resize', onWinReposition);
|
||||
window.addEventListener('scroll', onWinReposition, true);
|
||||
});
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('pointerdown', onDocPointerDown, true);
|
||||
window.removeEventListener('resize', onWinReposition);
|
||||
window.removeEventListener('scroll', onWinReposition, true);
|
||||
});
|
||||
|
||||
function applyColumnBatch(field: 'price' | 'multi' | 'qty') {
|
||||
const value =
|
||||
field === 'price'
|
||||
? colBatchPrice.value
|
||||
: field === 'multi'
|
||||
? colBatchMulti.value
|
||||
: colBatchQty.value;
|
||||
if (value == null) {
|
||||
message.warning('请先填写要设置的值');
|
||||
return;
|
||||
}
|
||||
const patch =
|
||||
field === 'price'
|
||||
? { priceYuan: value }
|
||||
: field === 'multi'
|
||||
? { multiPriceYuan: value }
|
||||
: { quantity: value };
|
||||
emit('apply-batch', patch);
|
||||
closeBatchPanel();
|
||||
}
|
||||
|
||||
function onColumnBatchEnter(
|
||||
field: 'price' | 'multi' | 'qty',
|
||||
event: KeyboardEvent,
|
||||
) {
|
||||
if (event.isComposing || event.keyCode === 229) return;
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
applyColumnBatch(field);
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@@ -106,6 +243,55 @@ function setTableWrapRef(el: unknown) {
|
||||
:locale="{ emptyText: '暂无 SKU 组合' }"
|
||||
:columns="skuTableColumns"
|
||||
>
|
||||
<template #headerCell="{ column }">
|
||||
<template v-if="column.key === 'price'">
|
||||
<div class="sku-batch-header">
|
||||
<span>单买价</span>
|
||||
<button
|
||||
type="button"
|
||||
class="sku-batch-icon-btn"
|
||||
title="批量设置单买价"
|
||||
aria-label="批量设置单买价"
|
||||
@mousedown.prevent
|
||||
@click="toggleColBatch('price', $event)"
|
||||
>
|
||||
<BatchIcon class="sku-batch-icon" />
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'multi'">
|
||||
<div class="sku-batch-header">
|
||||
<span>拼单价</span>
|
||||
<button
|
||||
type="button"
|
||||
class="sku-batch-icon-btn"
|
||||
title="批量设置拼单价"
|
||||
aria-label="批量设置拼单价"
|
||||
@mousedown.prevent
|
||||
@click="toggleColBatch('multi', $event)"
|
||||
>
|
||||
<BatchIcon class="sku-batch-icon" />
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'qty'">
|
||||
<div class="sku-batch-header">
|
||||
<span>库存</span>
|
||||
<button
|
||||
type="button"
|
||||
class="sku-batch-icon-btn"
|
||||
title="批量设置库存"
|
||||
aria-label="批量设置库存"
|
||||
@mousedown.prevent
|
||||
@click="toggleColBatch('qty', $event)"
|
||||
>
|
||||
<BatchIcon class="sku-batch-icon" />
|
||||
</button>
|
||||
</div>
|
||||
</template>
|
||||
<template v-else>{{ column.title }}</template>
|
||||
</template>
|
||||
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="String(column.key || '').startsWith('spec_')">
|
||||
<AutoComplete
|
||||
@@ -259,9 +445,95 @@ function setTableWrapRef(el: unknown) {
|
||||
</Table>
|
||||
</div>
|
||||
</Card>
|
||||
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="colBatchOpen"
|
||||
class="sku-batch-panel"
|
||||
:style="batchPanelStyle"
|
||||
@mousedown.stop
|
||||
>
|
||||
<InputNumber
|
||||
v-if="colBatchOpen === 'price'"
|
||||
:ref="onBatchInputMounted"
|
||||
:value="colBatchPrice"
|
||||
:min="0.01"
|
||||
:precision="2"
|
||||
placeholder="统一单买价,回车应用"
|
||||
class="w-40"
|
||||
@update:value="
|
||||
(v) => (colBatchPrice = v == null ? undefined : Number(v))
|
||||
"
|
||||
@keydown.enter="(e: KeyboardEvent) => onColumnBatchEnter('price', e)"
|
||||
/>
|
||||
<InputNumber
|
||||
v-else-if="colBatchOpen === 'multi'"
|
||||
:ref="onBatchInputMounted"
|
||||
:value="colBatchMulti"
|
||||
:min="0.01"
|
||||
:precision="2"
|
||||
placeholder="统一拼单价,回车应用"
|
||||
class="w-40"
|
||||
@update:value="
|
||||
(v) => (colBatchMulti = v == null ? undefined : Number(v))
|
||||
"
|
||||
@keydown.enter="(e: KeyboardEvent) => onColumnBatchEnter('multi', e)"
|
||||
/>
|
||||
<InputNumber
|
||||
v-else
|
||||
:ref="onBatchInputMounted"
|
||||
:value="colBatchQty"
|
||||
:min="0"
|
||||
placeholder="统一库存,回车应用"
|
||||
class="w-40"
|
||||
@update:value="(v) => (colBatchQty = v == null ? undefined : Number(v))"
|
||||
@keydown.enter="(e: KeyboardEvent) => onColumnBatchEnter('qty', e)"
|
||||
/>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.sku-batch-header {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 2px;
|
||||
}
|
||||
|
||||
.sku-batch-icon-btn {
|
||||
display: inline-flex;
|
||||
flex-shrink: 0;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
line-height: 1;
|
||||
color: hsl(var(--muted-foreground));
|
||||
cursor: pointer;
|
||||
background: transparent;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
outline: none;
|
||||
transition: color 0.15s;
|
||||
}
|
||||
|
||||
.sku-batch-icon-btn:hover {
|
||||
color: hsl(var(--primary));
|
||||
background: transparent;
|
||||
}
|
||||
|
||||
.sku-batch-icon-btn:focus-visible {
|
||||
color: hsl(var(--primary));
|
||||
}
|
||||
|
||||
.sku-batch-icon {
|
||||
display: block;
|
||||
width: 14px;
|
||||
height: 14px;
|
||||
}
|
||||
|
||||
/* SKU 列内缩略图:更小的 picture-card,悬停仍显示预览/删除 */
|
||||
.sku-thumb-upload :deep(.ant-upload-select),
|
||||
.sku-thumb-upload :deep(.ant-upload-list-item-container) {
|
||||
@@ -275,3 +547,16 @@ function setTableWrapRef(el: unknown) {
|
||||
padding: 2px;
|
||||
}
|
||||
</style>
|
||||
|
||||
<!-- Teleport 到 body,需非 scoped 样式 -->
|
||||
<style>
|
||||
.sku-batch-panel {
|
||||
position: fixed;
|
||||
z-index: 1100;
|
||||
padding: 8px;
|
||||
background: hsl(var(--popover, var(--background, 0 0% 100%)));
|
||||
border: 1px solid hsl(var(--border, 0 0% 90%));
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 8px 24px rgb(0 0 0 / 12%);
|
||||
}
|
||||
</style>
|
||||
|
||||
+42
@@ -636,6 +636,47 @@ export function usePublishSku(options: {
|
||||
message.success('已清空内部 SKU 关联');
|
||||
}
|
||||
|
||||
/** 批量改价/库存:未传字段不覆盖;默认改全部行。 */
|
||||
function applySkuBatchFields(patch: {
|
||||
priceYuan?: number;
|
||||
multiPriceYuan?: number;
|
||||
quantity?: number;
|
||||
}) {
|
||||
const targets = skuRows.value;
|
||||
if (targets.length === 0) {
|
||||
message.warning('暂无 SKU 可更新');
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasPrice = patch.priceYuan != null;
|
||||
const hasMulti = patch.multiPriceYuan != null;
|
||||
const hasQty = patch.quantity != null;
|
||||
if (!hasPrice && !hasMulti && !hasQty) {
|
||||
message.warning('请至少填写一项要批量设置的值');
|
||||
return false;
|
||||
}
|
||||
if (hasPrice && (patch.priceYuan as number) < 0.01) {
|
||||
message.warning('单买价不能小于 0.01');
|
||||
return false;
|
||||
}
|
||||
if (hasMulti && (patch.multiPriceYuan as number) < 0.01) {
|
||||
message.warning('拼单价不能小于 0.01');
|
||||
return false;
|
||||
}
|
||||
if (hasQty && (patch.quantity as number) < 0) {
|
||||
message.warning('库存不能为负数');
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const row of targets) {
|
||||
if (hasPrice) row.priceYuan = patch.priceYuan;
|
||||
if (hasMulti) row.multiPriceYuan = patch.multiPriceYuan;
|
||||
if (hasQty) row.quantity = patch.quantity as number;
|
||||
}
|
||||
message.success(`已更新 ${targets.length} 条 SKU`);
|
||||
return true;
|
||||
}
|
||||
|
||||
/** 新增规格后滚到表体底部,方便继续编辑新行 */
|
||||
async function scrollSkuTableToBottom() {
|
||||
await nextTick();
|
||||
@@ -689,6 +730,7 @@ export function usePublishSku(options: {
|
||||
onOutSkuSnBlur,
|
||||
applyInternalSkusInOrder,
|
||||
clearInternalSkuBindings,
|
||||
applySkuBatchFields,
|
||||
scrollSkuTableToBottom,
|
||||
resetSkuRowSeq,
|
||||
bumpSkuRowSeqFromKeys,
|
||||
|
||||
Reference in New Issue
Block a user