399 lines
12 KiB
Vue
399 lines
12 KiB
Vue
<template>
|
|
<div v-can="permissionCodes.productStock.create" class="product-stock-batch-select">
|
|
<div class="filters glass">
|
|
<t-input
|
|
v-model="keyword"
|
|
class="keyword"
|
|
type="search"
|
|
borderless
|
|
clearable
|
|
placeholder="搜索产品名称或编码"
|
|
/>
|
|
</div>
|
|
<t-pull-down-refresh
|
|
class="list-wrap empty-cell-title-margin"
|
|
v-model="isRefreshing"
|
|
:refresh-timeout="30000"
|
|
@refresh="refresh"
|
|
@scrolltolower="loadMore"
|
|
>
|
|
<product-sku-selector
|
|
class="selector"
|
|
v-if="productList.length"
|
|
v-model:expanded-product-key-list="expandedProductKeyList"
|
|
:items="selectorItemList"
|
|
:selected-sku-id-list="selectedSkuIdList"
|
|
selection-mode="multiple"
|
|
@change="handleProductChange"
|
|
@toggle="toggleSku"
|
|
@check="toggleProductSkus"
|
|
/>
|
|
<div v-else class="center-tips" v-text="emptyText" />
|
|
<div v-if="productList.length" class="center-tips" v-text="isLoading ? '正在加载更多' : isNoMore ? '已经到底了' : '上拉加载更多'" />
|
|
</t-pull-down-refresh>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { onBeforeRouteLeave } from "vue-router";
|
|
import { useUserStore } from "timi-web-api";
|
|
import ProductAPI from "@/api/ProductAPI";
|
|
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
|
import { useNavBarStore } from "@/store/navBarStore";
|
|
import { useProductStockBatchStore } from "@/store/productStockBatchStore";
|
|
import type { Product, ProductSku, ProductStockProduct } from "@/types/Product";
|
|
import type { ProductSkuSelectorItem } from "@/types/ProductSkuSelection";
|
|
import { getErrorMessage } from "@/utils/error";
|
|
import { getProductPhotoURL } from "@/utils/product";
|
|
import { Message } from "@/utils/message";
|
|
import { permissionCodes } from "@/utils/permission";
|
|
import { getCurrentStoreId } from "@/utils/store";
|
|
|
|
defineOptions({ name: "ProductStockBatchSelect" });
|
|
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
const navBarStore = useNavBarStore();
|
|
const userStore = useUserStore();
|
|
const currentStoreStore = useCurrentStoreStore();
|
|
const productStockBatchStore = useProductStockBatchStore();
|
|
const keyword = ref("");
|
|
const productList = ref<ProductStockProduct[]>([]);
|
|
const productDetailMap = ref<Record<string, Product>>({});
|
|
const checkingProductIdSet = reactive(new Set<string>());
|
|
const expandedProductKeyList = ref<Array<string | number>>([]);
|
|
const loadingProductId = ref("");
|
|
const pageIndex = ref(0);
|
|
const total = ref(0);
|
|
const isLoading = ref(false);
|
|
const isRefreshing = ref(false);
|
|
const isNoMore = ref(false);
|
|
const selectedSkuIdList = computed(() => productStockBatchStore.selectedItemList
|
|
.map(item => item.sku.id)
|
|
.filter((id): id is string => !!id));
|
|
const canContinue = computed(() => 0 < productStockBatchStore.selectedItemList.length);
|
|
const canReadAndCreate = computed(() =>
|
|
userStore.hasPermissions(permissionCodes.productStock.read)
|
|
&& userStore.hasPermissions(permissionCodes.productStock.create)
|
|
);
|
|
const emptyText = computed(() => {
|
|
if (isLoading.value) {
|
|
return "正在加载产品";
|
|
}
|
|
return keyword.value ? "未找到匹配产品" : "暂无可入库产品";
|
|
});
|
|
const selectorItemList = computed<ProductSkuSelectorItem[]>(() => {
|
|
const selectedCountByProduct = new Map<string, number>();
|
|
for (const selectedItem of productStockBatchStore.selectedItemList) {
|
|
selectedCountByProduct.set(selectedItem.productId, (selectedCountByProduct.get(selectedItem.productId) || 0) + 1);
|
|
}
|
|
return productList.value.map(item => {
|
|
const selectorItem: ProductSkuSelectorItem = {
|
|
key: item.productId,
|
|
name: item.productName || "未命名产品",
|
|
photoUrl: getProductPhotoURL(item),
|
|
singleSkuId: item.skuCount === 1 ? item.skuId : undefined,
|
|
skuCount: item.skuCount || 0,
|
|
selectedSkuCount: selectedCountByProduct.get(item.productId) || 0,
|
|
checkLoading: checkingProductIdSet.has(item.productId) || loadingProductId.value === item.productId
|
|
};
|
|
const product = getProduct(item.productId);
|
|
if (loadingProductId.value === item.productId || checkingProductIdSet.has(item.productId) && !product) {
|
|
return { ...selectorItem, message: "正在加载 SKU" };
|
|
}
|
|
if (!product) {
|
|
const message = expandedProductKeyList.value.includes(item.productId)
|
|
? "产品详情不可用,请重新展开"
|
|
: "正在加载 SKU";
|
|
return { ...selectorItem, message };
|
|
}
|
|
if (product.status !== "ACTIVE") {
|
|
return { ...selectorItem, singleSkuId: undefined, skuCount: 0, message: "该产品已停用,不能入库" };
|
|
}
|
|
const skuList = getSelectableSkuList(item.productId);
|
|
return skuList.length
|
|
? { ...selectorItem, product, skuList, singleSkuId: skuList.length === 1 ? skuList[0].id : undefined }
|
|
: { ...selectorItem, singleSkuId: undefined, skuCount: 0, message: "该产品没有可入库的 SKU" };
|
|
});
|
|
});
|
|
let keywordTimer: number | undefined;
|
|
let requestToken = 0;
|
|
let productDetailToken = 0;
|
|
let activeStoreId = "";
|
|
|
|
function getCurrentRouteDepth(): number {
|
|
const transitionDepth = Array.isArray(route.query.transitionDepth)
|
|
? route.query.transitionDepth[0]
|
|
: route.query.transitionDepth;
|
|
const depth = Number(transitionDepth);
|
|
if (transitionDepth && Number.isInteger(depth) && -1 < depth && depth < 21) {
|
|
return depth;
|
|
}
|
|
return Number(route.meta.depth ?? 0);
|
|
}
|
|
|
|
function updateNavBar(): void {
|
|
navBarStore.clearRight();
|
|
if (canReadAndCreate.value) {
|
|
navBarStore.setRightText("下一步", "var(--td-brand-color)", toConfigure);
|
|
navBarStore.setRightDisabled(!canContinue.value);
|
|
}
|
|
}
|
|
|
|
function getProduct(productId: string): Product | undefined {
|
|
return productDetailMap.value[productId];
|
|
}
|
|
|
|
function getSelectableSkuList(productId: string): ProductSku[] {
|
|
const product = getProduct(productId);
|
|
if (!product || product.status !== "ACTIVE" || !product.stockEnabled) {
|
|
return [];
|
|
}
|
|
return (product.skuList || []).filter(sku =>
|
|
!!sku.id
|
|
&& sku.status === "ACTIVE"
|
|
&& sku.stockEnabled
|
|
&& (sku.unitList || []).some(unit => unit.allowPurchase && unit.id)
|
|
);
|
|
}
|
|
|
|
async function handleProductChange(value: Array<string | number>): Promise<void> {
|
|
const productId = String(value[0] || "");
|
|
const token = ++productDetailToken;
|
|
loadingProductId.value = "";
|
|
if (!productId) {
|
|
return;
|
|
}
|
|
const cachedProduct = getProduct(productId);
|
|
if (cachedProduct || checkingProductIdSet.has(productId)) {
|
|
return;
|
|
}
|
|
loadingProductId.value = productId;
|
|
try {
|
|
const product = await ProductAPI.detail(productId);
|
|
if (token !== productDetailToken) {
|
|
return;
|
|
}
|
|
productDetailMap.value = { ...productDetailMap.value, [productId]: product };
|
|
if (product.status !== "ACTIVE") {
|
|
Message.warning("该产品已停用,不能入库");
|
|
} else if (getSelectableSkuList(productId).length === 1) {
|
|
expandedProductKeyList.value = expandedProductKeyList.value.filter(key => key !== productId);
|
|
}
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品详情加载失败"));
|
|
} finally {
|
|
if (token === productDetailToken) {
|
|
loadingProductId.value = "";
|
|
}
|
|
}
|
|
}
|
|
|
|
function toggleSku(productId: string, sku: ProductSku): boolean {
|
|
const item = productList.value.find(product => product.productId === productId);
|
|
if (!item || !sku.id || !getSelectableSkuList(productId).some(candidate => candidate.id === sku.id)) {
|
|
return false;
|
|
}
|
|
const isSelected = productStockBatchStore.hasSku(sku.id);
|
|
productStockBatchStore.requestNo = "";
|
|
productStockBatchStore.toggleSelection({
|
|
productId: item.productId,
|
|
productName: item.productName,
|
|
sku
|
|
});
|
|
return !isSelected;
|
|
}
|
|
|
|
async function toggleProductSkus(productId: string): Promise<void> {
|
|
if (checkingProductIdSet.has(productId)) {
|
|
return;
|
|
}
|
|
checkingProductIdSet.add(productId);
|
|
const requestedStoreId = activeStoreId;
|
|
try {
|
|
if (!getProduct(productId)) {
|
|
const product = await ProductAPI.detail(productId);
|
|
if (requestedStoreId !== activeStoreId) {
|
|
return;
|
|
}
|
|
productDetailMap.value = { ...productDetailMap.value, [productId]: product };
|
|
}
|
|
if (getProduct(productId)?.status !== "ACTIVE") {
|
|
Message.warning("该产品已停用,不能入库");
|
|
return;
|
|
}
|
|
const skuList = getSelectableSkuList(productId);
|
|
if (!skuList.length) {
|
|
Message.warning("该产品没有可入库的 SKU");
|
|
return;
|
|
}
|
|
if (skuList.length === 1) {
|
|
expandedProductKeyList.value = expandedProductKeyList.value.filter(key => key !== productId);
|
|
}
|
|
const item = productList.value.find(product => product.productId === productId);
|
|
if (!item) {
|
|
return;
|
|
}
|
|
const selectedSkuIdSet = new Set(selectedSkuIdList.value);
|
|
const allSelected = skuList.every(sku => selectedSkuIdSet.has(sku.id || ""));
|
|
productStockBatchStore.requestNo = "";
|
|
for (const sku of skuList) {
|
|
if (allSelected || !selectedSkuIdSet.has(sku.id || "")) {
|
|
productStockBatchStore.toggleSelection({
|
|
productId: item.productId,
|
|
productName: item.productName,
|
|
sku
|
|
});
|
|
}
|
|
}
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品详情加载失败"));
|
|
} finally {
|
|
checkingProductIdSet.delete(productId);
|
|
}
|
|
}
|
|
|
|
async function toConfigure(): Promise<void> {
|
|
if (!canContinue.value) {
|
|
Message.warning("请至少选择一个 SKU");
|
|
return;
|
|
}
|
|
await router.push({
|
|
name: "ProductStockBatchConfigure",
|
|
query: { transitionDepth: String(getCurrentRouteDepth() + 1) }
|
|
});
|
|
}
|
|
|
|
async function fetch(reset = false): Promise<void> {
|
|
if (!reset && (isLoading.value || isNoMore.value)) {
|
|
return;
|
|
}
|
|
if (reset) {
|
|
pageIndex.value = 0;
|
|
total.value = 0;
|
|
isNoMore.value = false;
|
|
expandedProductKeyList.value = [];
|
|
productDetailToken += 1;
|
|
}
|
|
const token = ++requestToken;
|
|
isLoading.value = true;
|
|
try {
|
|
const pageSize = 15;
|
|
const result = await ProductAPI.stockProductList({
|
|
index: pageIndex.value,
|
|
size: pageSize,
|
|
keyword: keyword.value.trim() || undefined
|
|
});
|
|
if (token !== requestToken) {
|
|
return;
|
|
}
|
|
const list = result.list || [];
|
|
productList.value = reset ? list : productList.value.concat(list);
|
|
total.value = result.total || 0;
|
|
pageIndex.value += 1;
|
|
isNoMore.value = list.length < pageSize
|
|
|| productList.value.length === total.value
|
|
|| total.value < productList.value.length;
|
|
} finally {
|
|
if (token === requestToken) {
|
|
isLoading.value = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
async function refresh(): Promise<void> {
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
} finally {
|
|
isRefreshing.value = false;
|
|
}
|
|
}
|
|
|
|
async function loadMore(): Promise<void> {
|
|
try {
|
|
await fetch();
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
}
|
|
|
|
watch(canContinue, updateNavBar);
|
|
watch(keyword, () => {
|
|
window.clearTimeout(keywordTimer);
|
|
keywordTimer = window.setTimeout(async () => {
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
}, 300);
|
|
});
|
|
|
|
onMounted(async () => {
|
|
const storeId = currentStoreStore.currentStoreId || getCurrentStoreId();
|
|
productStockBatchStore.ensureStore(storeId);
|
|
activeStoreId = storeId;
|
|
updateNavBar();
|
|
if (!canReadAndCreate.value) {
|
|
return;
|
|
}
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
});
|
|
|
|
onActivated(async () => {
|
|
const storeId = currentStoreStore.currentStoreId || getCurrentStoreId();
|
|
productStockBatchStore.ensureStore(storeId);
|
|
if (activeStoreId && activeStoreId !== storeId) {
|
|
productDetailMap.value = {};
|
|
try {
|
|
await fetch(true);
|
|
} catch (error) {
|
|
Message.error(getErrorMessage(error, "产品列表加载失败"));
|
|
}
|
|
}
|
|
activeStoreId = storeId;
|
|
updateNavBar();
|
|
});
|
|
|
|
onBeforeRouteLeave(() => navBarStore.clearRight());
|
|
|
|
onUnmounted(() => {
|
|
navBarStore.clearRight();
|
|
window.clearTimeout(keywordTimer);
|
|
requestToken += 1;
|
|
productDetailToken += 1;
|
|
});
|
|
</script>
|
|
|
|
<style scoped lang="less">
|
|
.product-stock-batch-select {
|
|
padding: var(--app-nav-height) 0 1rem;
|
|
|
|
.filters {
|
|
top: var(--app-nav-height);
|
|
z-index: 10;
|
|
padding: .75rem 1rem;
|
|
position: sticky;
|
|
|
|
.keyword {
|
|
border: 1px solid var(--td-component-border);
|
|
padding: .5rem 1rem;
|
|
border-radius: 1rem;
|
|
}
|
|
}
|
|
|
|
.list-wrap {
|
|
|
|
.selector {
|
|
margin: 0 1rem;
|
|
}
|
|
}
|
|
}
|
|
</style>
|