- create:仓库必须与店铺同货主;新增 _assertItemsOwner 校验明细商品/组合货主 (兼容 item_id / product_id 两种明细口径) - update:先处理店铺变更并推导生效货主(越权换店铺 403),仓库与明细按生效货主校验, 货主变更同步 order_index;subsidiary_id 禁止接口直写 - saveItems / changeWarehouse:补充同货主校验 - filters:货主权限下仓库下拉同步收窄,店铺/仓库下拉返回 subsidiary_id - 前端:建单弹窗仓库按店铺货主过滤、切店铺自动回落并清空跨货主商品, ProductSelect 新增 subsidiaryId 透传商品搜索与查询弹窗 - 契约:店铺/仓库下拉项补 subsidiary_id,建单店铺/仓库字段说明同货主约束
247 lines
6.6 KiB
Vue
247 lines
6.6 KiB
Vue
<script setup lang="ts">
|
||
import { computed, ref } from 'vue'
|
||
import { DEFAULT_PAGE_SIZE } from '@/composables/useListPage'
|
||
import {
|
||
getProducts,
|
||
searchProducts,
|
||
type ProductItem,
|
||
type ProductSearchItem,
|
||
} from '@/api/product'
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
modelValue: number | null
|
||
placeholder?: string
|
||
disabled?: boolean
|
||
/** 已选中但选项尚未加载(如编辑回显)时用于展示的标签 */
|
||
modelLabel?: string
|
||
/** 限定商品所属货主(下单时按店铺货主收窄,避免选到其它货主的商品) */
|
||
subsidiaryId?: number | null
|
||
}>(),
|
||
{
|
||
placeholder: '搜索并选择商品',
|
||
disabled: false,
|
||
modelLabel: '',
|
||
subsidiaryId: null,
|
||
},
|
||
)
|
||
|
||
const emit = defineEmits<{
|
||
'update:modelValue': [value: number | null]
|
||
select: [product: ProductSearchItem]
|
||
}>()
|
||
|
||
const options = ref<ProductSearchItem[]>([])
|
||
const loading = ref(false)
|
||
|
||
async function remoteMethod(keyword: string) {
|
||
loading.value = true
|
||
try {
|
||
options.value = await searchProducts(
|
||
keyword || '',
|
||
props.subsidiaryId == null ? undefined : { subsidiary_id: props.subsidiaryId },
|
||
)
|
||
} catch {
|
||
options.value = []
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
// 已选中但选项未加载(编辑回显)时,用 modelLabel 合成一个兜底选项
|
||
const selectOptions = computed(() => {
|
||
const list = [...options.value]
|
||
if (
|
||
props.modelValue != null &&
|
||
!list.some((o) => o.id === props.modelValue) &&
|
||
props.modelLabel
|
||
) {
|
||
list.push({
|
||
id: props.modelValue,
|
||
item_code: '',
|
||
item_name: props.modelLabel,
|
||
goods_code: null,
|
||
barcode: null,
|
||
sku_property: null,
|
||
stock_unit: null,
|
||
status: 1,
|
||
})
|
||
}
|
||
return list
|
||
})
|
||
|
||
function optLabel(p: ProductSearchItem) {
|
||
return p.item_code ? `${p.item_code} ${p.item_name}` : p.item_name
|
||
}
|
||
|
||
function onChange(val: number | null) {
|
||
emit('update:modelValue', val)
|
||
if (val != null) {
|
||
const p = options.value.find((x) => x.id === val)
|
||
if (p) emit('select', p)
|
||
}
|
||
}
|
||
|
||
/* -------------------- 查询弹窗(可翻页浏览) -------------------- */
|
||
const pickerVisible = ref(false)
|
||
const searchKey = ref('')
|
||
const pickerLoading = ref(false)
|
||
const pickerList = ref<ProductItem[]>([])
|
||
const pickerTotal = ref(0)
|
||
const pickerPage = ref(1)
|
||
const pickerPageSize = ref(DEFAULT_PAGE_SIZE)
|
||
|
||
async function openPicker() {
|
||
pickerVisible.value = true
|
||
searchKey.value = ''
|
||
pickerPage.value = 1
|
||
await fetchPicker()
|
||
}
|
||
async function fetchPicker() {
|
||
pickerLoading.value = true
|
||
try {
|
||
const res = await getProducts({
|
||
page: pickerPage.value,
|
||
pageSize: pickerPageSize.value,
|
||
keyword: searchKey.value || undefined,
|
||
status: 1,
|
||
...(props.subsidiaryId == null ? {} : { subsidiary_id: props.subsidiaryId }),
|
||
})
|
||
pickerList.value = res.list
|
||
pickerTotal.value = res.total
|
||
} catch {
|
||
pickerList.value = []
|
||
} finally {
|
||
pickerLoading.value = false
|
||
}
|
||
}
|
||
function onPickerSearch() {
|
||
pickerPage.value = 1
|
||
fetchPicker()
|
||
}
|
||
function onPickerPageChange(page: number) {
|
||
pickerPage.value = page
|
||
fetchPicker()
|
||
}
|
||
function pickProduct(row: ProductItem) {
|
||
const mapped: ProductSearchItem = {
|
||
id: row.id,
|
||
item_code: row.item_code,
|
||
goods_code: row.goods_code,
|
||
barcode: row.barcode,
|
||
item_name: row.item_name,
|
||
sku_property: row.sku_property,
|
||
stock_unit: row.stock_unit,
|
||
status: row.status,
|
||
}
|
||
if (!options.value.some((o) => o.id === row.id)) options.value.push(mapped)
|
||
emit('update:modelValue', row.id)
|
||
emit('select', mapped)
|
||
pickerVisible.value = false
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="product-select">
|
||
<el-select
|
||
:model-value="modelValue"
|
||
filterable
|
||
remote
|
||
clearable
|
||
:remote-method="remoteMethod"
|
||
:loading="loading"
|
||
:disabled="disabled"
|
||
:placeholder="placeholder"
|
||
class="product-select__input"
|
||
@update:model-value="onChange"
|
||
>
|
||
<el-option v-for="p in selectOptions" :key="p.id" :label="optLabel(p)" :value="p.id" />
|
||
</el-select>
|
||
<el-button size="small" :disabled="disabled" @click="openPicker">查询</el-button>
|
||
</div>
|
||
|
||
<el-dialog v-model="pickerVisible" title="商品查询" width="760px" append-to-body>
|
||
<el-form :inline="true" class="picker-search">
|
||
<el-form-item label="关键词">
|
||
<el-input
|
||
v-model="searchKey"
|
||
placeholder="商品编码 / 名称 / 货物编码 / 条码"
|
||
clearable
|
||
style="width: 280px"
|
||
@keyup.enter="onPickerSearch"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="primary" @click="onPickerSearch">查询</el-button>
|
||
<el-button @click="searchKey = ''; onPickerSearch()">重置</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
<div v-if="subsidiaryId != null" class="picker-tip">仅显示当前所选店铺同货主(所属公司)的商品</div>
|
||
<el-table
|
||
:data="pickerList"
|
||
v-loading="pickerLoading"
|
||
height="360"
|
||
size="small"
|
||
highlight-current-row
|
||
@row-dblclick="pickProduct"
|
||
>
|
||
<el-table-column prop="item_code" label="商品编码" width="140" />
|
||
<el-table-column prop="item_name" label="商品名称" min-width="160" />
|
||
<el-table-column prop="sku_property" label="规格" width="140" />
|
||
<el-table-column prop="stock_unit" label="单位" width="80" />
|
||
<el-table-column label="状态" width="90">
|
||
<template #default="{ row }">
|
||
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
|
||
{{ row.status === 1 ? '启用' : '禁用' }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" width="80" fixed="right">
|
||
<template #default="{ row }">
|
||
<el-button link type="primary" @click="pickProduct(row)">选择</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<el-pagination
|
||
class="picker-pager"
|
||
layout="prev, pager, next, total"
|
||
:total="pickerTotal"
|
||
:current-page="pickerPage"
|
||
:page-size="pickerPageSize"
|
||
@current-change="onPickerPageChange"
|
||
/>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.product-select {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 6px;
|
||
width: 100%;
|
||
}
|
||
.product-select__input {
|
||
flex: 1;
|
||
min-width: 0;
|
||
}
|
||
.picker-search {
|
||
margin-bottom: 6px;
|
||
}
|
||
.picker-tip {
|
||
margin-bottom: 6px;
|
||
color: var(--el-text-color-secondary);
|
||
font-size: 12px;
|
||
}
|
||
.picker-pager {
|
||
margin-top: 8px;
|
||
justify-content: flex-end;
|
||
}
|
||
/* 弹窗(append-to-body 仍带组件 scope 属性,可用 :deep 命中) */
|
||
.product-select :deep(.el-dialog__header) {
|
||
padding: 10px 12px;
|
||
}
|
||
.product-select :deep(.el-dialog__body) {
|
||
padding: 4px 12px 10px;
|
||
}
|
||
</style>
|