add ProductStockBatchSelect.vue

This commit is contained in:
Timi
2026-10-15 15:29:56 +08:00
parent d8ac19baa7
commit a5ac6c5e69
14 changed files with 1605 additions and 352 deletions
@@ -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>