add OrderFulfillmentQuick.vue

This commit is contained in:
Timi
2026-10-21 00:14:06 +08:00
parent 77e0e7a8e0
commit db0431fbd5
13 changed files with 1716 additions and 35 deletions
@@ -3,8 +3,10 @@
v-model="popupVisible"
title="选择产品 SKU"
height="80vh"
right-button-text=""
@left-button-click="close"
left-button-text="取消"
:right-button-text="props.selectionMode === 'multiple' ? '完成' : ''"
@left-button-click="cancelSelection"
@right-button-click="confirmSelection"
>
<div class="popup">
<div class="search-wrap">
@@ -13,13 +15,19 @@
<div class="items">
<product-sku-selector
v-if="selectorItemList.length"
class="selector"
v-model:expanded-product-key-list="expandedProductKeyList"
:items="selectorItemList"
:selected-sku-id-list="selectedSkuIdList"
:selected-unit-id="props.modelValue?.skuUnitId"
:selectable-sku-id-list="props.selectableSkuIdList"
:selected-unit-id="props.selectionMode === 'single' ? props.modelValue?.skuUnitId : undefined"
:selection-mode="props.selectionMode"
@change="handleProductChange"
@select="selectUnit"
@toggle="toggleSku"
@check="toggleProduct"
/>
<center-tips v-else :content="loading ? '正在加载可售产品' : '暂无可售产品'" />
<center-tips v-else :content="loading ? '正在加载可售产品' : emptyProductText" />
</div>
</div>
</bottom-popup>
@@ -28,27 +36,37 @@
<script setup lang="ts">
import { BottomPopup } from "timi-tdesign-mobile";
import ProductAPI from "@/api/ProductAPI";
import type { Product, ProductSelection } from "@/types/Product";
import type { ProductSkuSelectionValue, ProductSkuSelectorItem } from "@/types/ProductSkuSelection";
import type { Product, ProductSelection, ProductSku } from "@/types/Product";
import type { ProductSkuSelectionMode, ProductSkuSelectionValue, ProductSkuSelectorItem } from "@/types/ProductSkuSelection";
import { getErrorMessage } from "@/utils/error";
import { Message } from "@/utils/message";
import { getProductPhotoURL } from "@/utils/product";
interface Props {
modelValue: ProductSkuSelectionValue | undefined;
modelValue?: ProductSkuSelectionValue;
visible?: boolean;
selectionMode?: ProductSkuSelectionMode;
selectableProductIdList?: string[];
selectableSkuIdList?: string[];
selectedSkuIdList?: string[];
}
const props = defineProps<Props>();
const props = withDefaults(defineProps<Props>(), { selectionMode: "single" });
const emit = defineEmits<{
"update:modelValue": [value: Props["modelValue"]];
"update:visible": [value: boolean];
"update:selectedSkuIdList": [value: string[]];
confirm: [value: string[]];
select: [selection: ProductSelection];
}>();
// ---------- 弹窗与产品列表 ----------
const productList = ref<Product[]>([]);
const productDetailMap = ref<Record<string, Product>>({});
const loadingProductKeySet = reactive(new Set<string>());
const checkingProductKeySet = reactive(new Set<string>());
const productDetailPromiseMap = new Map<string, Promise<Product | undefined>>();
const internalVisible = ref(false);
const popupVisible = computed({
get: () => props.visible ?? internalVisible.value,
@@ -60,23 +78,55 @@ const popupVisible = computed({
const loading = ref(false);
const keyword = ref("");
const expandedProductKeyList = ref<Array<string | number>>([]);
const selectedSkuIdList = computed(() => props.modelValue?.skuId ? [props.modelValue.skuId] : []);
const selectedSkuIdDraft = ref<string[]>([]);
const selectedSkuIdList = computed(() => props.selectionMode === "multiple"
? selectedSkuIdDraft.value
: props.modelValue?.skuId ? [props.modelValue.skuId] : []);
const selectableProductIdSet = computed(() => props.selectableProductIdList === undefined
? undefined
: new Set(props.selectableProductIdList));
const selectableSkuIdSet = computed(() => props.selectableSkuIdList === undefined
? undefined
: new Set(props.selectableSkuIdList));
const visibleProductList = computed(() => {
const text = keyword.value.trim().toLocaleLowerCase();
return productList.value.filter(item => !text || `${item.name}${item.code}`.toLocaleLowerCase().includes(text));
return productList.value.filter(item => (!selectableProductIdSet.value || selectableProductIdSet.value.has(item.id || ""))
&& (!text || `${item.name}${item.code}`.toLocaleLowerCase().includes(text)));
});
const selectorItemList = computed<ProductSkuSelectorItem[]>(() => visibleProductList.value.map((product, index) => ({
key: getProductKey(product, index),
name: product.name || product.code || "未命名产品",
photoUrl: getProductPhotoURL(product),
product
})));
const emptyProductText = computed(() => props.selectableProductIdList?.length === 0 ? "客户暂无已购可履约产品" : "暂无可售产品");
const selectorItemList = computed<ProductSkuSelectorItem[]>(() => visibleProductList.value.map((summary, index) => {
const key = getProductKey(summary, index);
const product = productDetailMap.value[key] || (summary.skuList?.length ? summary : undefined);
const skuList = product ? getSelectableSkuList(product) : [];
const selectedSkuCount = skuList.filter(sku => !!sku.id && selectedSkuIdList.value.includes(sku.id)).length;
const isLoadingProduct = loadingProductKeySet.has(key);
let message = "";
if (!product) {
message = isLoadingProduct ? "正在加载 SKU" : "展开产品以加载 SKU";
} else if (!skuList.length) {
message = product.status && product.status !== "ACTIVE" ? "该产品已停用" : "该产品没有可售 SKU";
}
return {
key,
name: summary.name || summary.code || "未命名产品",
photoUrl: getProductPhotoURL(summary),
product,
skuList: product ? skuList : undefined,
singleSkuId: skuList.length === 1 ? skuList[0].id : undefined,
skuCount: product ? skuList.length : 0,
selectedSkuCount,
canCheck: props.selectionMode === "multiple" && !product,
checkLoading: isLoadingProduct || checkingProductKeySet.has(key),
message
};
}));
watch(popupVisible, async visible => {
if (!visible) {
return;
}
expandedProductKeyList.value = [];
selectedSkuIdDraft.value = [...(props.selectedSkuIdList || [])];
if (loading.value || productList.value.length) {
return;
}
@@ -90,23 +140,147 @@ watch(popupVisible, async visible => {
}
});
watch(
[() => props.selectionMode, () => props.selectedSkuIdList],
([selectionMode, skuIdList]) => {
selectedSkuIdDraft.value = selectionMode === "multiple" ? [...(skuIdList || [])] : [];
},
{ immediate: true, deep: true }
);
function close(): void {
popupVisible.value = false;
expandedProductKeyList.value = [];
}
function cancelSelection(): void {
selectedSkuIdDraft.value = [...(props.selectedSkuIdList || [])];
close();
}
function confirmSelection(): void {
if (props.selectionMode === "multiple") {
const skuIdList = [...selectedSkuIdDraft.value];
emit("update:selectedSkuIdList", skuIdList);
emit("confirm", skuIdList);
}
close();
}
function getProductKey(product: Product, index: number): string {
return product.id || product.code || `product-${index}`;
}
function getSelectableSkuList(product: Product): ProductSku[] {
return (product.skuList || []).filter(sku => sku.status === "ACTIVE"
&& (!selectableSkuIdSet.value || !!sku.id && selectableSkuIdSet.value.has(sku.id)));
}
function getProduct(productKey: string): Product | undefined {
if (productDetailMap.value[productKey]) {
return productDetailMap.value[productKey];
}
return productList.value.find((product, index) => getProductKey(product, index) === productKey && !!product.skuList?.length);
}
async function ensureProductDetail(productKey: string): Promise<Product | undefined> {
const cachedProduct = productDetailMap.value[productKey] || getProduct(productKey);
if (cachedProduct) {
return cachedProduct;
}
const pendingRequest = productDetailPromiseMap.get(productKey);
if (pendingRequest) {
return await pendingRequest;
}
const summary = productList.value.find((product, index) => getProductKey(product, index) === productKey);
const productId = summary?.id;
if (!productId) {
return undefined;
}
loadingProductKeySet.add(productKey);
const request = (async () => {
try {
const product = await ProductAPI.detail(productId);
productDetailMap.value = { ...productDetailMap.value, [productKey]: product };
return product;
} catch (error) {
Message.error(getErrorMessage(error, "产品详情加载失败"));
return undefined;
} finally {
loadingProductKeySet.delete(productKey);
productDetailPromiseMap.delete(productKey);
}
})();
productDetailPromiseMap.set(productKey, request);
return await request;
}
async function handleProductChange(value: Array<string | number>): Promise<void> {
const productKey = String(value[0] || "");
if (productKey) {
await ensureProductDetail(productKey);
}
}
function selectUnit(selection: ProductSelection): void {
if (!selection.sku.id || !selection.unit.id) {
if (props.selectionMode !== "single" || !selection.sku.id || !selection.unit.id) {
return;
}
emit("select", selection);
emit("update:modelValue", { skuId: selection.sku.id, skuUnitId: selection.unit.id });
close();
}
function toggleSku(productKey: string, sku: ProductSku): void {
if (props.selectionMode !== "multiple" || !sku.id) {
return;
}
const product = getProduct(productKey);
if (!product || !getSelectableSkuList(product).some(item => item.id === sku.id)) {
return;
}
const currentIdSet = new Set(selectedSkuIdList.value);
if (currentIdSet.has(sku.id)) {
currentIdSet.delete(sku.id);
} else {
currentIdSet.add(sku.id);
}
updateSelectedSkuIdList([...currentIdSet]);
}
async function toggleProduct(productKey: string): Promise<void> {
if (props.selectionMode !== "multiple" || checkingProductKeySet.has(productKey)) {
return;
}
checkingProductKeySet.add(productKey);
try {
const product = await ensureProductDetail(productKey);
if (!product) {
return;
}
const skuIdList = getSelectableSkuList(product).flatMap(sku => sku.id ? [sku.id] : []);
if (!skuIdList.length) {
Message.warning("该产品没有可售 SKU");
return;
}
const currentIdSet = new Set(selectedSkuIdList.value);
const isSelected = skuIdList.every(id => currentIdSet.has(id));
for (const skuId of skuIdList) {
if (isSelected) {
currentIdSet.delete(skuId);
} else {
currentIdSet.add(skuId);
}
}
updateSelectedSkuIdList([...currentIdSet]);
} finally {
checkingProductKeySet.delete(productKey);
}
}
function updateSelectedSkuIdList(value: string[]): void {
selectedSkuIdDraft.value = value;
}
</script>
<style scoped lang="less">
@@ -136,6 +310,10 @@ function selectUnit(selection: ProductSelection): void {
min-height: 0;
overflow-y: auto;
padding-bottom: env(safe-area-inset-bottom, 0);
.selector {
border-radius: 0;
}
}
}
</style>
@@ -26,7 +26,7 @@
icon="line"
:model-value="isProductChecked(item) || isProductPartiallyChecked(item)"
:indeterminate="isProductPartiallyChecked(item)"
:disabled="item.checkLoading || !getSkuCount(item)"
:disabled="!canCheckProduct(item)"
:aria-label="`选择${item.name}`"
:aria-checked="isProductPartiallyChecked(item) ? 'mixed' : isProductChecked(item)"
borderless
@@ -47,7 +47,7 @@
<product-sku-options
v-else-if="item.product && openedProductKeySet.has(item.key)"
:product="item.product"
:sku-list="item.skuList"
:sku-list="getSkuList(item)"
:selected-sku-id-list="props.selectedSkuIdList"
:selected-unit-id="props.selectedUnitId"
:selection-mode="props.selectionMode"
@@ -66,12 +66,16 @@ import ProductSkuOptions from "./ProductSkuOptions.vue";
interface Props {
items: ProductSkuSelectorItem[];
selectedSkuIdList: string[];
selectableSkuIdList?: string[];
selectedUnitId?: string;
selectionMode?: ProductSkuSelectionMode;
}
const props = withDefaults(defineProps<Props>(), { selectionMode: "single" });
const expandedProductKeyList = defineModel<Array<string | number>>("expandedProductKeyList", { default: () => [] });
const selectableSkuIdSet = computed(() => props.selectableSkuIdList === undefined
? undefined
: new Set(props.selectableSkuIdList));
// 首次展开后保留选项内容,供面板收起时计算动画高度。
const openedProductKeySet = reactive(new Set<string>());
const emit = defineEmits<{
@@ -82,18 +86,34 @@ const emit = defineEmits<{
}>();
function isDirectSelect(item: ProductSkuSelectorItem): boolean {
return props.selectionMode === "multiple" && !!item.singleSkuId;
return props.selectionMode === "multiple"
&& (selectableSkuIdSet.value ? getSkuCount(item) === 1 : !!item.singleSkuId);
}
function getSkuCount(item: ProductSkuSelectorItem): number {
if (selectableSkuIdSet.value) {
return getSkuList(item).length;
}
return item.skuList?.length ?? item.skuCount ?? 0;
}
function getSkuList(item: ProductSkuSelectorItem): ProductSku[] {
const skuList = item.skuList || item.product?.skuList || [];
if (!selectableSkuIdSet.value) {
return skuList;
}
return skuList.filter(sku => !!sku.id && selectableSkuIdSet.value?.has(sku.id));
}
function canCheckProduct(item: ProductSkuSelectorItem): boolean {
return !item.checkLoading && (0 < getSkuCount(item) || item.canCheck === true);
}
function getSelectedSkuCount(item: ProductSkuSelectorItem): number {
if (!item.skuList) {
if (!item.skuList && !item.product?.skuList) {
return item.selectedSkuCount || 0;
}
return item.skuList.filter(sku => !!sku.id && props.selectedSkuIdList.includes(sku.id)).length;
return getSkuList(item).filter(sku => !!sku.id && props.selectedSkuIdList.includes(sku.id)).length;
}
function isProductChecked(item: ProductSkuSelectorItem): boolean {
@@ -107,7 +127,7 @@ function isProductPartiallyChecked(item: ProductSkuSelectorItem): boolean {
}
function toggleProduct(item: ProductSkuSelectorItem): void {
if (props.selectionMode === "multiple" && !item.checkLoading && getSkuCount(item)) {
if (props.selectionMode === "multiple" && canCheckProduct(item)) {
emit("check", item.key);
}
}
@@ -138,6 +158,7 @@ watch(expandedProductKeyList, (keyList) => {
.item {
--td-collapse-content-padding: .25rem 1rem .75rem 1rem;
--td-checkbox-vertical-padding: .5rem;
&.direct {
pointer-events: auto;