feat: 快递报价编辑页分区导航支持拖拽排序

This commit is contained in:
2026-10-28 16:07:48 +08:00
parent 7781f53d8a
commit abe75f87aa
3 changed files with 206 additions and 55 deletions
@@ -1,11 +1,16 @@
<script lang="ts" setup>
/**
* 分区×重量带编辑:动态嵌套用 antd Card/Table;左侧 Anchor 快速跳转分区。
* 分区×重量带编辑:左侧分区导航(可选进入拖拽排序)+ 右侧分区卡片。
* 排序落库用数组下标 → sortOrder(表字段已有)。
*/
import { computed, nextTick, ref } from 'vue';
import type { Sortable } from '@vben/hooks';
import { computed, nextTick, onBeforeUnmount, ref } from 'vue';
import { useSortable } from '@vben/hooks';
import { createIconifyIcon } from '@vben/icons';
import {
Anchor,
Button,
Card,
Input,
@@ -17,23 +22,10 @@ import {
message,
} from 'ant-design-vue';
export interface BandRow {
weightFromG: number;
weightToG: number | null;
priceMode: string;
flatAmount: number;
firstWeightG: number;
firstAmount: number;
continuePerKg: number;
}
import { createRegionUid, type BandRow, type RegionRow } from './region-band';
export interface RegionRow {
regionName: string;
zoneLabel: string;
provinces: string[];
isDefault: boolean;
bands: BandRow[];
}
/** 分区导航拖拽手柄 */
const GripVerticalIcon = createIconifyIcon('lucide:grip-vertical');
const props = defineProps<{
modelValue: RegionRow[];
@@ -47,24 +39,24 @@ const emit = defineEmits<{
defineOptions({ name: 'RegionBandEditor' });
const scrollRef = ref<HTMLElement | null>(null);
const navListRef = ref<HTMLElement | null>(null);
let navSortable: Sortable | null = null;
/** 是否处于拖拽排序模式 */
const sorting = ref(false);
/** 排序草稿,确定后写回 model;取消则丢弃 */
const draftRegions = ref<RegionRow[]>([]);
const regions = computed({
get: () => props.modelValue,
set: (v) => emit('update:modelValue', v),
});
const anchorItems = computed(() =>
regions.value.map((r, i) => ({
key: `region-${i}`,
href: `#quote-region-${i}`,
title: r.zoneLabel || r.regionName || `分区${i + 1}`,
})),
/** 导航列表数据源:排序中用草稿,否则用正式数据 */
const navRegions = computed(() =>
sorting.value ? draftRegions.value : regions.value,
);
function getScrollContainer() {
return scrollRef.value || window;
}
const bandColumnDefs = [
{ title: '从(克)', key: 'from', width: 110 },
{ title: '到(克)', key: 'to', width: 110 },
@@ -85,11 +77,87 @@ function emptyBand(fromG = 0): BandRow {
};
}
function scrollToRegion(uid: string) {
if (sorting.value) return;
document
.getElementById(`quote-region-${uid}`)
?.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
function destroyNavSortable() {
navSortable?.destroy();
navSortable = null;
}
/** 进入排序后再绑 Sortable,避免默认态误拖、也避免 ref 未挂载 */
async function initNavSortable() {
destroyNavSortable();
await nextTick();
const el = navListRef.value;
if (!el || draftRegions.value.length < 2) return;
const { initializeSortable } = useSortable(el, {
animation: 200,
delay: 0,
handle: '.region-nav-drag-handle',
draggable: '.region-nav-item',
onEnd(evt) {
const { oldIndex, newIndex } = evt;
if (
oldIndex == null ||
newIndex == null ||
oldIndex === newIndex ||
Number.isNaN(oldIndex) ||
Number.isNaN(newIndex)
) {
return;
}
const list = [...draftRegions.value];
const [moved] = list.splice(oldIndex, 1);
if (!moved) return;
list.splice(newIndex, 0, moved);
draftRegions.value = list;
// Vue 按 uid 重排 DOM 后需重绑,否则下次拖拽失效
void initNavSortable();
},
});
navSortable = await initializeSortable();
}
async function enterSortMode() {
if (regions.value.length < 2) {
message.warning('至少两个分区才能排序');
return;
}
draftRegions.value = regions.value.map((r) => ({ ...r }));
sorting.value = true;
await initNavSortable();
}
function confirmSort() {
regions.value = draftRegions.value.map((r) => ({ ...r }));
destroyNavSortable();
sorting.value = false;
draftRegions.value = [];
}
function cancelSort() {
destroyNavSortable();
sorting.value = false;
draftRegions.value = [];
}
onBeforeUnmount(() => {
destroyNavSortable();
});
function addRegion() {
if (sorting.value) return;
const uid = createRegionUid();
const nextIndex = regions.value.length;
regions.value = [
...regions.value,
{
uid,
regionName: `分区${nextIndex + 1}`,
zoneLabel: '',
provinces: [],
@@ -97,14 +165,11 @@ function addRegion() {
bands: [emptyBand(0)],
},
];
void nextTick(() => {
document
.getElementById(`quote-region-${nextIndex}`)
?.scrollIntoView({ behavior: 'smooth', block: 'start' });
});
void nextTick(() => scrollToRegion(uid));
}
function removeRegion(idx: number) {
if (sorting.value) return;
if (regions.value.length <= 1) {
message.warning('至少保留一个分区');
return;
@@ -149,10 +214,6 @@ function patchBand(ri: number, bi: number, patch: Partial<BandRow>) {
const bands = region.bands.map((b, i) => (i === bi ? { ...b, ...patch } : b));
patchRegion(ri, { bands });
}
function onAnchorClick(e: Event) {
e.preventDefault();
}
</script>
<template>
@@ -170,19 +231,72 @@ function onAnchorClick(e: Event) {
}"
>
<template #extra>
<Button size="small" type="dashed" @click="addRegion">添加分区</Button>
<Button
size="small"
type="dashed"
:disabled="sorting"
@click="addRegion"
>
添加分区
</Button>
</template>
<div class="flex min-h-0 flex-1 gap-3 overflow-hidden">
<aside
class="w-36 shrink-0 overflow-y-auto border-r border-border pr-2 pt-1"
class="w-44 shrink-0 overflow-y-auto border-r border-border pr-2 pt-1"
>
<div class="mb-2 text-xs text-muted-foreground">分区导航</div>
<Anchor
:affix="false"
:items="anchorItems"
:get-container="getScrollContainer"
@click="onAnchorClick"
/>
<div class="mb-2 flex items-center justify-between gap-1">
<span class="text-xs text-muted-foreground">分区导航</span>
<template v-if="sorting">
<Space :size="4">
<Button size="small" type="link" @click="cancelSort">取消</Button>
<Button size="small" type="link" @click="confirmSort">
确定
</Button>
</Space>
</template>
<Button
v-else
size="small"
type="link"
class="!px-0"
:disabled="regions.length < 2"
@click="enterSortMode"
>
调整顺序
</Button>
</div>
<div
v-if="sorting"
class="mb-1 text-xs text-muted-foreground"
>
拖动手柄调整,完成后点确定
</div>
<div ref="navListRef" class="flex flex-col gap-0.5">
<div
v-for="(region, ri) in navRegions"
:key="region.uid"
class="region-nav-item flex items-center gap-1 rounded px-1 py-1 text-sm hover:bg-muted/50"
:class="sorting ? 'bg-muted/30' : undefined"
>
<span
v-if="sorting"
class="region-nav-drag-handle shrink-0 cursor-grab text-muted-foreground active:cursor-grabbing"
title="拖动调整顺序"
@click.stop
>
<GripVerticalIcon class="size-3.5" />
</span>
<button
type="button"
class="min-w-0 flex-1 truncate text-left text-foreground"
:class="sorting ? 'cursor-default' : undefined"
:title="region.zoneLabel || region.regionName || `分区${ri + 1}`"
@click="scrollToRegion(region.uid)"
>
{{ region.zoneLabel || region.regionName || `分区${ri + 1}` }}
</button>
</div>
</div>
</aside>
<div
ref="scrollRef"
@@ -190,14 +304,20 @@ function onAnchorClick(e: Event) {
>
<Card
v-for="(region, ri) in regions"
:id="`quote-region-${ri}`"
:key="ri"
:id="`quote-region-${region.uid}`"
:key="region.uid"
size="small"
class="shrink-0 scroll-mt-2"
:title="region.regionName || `分区${ri + 1}`"
>
<template #extra>
<Button type="link" danger size="small" @click="removeRegion(ri)">
<Button
type="link"
danger
size="small"
:disabled="sorting"
@click="removeRegion(ri)"
>
删除分区
</Button>
</template>
@@ -253,7 +373,7 @@ function onAnchorClick(e: Event) {
:pagination="false"
:columns="bandColumnDefs"
:data-source="region.bands"
:row-key="(_r: BandRow, i: number) => `${ri}-${i}`"
:row-key="(_r: BandRow, i?: number) => `${region.uid}-${i ?? 0}`"
>
<template #bodyCell="{ column, index }">
<template v-if="column.key === 'from'">
@@ -0,0 +1,26 @@
/** 分区×重量带编辑用的行模型与前端 uid */
export interface BandRow {
weightFromG: number;
weightToG: number | null;
priceMode: string;
flatAmount: number;
firstWeightG: number;
firstAmount: number;
continuePerKg: number;
}
export interface RegionRow {
/** 前端稳定键,拖拽重排后避免卡片错位 */
uid: string;
regionName: string;
zoneLabel: string;
provinces: string[];
isDefault: boolean;
bands: BandRow[];
}
/** 生成分区前端 uid */
export function createRegionUid() {
return `r-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
}
@@ -20,9 +20,8 @@ import {
} from '#/api/ec/express-quote';
import { formatOverviewTabTitle } from '#/utils/overview-tab-title';
import RegionBandEditor, {
type RegionRow,
} from './components/RegionBandEditor.vue';
import RegionBandEditor from './components/RegionBandEditor.vue';
import { createRegionUid, type RegionRow } from './components/region-band';
defineOptions({ name: 'ExpressQuoteEdit' });
@@ -43,6 +42,7 @@ const pageTitle = ref('快递报价单编辑「新建」');
const provinces = ref<ExpressQuoteVO.ProvinceOption[]>([]);
const regions = ref<RegionRow[]>([
{
uid: createRegionUid(),
regionName: '默认区',
zoneLabel: '',
provinces: [],
@@ -280,6 +280,7 @@ function fillRegionsFromVersion(v?: ExpressQuoteVO.Version) {
if (!v?.regions?.length) {
regions.value = [
{
uid: createRegionUid(),
regionName: '默认区',
zoneLabel: '',
provinces: [],
@@ -299,7 +300,11 @@ function fillRegionsFromVersion(v?: ExpressQuoteVO.Version) {
];
return;
}
regions.value = v.regions.map((r) => ({
const sorted = [...v.regions].sort(
(a, b) => (a.sortOrder ?? 0) - (b.sortOrder ?? 0),
);
regions.value = sorted.map((r) => ({
uid: createRegionUid(),
regionName: r.regionName,
zoneLabel: r.zoneLabel || '',
provinces: r.provinces ? [...r.provinces] : [],