326 lines
10 KiB
Vue
326 lines
10 KiB
Vue
<template>
|
|
<bottom-popup
|
|
v-model="popupVisible"
|
|
title="选择产品 SKU"
|
|
height="80vh"
|
|
left-button-text="取消"
|
|
:right-button-text="props.selectionMode === 'multiple' ? '完成' : ''"
|
|
@left-button-click="cancelSelection"
|
|
@right-button-click="confirmSelection"
|
|
>
|
|
<div class="popup">
|
|
<div class="search-wrap">
|
|
<t-input v-model="keyword" class="search" borderless placeholder="搜索产品名称或编码" />
|
|
</div>
|
|
<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"
|
|
: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 ? (props.productScope === 'all' ? '正在加载产品' : '正在加载可售产品') : emptyProductText" />
|
|
</div>
|
|
</div>
|
|
</bottom-popup>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { BottomPopup } from "timi-tdesign-mobile";
|
|
import ProductAPI from "@/api/ProductAPI";
|
|
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;
|
|
visible?: boolean;
|
|
selectionMode?: ProductSkuSelectionMode;
|
|
/** sellable 只列可售产品;all 连停售产品一起列,用于来源产品这类仅作识别的场景 */
|
|
productScope?: "sellable" | "all";
|
|
selectableProductIdList?: string[];
|
|
selectableSkuIdList?: string[];
|
|
selectedSkuIdList?: string[];
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), { selectionMode: "single", productScope: "sellable" });
|
|
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,
|
|
set: value => {
|
|
internalVisible.value = value;
|
|
emit("update:visible", value);
|
|
}
|
|
});
|
|
const loading = ref(false);
|
|
const keyword = ref("");
|
|
const expandedProductKeyList = ref<Array<string | number>>([]);
|
|
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 => (!selectableProductIdSet.value || selectableProductIdSet.value.has(item.id || ""))
|
|
&& (!text || `${item.name}${item.code}`.toLocaleLowerCase().includes(text)));
|
|
});
|
|
const emptyProductText = computed(() => props.selectableProductIdList?.length === 0
|
|
? "客户暂无已购可履约产品"
|
|
: (props.productScope === "all" ? "暂无产品" : "暂无可售产品"));
|
|
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;
|
|
}
|
|
loading.value = true;
|
|
try {
|
|
productList.value = props.productScope === "all"
|
|
? (await ProductAPI.list({ index: 0, size: 1000 })).list || []
|
|
: await ProductAPI.sellableList({ index: 0, size: 1000 });
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, props.productScope === "all" ? "产品加载失败" : "可售产品加载失败"));
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
});
|
|
|
|
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 (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">
|
|
.popup {
|
|
height: 100%;
|
|
display: flex;
|
|
padding: 1rem 0 0 0;
|
|
overflow: hidden;
|
|
min-height: 0;
|
|
flex-direction: column;
|
|
|
|
.search-wrap {
|
|
z-index: 1;
|
|
padding: 0 0 .75rem;
|
|
|
|
.search {
|
|
--td-input-vertical-padding: .5rem .875rem;
|
|
|
|
margin: 0 1rem;
|
|
border: .0625rem solid var(--td-component-border);
|
|
border-radius: .875rem;
|
|
}
|
|
}
|
|
|
|
.items {
|
|
flex: 1;
|
|
min-height: 0;
|
|
overflow-y: auto;
|
|
padding-bottom: env(safe-area-inset-bottom, 0);
|
|
|
|
.selector {
|
|
border-radius: 0;
|
|
}
|
|
}
|
|
}
|
|
</style>
|