add ProductStockBatchSelect.vue
This commit is contained in:
@@ -0,0 +1,400 @@
|
||||
<template>
|
||||
<div v-can="permissionCodes.productStock.create" class="product-stock-batch-configure">
|
||||
<t-cell-group theme="card" class="batch-info">
|
||||
<t-form class="form" label-width="5rem">
|
||||
<t-form-item label="入库备注" required-mark>
|
||||
<t-textarea
|
||||
v-model="productStockBatchStore.remark"
|
||||
:disabled="isSaving"
|
||||
borderless
|
||||
maxlength="500"
|
||||
autosize
|
||||
placeholder="请输入本批次入库备注"
|
||||
@update:model-value="invalidateRequestNo"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<media-selector
|
||||
:model-value="globalAttachmentUrlList"
|
||||
v-model:photo-items="productStockBatchStore.attachmentItems"
|
||||
:upload="uploadPhotos"
|
||||
:edit-mode="true"
|
||||
:loading="isSaving"
|
||||
:show-remove="!isSaving"
|
||||
title="共用附件(未单独添加时使用)"
|
||||
@update:photo-items="invalidateRequestNo"
|
||||
/>
|
||||
<div class="attachment-tip" v-text="'每个 SKU 都必须有附件;未单独添加时使用共用附件'" />
|
||||
</t-form-item>
|
||||
</t-form>
|
||||
</t-cell-group>
|
||||
<div v-if="productStockBatchStore.selectedItemList.length" class="item-list">
|
||||
<article
|
||||
v-for="item in productStockBatchStore.selectedItemList"
|
||||
:key="item.sku.id"
|
||||
class="sku-item"
|
||||
>
|
||||
<header class="sku-header">
|
||||
<div class="sku-title">
|
||||
<strong v-text="item.sku.name || '未命名 SKU'" />
|
||||
<span class="sku-note" v-text="getSkuNote(item)" />
|
||||
</div>
|
||||
<t-button
|
||||
theme="danger"
|
||||
variant="text"
|
||||
size="small"
|
||||
content="移除"
|
||||
:disabled="isSaving"
|
||||
@click.stop="removeItem(item.sku.id || '')"
|
||||
/>
|
||||
</header>
|
||||
<div class="unit-selection">
|
||||
<strong v-text="'入库单位'" />
|
||||
<div class="unit-list" role="group" :aria-label="`选择${item.sku.name || 'SKU'}入库单位`">
|
||||
<t-button
|
||||
v-for="unit in getSelectableUnits(item)"
|
||||
:key="unit.id"
|
||||
size="small"
|
||||
:theme="item.skuUnitId === unit.id ? 'primary' : 'default'"
|
||||
:variant="item.skuUnitId === unit.id ? 'base' : 'outline'"
|
||||
:disabled="isSaving"
|
||||
:aria-pressed="item.skuUnitId === unit.id"
|
||||
@click="selectUnit(item, unit)"
|
||||
>
|
||||
<span v-text="unit.name || '未命名单位'" />
|
||||
</t-button>
|
||||
</div>
|
||||
</div>
|
||||
<t-form class="form" label-width="5rem">
|
||||
<t-form-item label="数量" required-mark>
|
||||
<t-input
|
||||
v-model="item.quantity"
|
||||
:disabled="isSaving"
|
||||
type="number"
|
||||
align="right"
|
||||
borderless
|
||||
:placeholder="item.sku.allowDecimalQuantity ? '最多三位小数' : '请输入整数'"
|
||||
@update:model-value="invalidateRequestNo"
|
||||
/>
|
||||
</t-form-item>
|
||||
<t-form-item label-width="0">
|
||||
<media-selector
|
||||
:model-value="getAttachmentUrlList(item.attachmentItems)"
|
||||
v-model:photo-items="item.attachmentItems"
|
||||
:upload="uploadPhotos"
|
||||
:edit-mode="true"
|
||||
:loading="isSaving"
|
||||
:show-remove="!isSaving"
|
||||
title="SKU 附件(可选)"
|
||||
@update:photo-items="invalidateRequestNo"
|
||||
/>
|
||||
<div class="attachment-tip" v-text="'添加 SKU 附件后,该 SKU 不使用共用附件'" />
|
||||
</t-form-item>
|
||||
</t-form>
|
||||
</article>
|
||||
</div>
|
||||
<t-empty v-else description="没有已选 SKU,请返回重新选择" />
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { onBeforeRouteLeave } from "vue-router";
|
||||
import { useUserStore } from "timi-web-api";
|
||||
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
||||
import { useNavBarStore } from "@/store/navBarStore";
|
||||
import { useProductStockBatchStore, type ProductStockBatchSelection } from "@/store/productStockBatchStore";
|
||||
import MediaSelector from "@/components/mediaSelector/MediaSelector.vue";
|
||||
import ProductAPI from "@/api/ProductAPI";
|
||||
import type { ProductStockManualInBatchItem, ProductStockManualInBatchRequest, ProductSkuUnit } from "@/types/Product";
|
||||
import type { PhotoItem } from "@/types/Photo";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { createProductRequestNo } from "@/utils/product";
|
||||
import { emitListDataChanged, listDataEventKeys } from "@/utils/listDataEvents";
|
||||
import { Message } from "@/utils/message";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { getCurrentStoreId } from "@/utils/store";
|
||||
import { uploadPhotoList } from "@/utils/photo";
|
||||
|
||||
defineOptions({ name: "ProductStockBatchConfigure" });
|
||||
|
||||
const router = useRouter();
|
||||
const navBarStore = useNavBarStore();
|
||||
const currentStoreStore = useCurrentStoreStore();
|
||||
const productStockBatchStore = useProductStockBatchStore();
|
||||
const userStore = useUserStore();
|
||||
const activeStoreId = ref("");
|
||||
const isSaving = ref(false);
|
||||
const uploadCount = ref(0);
|
||||
const globalAttachmentUrlList = computed(() => productStockBatchStore.attachmentItems.map(item => item.url));
|
||||
const canSubmit = computed(() => {
|
||||
const itemList = productStockBatchStore.selectedItemList;
|
||||
return !isSaving.value
|
||||
&& uploadCount.value === 0
|
||||
&& !!productStockBatchStore.remark.trim()
|
||||
&& 0 < itemList.length
|
||||
&& itemList.every(item => isItemValid(item));
|
||||
});
|
||||
|
||||
function getSelectableUnits(item?: ProductStockBatchSelection): ProductSkuUnit[] {
|
||||
return (item?.sku.unitList || []).filter(unit => unit.allowPurchase && !!unit.id);
|
||||
}
|
||||
|
||||
function getSkuNote(item: ProductStockBatchSelection): string {
|
||||
return [item.productName || "未命名产品", item.sku.specSummary].filter(Boolean).join(" · ");
|
||||
}
|
||||
|
||||
function getAttachmentUrlList(itemList: PhotoItem[]): string[] {
|
||||
return itemList.map(item => item.url);
|
||||
}
|
||||
|
||||
function getAttachmentList(itemList: PhotoItem[]): NonNullable<ProductStockManualInBatchItem["attachmentList"]> {
|
||||
return itemList.flatMap(item => item.tempFileId ? [{
|
||||
tempFileId: item.tempFileId,
|
||||
attachType: item.attachType || "PHOTO",
|
||||
name: item.attachment?.name
|
||||
}] : []);
|
||||
}
|
||||
|
||||
function invalidateRequestNo(): void {
|
||||
productStockBatchStore.requestNo = "";
|
||||
}
|
||||
|
||||
function isQuantityValid(item: ProductStockBatchSelection): boolean {
|
||||
if (!/^\d+(?:\.\d{1,3})?$/.test(item.quantity) || !(0 < Number(item.quantity))) {
|
||||
return false;
|
||||
}
|
||||
return item.sku.allowDecimalQuantity || /^\d+$/.test(item.quantity);
|
||||
}
|
||||
|
||||
function isItemValid(item: ProductStockBatchSelection): boolean {
|
||||
const unitList = getSelectableUnits(item);
|
||||
const hasUnit = unitList.some(unit => unit.id === item.skuUnitId);
|
||||
const itemAttachmentList = getAttachmentList(item.attachmentItems);
|
||||
const globalAttachmentList = getAttachmentList(productStockBatchStore.attachmentItems);
|
||||
return hasUnit
|
||||
&& isQuantityValid(item)
|
||||
&& 0 < (itemAttachmentList.length || globalAttachmentList.length);
|
||||
}
|
||||
|
||||
function updateNavBar(): void {
|
||||
navBarStore.clearRight();
|
||||
if (!userStore.hasPermissions(permissionCodes.productStock.create)) {
|
||||
return;
|
||||
}
|
||||
navBarStore.setRightText(isSaving.value ? "提交中" : "提交", "var(--td-brand-color)", submit);
|
||||
navBarStore.setRightDisabled(!canSubmit.value);
|
||||
}
|
||||
|
||||
function selectUnit(item: ProductStockBatchSelection, unit: ProductSkuUnit): void {
|
||||
if (isSaving.value) {
|
||||
return;
|
||||
}
|
||||
if (unit.id && getSelectableUnits(item).some(candidate => candidate.id === unit.id)) {
|
||||
if (item.skuUnitId !== unit.id) {
|
||||
invalidateRequestNo();
|
||||
}
|
||||
item.skuUnitId = unit.id;
|
||||
}
|
||||
}
|
||||
|
||||
function removeItem(skuId: string): void {
|
||||
if (isSaving.value) {
|
||||
return;
|
||||
}
|
||||
const itemIndex = productStockBatchStore.selectedItemList.findIndex(item => item.sku.id === skuId);
|
||||
if (-1 < itemIndex) {
|
||||
invalidateRequestNo();
|
||||
productStockBatchStore.selectedItemList.splice(itemIndex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
async function uploadPhotos(fileList: File[]): Promise<PhotoItem[]> {
|
||||
uploadCount.value += 1;
|
||||
try {
|
||||
return await uploadPhotoList(fileList);
|
||||
} finally {
|
||||
uploadCount.value -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
function validate(): boolean {
|
||||
if (!productStockBatchStore.remark.trim()) {
|
||||
Message.warning("入库备注不能为空");
|
||||
return false;
|
||||
}
|
||||
if (!productStockBatchStore.selectedItemList.length) {
|
||||
Message.warning("请至少选择一个 SKU");
|
||||
return false;
|
||||
}
|
||||
for (const item of productStockBatchStore.selectedItemList) {
|
||||
if (!getSelectableUnits(item).some(unit => unit.id === item.skuUnitId)) {
|
||||
Message.warning((item.sku.name || "SKU") + ":请选择入库单位");
|
||||
return false;
|
||||
}
|
||||
if (!isQuantityValid(item)) {
|
||||
const quantityTip = item.sku.allowDecimalQuantity
|
||||
? "数量必须大于 0,最多三位小数"
|
||||
: "数量必须是大于 0 的整数";
|
||||
Message.warning((item.sku.name || "SKU") + ":" + quantityTip);
|
||||
return false;
|
||||
}
|
||||
if (!getAttachmentList(item.attachmentItems).length && !getAttachmentList(productStockBatchStore.attachmentItems).length) {
|
||||
Message.warning((item.sku.name || "SKU") + ":请添加 SKU 附件或共用附件");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (0 < uploadCount.value) {
|
||||
Message.warning("附件上传完成后再提交");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function buildRequest(): ProductStockManualInBatchRequest {
|
||||
const itemList: ProductStockManualInBatchItem[] = productStockBatchStore.selectedItemList.map(item => {
|
||||
const attachmentList = getAttachmentList(item.attachmentItems);
|
||||
return {
|
||||
skuId: item.sku.id || "",
|
||||
skuUnitId: item.skuUnitId,
|
||||
quantity: item.quantity,
|
||||
...(attachmentList.length ? { attachmentList } : {})
|
||||
};
|
||||
});
|
||||
const attachmentList = getAttachmentList(productStockBatchStore.attachmentItems);
|
||||
return {
|
||||
requestNo: productStockBatchStore.requestNo || (productStockBatchStore.requestNo = createProductRequestNo()),
|
||||
storeId: productStockBatchStore.storeId,
|
||||
remark: productStockBatchStore.remark.trim(),
|
||||
...(attachmentList.length ? { attachmentList } : {}),
|
||||
itemList
|
||||
};
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
if (isSaving.value || !validate()) {
|
||||
return;
|
||||
}
|
||||
isSaving.value = true;
|
||||
updateNavBar();
|
||||
try {
|
||||
await ProductAPI.manualInBatch(buildRequest());
|
||||
emitListDataChanged(listDataEventKeys.productStockChanged);
|
||||
Message.success("批量入库已提交");
|
||||
productStockBatchStore.reset();
|
||||
await router.replace({ name: "ProductStockList" });
|
||||
} catch (error) {
|
||||
Message.error(getErrorMessage(error, "批量入库失败,请确认请求号后重试"));
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
updateNavBar();
|
||||
}
|
||||
}
|
||||
|
||||
watch([canSubmit, isSaving], updateNavBar);
|
||||
|
||||
onMounted(() => {
|
||||
const storeId = currentStoreStore.currentStoreId || getCurrentStoreId();
|
||||
productStockBatchStore.ensureStore(storeId);
|
||||
activeStoreId.value = storeId;
|
||||
if (!productStockBatchStore.selectedItemList.length) {
|
||||
void router.replace({ name: "ProductStockBatchSelect" });
|
||||
}
|
||||
updateNavBar();
|
||||
});
|
||||
|
||||
onActivated(() => {
|
||||
const storeId = currentStoreStore.currentStoreId || getCurrentStoreId();
|
||||
if (activeStoreId.value && activeStoreId.value !== storeId) {
|
||||
productStockBatchStore.ensureStore(storeId);
|
||||
activeStoreId.value = storeId;
|
||||
void router.replace({ name: "ProductStockBatchSelect" });
|
||||
return;
|
||||
}
|
||||
activeStoreId.value = storeId;
|
||||
updateNavBar();
|
||||
});
|
||||
|
||||
onBeforeRouteLeave(() => navBarStore.clearRight());
|
||||
|
||||
onUnmounted(() => navBarStore.clearRight());
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.product-stock-batch-configure {
|
||||
gap: 1rem;
|
||||
padding: var(--app-nav-height) 0 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
.form {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
.batch-info {
|
||||
padding: .25rem .75rem;
|
||||
}
|
||||
|
||||
.item-list {
|
||||
gap: 1rem;
|
||||
margin: 0 1rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.sku-item {
|
||||
gap: 1rem;
|
||||
border: 1px solid var(--td-component-border);
|
||||
padding: 1rem;
|
||||
display: flex;
|
||||
background: var(--td-bg-color-container);
|
||||
border-radius: 1rem;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.sku-header {
|
||||
gap: .75rem;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.sku-title {
|
||||
gap: .25rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
strong {
|
||||
color: var(--td-text-color-primary);
|
||||
font-size: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.sku-note {
|
||||
color: var(--td-text-color-secondary);
|
||||
font-size: .75rem;
|
||||
}
|
||||
|
||||
.unit-list {
|
||||
gap: .5rem;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.unit-selection {
|
||||
gap: .5rem;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
|
||||
strong {
|
||||
color: var(--td-text-color-primary);
|
||||
font-size: .875rem;
|
||||
}
|
||||
}
|
||||
|
||||
.attachment-tip {
|
||||
color: var(--td-text-color-secondary);
|
||||
margin-top: .25rem;
|
||||
font-size: .75rem;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,398 @@
|
||||
<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>
|
||||
@@ -65,13 +65,16 @@
|
||||
import { useUserStore } from "timi-web-api";
|
||||
import { onBeforeRouteLeave } from "vue-router";
|
||||
import ProductAPI from "@/api/ProductAPI";
|
||||
import { useCurrentStoreStore } from "@/store/currentStoreStore";
|
||||
import { useNavBarStore } from "@/store/navBarStore";
|
||||
import { useProductStockBatchStore } from "@/store/productStockBatchStore";
|
||||
import type { ProductStockProduct } from "@/types/Product";
|
||||
import { getErrorMessage } from "@/utils/error";
|
||||
import { getProductPhotoURL, getProductQuantityText } from "@/utils/product";
|
||||
import { events, isCurrentStoreEvent, listDataEventKeys, type ListDataChangedPayload } from "@/utils/listDataEvents";
|
||||
import { Message } from "@/utils/message";
|
||||
import { permissionCodes } from "@/utils/permission";
|
||||
import { getCurrentStoreId } from "@/utils/store";
|
||||
|
||||
defineOptions({ name: "ProductStockList" });
|
||||
|
||||
@@ -80,6 +83,8 @@ const route = useRoute();
|
||||
const productStockListRef = ref<HTMLElement | null>(null);
|
||||
const navBarStore = useNavBarStore();
|
||||
const userStore = useUserStore();
|
||||
const currentStoreStore = useCurrentStoreStore();
|
||||
const productStockBatchStore = useProductStockBatchStore();
|
||||
const keyword = ref("");
|
||||
const productList = ref<ProductStockProduct[]>([]);
|
||||
const pageIndex = ref(0);
|
||||
@@ -126,11 +131,35 @@ function restorePageScrollPosition(): void {
|
||||
}
|
||||
|
||||
const emptyText = computed(() => isLoading.value ? "正在加载产品" : keyword.value ? "未找到匹配产品" : "暂无启用库存管理的产品");
|
||||
const canBatchIn = computed(() =>
|
||||
userStore.hasPermissions(permissionCodes.productStock.read)
|
||||
&& userStore.hasPermissions(permissionCodes.productStock.create)
|
||||
);
|
||||
|
||||
function getCurrentRouteDepth(): number {
|
||||
return Number(route.meta.depth ?? 0);
|
||||
}
|
||||
|
||||
function updateNavBar(): void {
|
||||
navBarStore.clearRight();
|
||||
if (canBatchIn.value) {
|
||||
navBarStore.setRightText("批量入库", "var(--td-brand-color)", openBatchIn);
|
||||
}
|
||||
}
|
||||
|
||||
async function openBatchIn(): Promise<void> {
|
||||
const storeId = currentStoreStore.currentStoreId || getCurrentStoreId();
|
||||
if (!storeId) {
|
||||
Message.warning("请先选择门店");
|
||||
return;
|
||||
}
|
||||
productStockBatchStore.start(storeId);
|
||||
await router.push({
|
||||
name: "ProductStockBatchSelect",
|
||||
query: { transitionDepth: String(getCurrentRouteDepth() + 1) }
|
||||
});
|
||||
}
|
||||
|
||||
async function fetch(reset = false, preserveScroll = false): Promise<void> {
|
||||
if (!reset && (isLoading.value || isNoMore.value)) {
|
||||
return;
|
||||
@@ -272,7 +301,7 @@ async function toSkuList(item: ProductStockProduct): Promise<void> {
|
||||
|
||||
onMounted(async () => {
|
||||
unregisterProductStockListEvent = events.register(listDataEventKeys.productStockChanged, (payload: ListDataChangedPayload) => requestEventRefresh(payload));
|
||||
navBarStore.clearRight();
|
||||
updateNavBar();
|
||||
if (!userStore.hasPermissions(permissionCodes.productStock.read)) {
|
||||
return;
|
||||
}
|
||||
@@ -285,6 +314,7 @@ onMounted(async () => {
|
||||
|
||||
onActivated(() => {
|
||||
restorePageScrollPosition();
|
||||
updateNavBar();
|
||||
});
|
||||
|
||||
onDeactivated(() => {
|
||||
|
||||
Reference in New Issue
Block a user