liufumingren b946d09a03 style(frontend): 列表表格改为表体内部滚动,分页默认 20 条
- 各列表 el-table/DataTable 传 height="100%",让表头固定、表体滚动
  (原先只写了 CSS height,Element Plus 不会生成内部滚动容器,行多时被父容器裁掉)
- 每页条数选项改为 20/50/100/200,默认 20;移除硬编码的 10 条/页
  (外打平台商家列表、店铺商品绑定弹窗、商品选择弹窗统一走 DEFAULT_PAGE_SIZE)
- 清理因此失效的 .fill-table 样式

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-03 03:29:15 +08:00

703 lines
23 KiB
Vue

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import {
createStockOrderApi,
type StockBillType,
type StockItemLine,
type StockOrder,
type StockOrderPayload,
type StockItemPayload,
} from '@/api/stockOrder'
import { getWarehouseOptions, type WarehouseOption } from '@/api/warehouse'
import { searchProducts, type ProductSearchItem } from '@/api/product'
import SplitLayout from '@/components/SplitLayout.vue'
import DataTable from '@/components/DataTable.vue'
import ListPager from '@/components/ListPager.vue'
import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage'
import VirtualTable from '@/components/VirtualTable.vue'
import { formatDateTime } from '@/utils/datetime'
import VirtualSelect from '@/components/VirtualSelect.vue'
import type { VirtualColumn } from '@/components/virtualTable'
const props = defineProps<{
billType: number
billTypeName: string
partyField: 'supplier' | 'customer'
partyLabel: string
}>()
const api = createStockOrderApi(props.billType as StockBillType)
const STATUS_LABELS = computed<Record<number, string>>(() => {
const done = props.billType === 1 ? '完成入库' : '完成出库'
return { 1: '已创建', 2: '已审核', 3: '已推送', 4: done, 9: '已取消' }
})
const STATUS_TAGS: Record<number, string> = { 1: 'info', 2: 'warning', 3: 'primary', 4: 'success', 9: 'danger' }
const TYPE_OPTIONS = computed(() =>
props.billType === 1
? [
{ label: '采购入库', value: 1 },
{ label: '退货入库', value: 2 },
{ label: '其他', value: 9 },
]
: [
{ label: '销售出库', value: 1 },
{ label: '退货出库', value: 2 },
{ label: '其他', value: 9 },
],
)
const warehouses = ref<WarehouseOption[]>([])
/** 仓库下拉(全量仓库,虚拟滚动) */
const warehouseSelectOptions = computed(() =>
warehouses.value.map((w) => ({ label: `${w.code} ${w.name}`, value: w.id })),
)
const query = reactive({
page: 1,
pageSize: DEFAULT_PAGE_SIZE,
bill_no: '',
status: '' as string | number,
type: '' as string | number,
warehouse_id: '' as string | number,
})
const { loading, list, total, elapsed, fetchList, search, handlePageChange, handleSizeChange } = useListPage({
query,
fetcher: () => {
const params: Record<string, unknown> = { ...query }
if (!params.bill_no) delete params.bill_no
if (params.status === '') delete params.status
if (params.type === '') delete params.type
if (params.warehouse_id === '') delete params.warehouse_id
return api.getList(params)
},
})
async function fetchWarehouses() {
try {
const res = await getWarehouseOptions()
warehouses.value = res
} catch {
warehouses.value = []
}
}
function reset() {
query.bill_no = ''
query.status = ''
query.type = ''
query.warehouse_id = ''
search()
}
function typeLabel(row: StockOrder): string {
const t = (props.billType === 1 ? row.inbound_type : row.outbound_type) as number
return TYPE_OPTIONS.value.find((o) => o.value === t)?.label || String(t)
}
function partyValue(row: StockOrder): string {
return (row[props.partyField] as string) || '-'
}
/* -------------------- 新增 / 编辑 -------------------- */
const dialogVisible = ref(false)
const submitting = ref(false)
const formRef = ref<FormInstance>()
const editId = ref<number | null>(null)
const form = reactive({
warehouse_id: null as number | null,
type: 1,
status: 1,
party: '',
operator: '',
total_amount: 0,
remark: '',
})
const rules: FormRules = {
warehouse_id: [{ required: true, message: '请选择仓库', trigger: 'change' }],
}
function openCreate() {
editId.value = null
Object.assign(form, {
warehouse_id: null,
type: 1,
status: 1,
party: '',
operator: '',
total_amount: 0,
remark: '',
})
dialogVisible.value = true
}
function openEdit(row: StockOrder) {
editId.value = row.id
form.warehouse_id = row.warehouse_id ?? null
form.type = (props.billType === 1 ? row.inbound_type : row.outbound_type) || 1
form.status = row.status
form.party = (row[props.partyField] as string) || ''
form.operator = row.operator || ''
form.total_amount = Number(row.total_amount) || 0
form.remark = row.remark || ''
dialogVisible.value = true
}
async function submitForm() {
if (!formRef.value) return
await formRef.value.validate(async (valid) => {
if (!valid) return
submitting.value = true
try {
const payload: StockOrderPayload = {
warehouse_id: form.warehouse_id,
status: form.status,
operator: form.operator || null,
total_amount: form.total_amount,
remark: form.remark || undefined,
}
if (props.billType === 1) payload.inbound_type = form.type
else payload.outbound_type = form.type
payload[props.partyField] = form.party || null
if (editId.value == null) {
await api.create(payload)
ElMessage.success('新增成功')
} else {
await api.update(editId.value, payload)
ElMessage.success('保存成功')
}
dialogVisible.value = false
await fetchList()
} catch {
// 拦截器已提示
} finally {
submitting.value = false
}
})
}
/* -------------------- 状态流转(审核 / 推送 / 完成 / 取消 / 恢复) -------------------- */
const transiting = ref(false)
async function transit(action: 'audit' | 'push' | 'complete' | 'cancel' | 'restore') {
if (!currentOrder.value) return
transiting.value = true
try {
const id = currentOrder.value.id
if (action === 'audit') await api.audit(id)
else if (action === 'push') await api.push(id)
else if (action === 'complete') await api.complete(id)
else if (action === 'cancel') await api.cancel(id)
else if (action === 'restore') await api.restore(id)
ElMessage.success('操作成功')
const map: Record<string, number> = { audit: 2, push: 3, complete: 4, cancel: 9, restore: 1 }
currentOrder.value.status = map[action]
await fetchList()
} catch {
// 拦截器已提示
} finally {
transiting.value = false
}
}
async function handleDelete(row: StockOrder) {
try {
await ElMessageBox.confirm(`确认删除${props.billTypeName}「${row.bill_no}」?`, '删除确认', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
})
} catch {
return
}
try {
await api.remove(row.id)
ElMessage.success('已删除')
if (currentOrder.value && currentOrder.value.id === row.id) {
currentOrder.value = null
items.value = []
}
await fetchList()
} catch {
// 拦截器已提示
}
}
/* -------------------- 明细 -------------------- */
const itemsLoading = ref(false)
const currentOrder = ref<StockOrder | null>(null)
const activeDetailTab = ref('detail')
const items = ref<StockItemLine[]>([])
const productOptions = ref<ProductSearchItem[]>([])
/** 明细列(虚拟滚动,明细行可能有几百条) */
const itemColumns: VirtualColumn[] = [
{ key: 'name', title: '商品名称', minWidth: 140, slot: 'name' },
{ key: 'code', title: '商品编码', width: 120, slot: 'code' },
{ key: 'spec', title: '规格', width: 120, slot: 'spec' },
{ key: 'unit', title: '单位', width: 80, slot: 'unit' },
{ key: 'quantity', title: '数量', width: 90, align: 'right' },
{ key: 'price', title: '单价', width: 100, align: 'right', slot: 'price' },
{ key: 'total_amount', title: '金额', width: 110, align: 'right' },
{ key: 'actions', title: '操作', width: 140, slot: 'actions', fixed: true },
]
const productSearching = ref(false)
/** 商品快速搜索(聚合:商品编码/货品编码/条码/商品名称),仅返回启用商品 */
async function remoteSearchProduct(keyword: string) {
productSearching.value = true
try {
productOptions.value = await searchProducts(keyword || '')
} catch {
productOptions.value = []
} finally {
productSearching.value = false
}
}
async function fetchItems() {
if (!currentOrder.value) return
itemsLoading.value = true
try {
const res = await api.getItems(currentOrder.value.id, { pageSize: 200 })
items.value = res.list
} catch {
// 拦截器已提示
} finally {
itemsLoading.value = false
}
}
function handleRowClick(row: StockOrder) {
currentOrder.value = row
fetchItems()
}
const itemDialogVisible = ref(false)
const itemSubmitting = ref(false)
const itemEditId = ref<number | null>(null)
const itemForm = reactive({
product_id: null as number | null,
name: '',
code: '',
spec: '',
unit: '',
quantity: 1,
price: 0,
remark: '',
})
function onProductChange(pid: number | null) {
const p = productOptions.value.find((x) => x.id === pid)
if (p) {
itemForm.name = p.item_name || ''
itemForm.code = p.item_code || ''
itemForm.spec = p.sku_property || ''
itemForm.unit = p.stock_unit || ''
}
}
function openItemCreate() {
itemEditId.value = null
Object.assign(itemForm, {
product_id: null,
name: '',
code: '',
spec: '',
unit: '',
quantity: 1,
price: 0,
remark: '',
})
itemDialogVisible.value = true
}
function openItemEdit(row: StockItemLine) {
itemEditId.value = row.id
// 编辑时回显已关联商品,便于下拉展示(搜索接口仅返回启用商品)
productOptions.value = row.product
? [{ id: row.product.id, item_code: row.product.item_code, item_name: row.product.item_name, sku_property: row.product.sku_property, goods_code: null, barcode: null, stock_unit: row.unit || null, status: 1 }]
: []
Object.assign(itemForm, {
product_id: row.product_id,
name: row.name || '',
code: row.code || '',
spec: row.spec || '',
unit: row.unit || '',
quantity: Number(row.quantity) || 0,
price: Number(row.price) || 0,
remark: row.remark || '',
})
itemDialogVisible.value = true
}
async function submitItem() {
if (!currentOrder.value) return
itemSubmitting.value = true
try {
const payload: StockItemPayload = {
bill_id: currentOrder.value.id,
product_id: itemForm.product_id,
name: itemForm.name || null,
code: itemForm.code || null,
spec: itemForm.spec || null,
unit: itemForm.unit || null,
quantity: itemForm.quantity,
price: itemForm.price,
remark: itemForm.remark || null,
}
if (itemEditId.value == null) {
await api.createItem(payload)
ElMessage.success('已新增明细')
} else {
await api.updateItem(itemEditId.value, payload)
ElMessage.success('已保存明细')
}
itemDialogVisible.value = false
await fetchItems()
} catch {
// 拦截器已提示
} finally {
itemSubmitting.value = false
}
}
async function handleDeleteItem(row: StockItemLine) {
try {
await ElMessageBox.confirm(`确认删除明细「${row.name || row.code || row.id}」?`, '删除确认', {
type: 'warning',
confirmButtonText: '删除',
cancelButtonText: '取消',
})
} catch {
return
}
try {
await api.removeItem(row.id)
ElMessage.success('已删除')
await fetchItems()
} catch {
// 拦截器已提示
}
}
onMounted(async () => {
await fetchWarehouses()
await fetchList()
})
</script>
<template>
<el-card class="stock-card">
<template #header>
<div class="page-header">
<span>{{ billTypeName }}</span>
<el-button type="primary" @click="openCreate">新增{{ billTypeName }}</el-button>
</div>
</template>
<SplitLayout :first-size="360" storage-key="stock-order-split" :show-second="!!currentOrder">
<template #first>
<el-form :inline="true" class="search-bar">
<el-form-item label="单号">
<el-input v-model="query.bill_no" placeholder="支持模糊匹配" clearable @keyup.enter="search" />
</el-form-item>
<el-form-item label="状态">
<el-select v-model="query.status" placeholder="全部" clearable style="width: 130px">
<el-option v-for="(label, val) in STATUS_LABELS" :key="val" :label="label" :value="Number(val)" />
</el-select>
</el-form-item>
<el-form-item :label="billTypeName + '类型'">
<el-select v-model="query.type" placeholder="全部" clearable style="width: 140px">
<el-option v-for="o in TYPE_OPTIONS" :key="o.value" :label="o.label" :value="o.value" />
</el-select>
</el-form-item>
<el-form-item label="店铺/仓库">
<VirtualSelect
v-model="query.warehouse_id"
:options="warehouseSelectOptions"
placeholder="全部仓库"
clearable
style="width: 160px"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="search">查询</el-button>
<el-button @click="reset">重置</el-button>
</el-form-item>
</el-form>
<div class="top-table-wrap">
<DataTable
:loading="loading"
:data="list"
highlight-current-row
height="100%"
@row-click="handleRowClick"
>
<el-table-column prop="bill_no" label="单号" min-width="160" />
<el-table-column label="仓库" width="150">
<template #default="{ row }">{{ row.warehouse?.name || '-' }}</template>
</el-table-column>
<el-table-column label="类型" width="120">
<template #default="{ row }">{{ typeLabel(row) }}</template>
</el-table-column>
<el-table-column label="状态" width="100">
<template #default="{ row }">
<el-tag :type="STATUS_TAGS[row.status]">{{ STATUS_LABELS[row.status] || row.status }}</el-tag>
</template>
</el-table-column>
<el-table-column :label="partyLabel" width="140">
<template #default="{ row }">{{ partyValue(row) }}</template>
</el-table-column>
<el-table-column label="操作人" width="100">
<template #default="{ row }">{{ row.operator || '-' }}</template>
</el-table-column>
<el-table-column label="创建人" width="100">
<template #default="{ row }">{{ row.creator?.nickname || row.creator?.username || '-' }}</template>
</el-table-column>
<el-table-column label="金额" width="120" align="right">
<template #default="{ row }">{{ row.total_amount }}</template>
</el-table-column>
<el-table-column label="商品总数" width="100" align="right">
<template #default="{ row }">{{ row.item_total ?? 0 }}</template>
</el-table-column>
<el-table-column label="商品种类" width="100" align="right">
<template #default="{ row }">{{ row.item_kind ?? 0 }}</template>
</el-table-column>
<el-table-column prop="created_at" label="创建时间" width="180">
<template #default="{ row }">{{ formatDateTime(row.created_at) }}</template>
</el-table-column>
<el-table-column label="操作" width="200" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="openEdit(row)">编辑</el-button>
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
</template>
</el-table-column>
</DataTable>
</div>
<ListPager
:total="total"
:elapsed="elapsed"
:page="query.page"
:page-size="query.pageSize"
@page-change="handlePageChange"
@size-change="handleSizeChange"
/>
</template>
<template #second>
<el-tabs v-model="activeDetailTab" type="border-card" class="detail-tabs">
<el-tab-pane label="明细" name="detail">
<div class="items-toolbar">
<span class="items-title">
{{ billTypeName }}明细<template v-if="currentOrder"> · {{ currentOrder.bill_no }}</template>
</span>
<div class="status-actions" v-if="currentOrder">
<el-tag :type="STATUS_TAGS[currentOrder.status]">{{ STATUS_LABELS[currentOrder.status] || currentOrder.status }}</el-tag>
<el-button size="small" :disabled="transiting" v-if="currentOrder.status === 1" @click="transit('audit')">审核</el-button>
<el-button size="small" :disabled="transiting" v-if="currentOrder.status === 2" @click="transit('push')">推送</el-button>
<el-button size="small" :disabled="transiting" v-if="currentOrder.status === 3" @click="transit('complete')">完成</el-button>
<el-button size="small" :disabled="transiting" v-if="currentOrder.status === 9" @click="transit('restore')">恢复</el-button>
<el-button size="small" type="danger" plain :disabled="transiting" v-if="currentOrder.status !== 4 && currentOrder.status !== 9" @click="transit('cancel')">取消</el-button>
</div>
<el-button type="primary" :disabled="!currentOrder" @click="openItemCreate">新增明细</el-button>
</div>
<div class="bottom-table-wrap">
<template v-if="currentOrder">
<VirtualTable
:loading="itemsLoading"
:data="items"
:columns="itemColumns"
class="fill-table"
>
<template #name="{ row }">{{ row.name || (row.product ? row.product.item_name : '-') }}</template>
<template #code="{ row }">{{ row.code || (row.product ? row.product.item_code : '-') }}</template>
<template #spec="{ row }">{{ row.spec || '-' }}</template>
<template #unit="{ row }">{{ row.unit || '-' }}</template>
<template #price="{ row }">{{ row.price ?? '-' }}</template>
<template #actions="{ row }">
<el-button link type="primary" @click="openItemEdit(row)">编辑</el-button>
<el-button link type="danger" @click="handleDeleteItem(row)">删除</el-button>
</template>
</VirtualTable>
</template>
<el-empty v-else description="请点击上方单据查看明细" />
</div>
</el-tab-pane>
</el-tabs>
</template>
</SplitLayout>
<el-dialog v-model="dialogVisible" :title="editId == null ? '新增' + billTypeName : '编辑' + billTypeName" width="580px">
<el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
<el-form-item label="仓库" prop="warehouse_id">
<VirtualSelect
v-model="form.warehouse_id"
:options="warehouseSelectOptions"
placeholder="请选择仓库"
clearable
style="width: 100%"
/>
</el-form-item>
<el-form-item :label="billTypeName + '类型'" prop="type">
<el-select v-model="form.type" style="width: 100%">
<el-option v-for="o in TYPE_OPTIONS" :key="o.value" :label="o.label" :value="o.value" />
</el-select>
</el-form-item>
<el-form-item v-if="editId != null" label="状态" prop="status">
<el-select v-model="form.status" style="width: 100%">
<el-option v-for="(label, val) in STATUS_LABELS" :key="val" :label="label" :value="Number(val)" />
</el-select>
</el-form-item>
<el-form-item :label="partyLabel" prop="party">
<el-input v-model="form.party" placeholder="选填" />
</el-form-item>
<el-form-item label="操作人" prop="operator">
<el-input v-model="form.operator" placeholder="选填" />
</el-form-item>
<el-form-item label="单据金额" prop="total_amount">
<el-input-number v-model="form.total_amount" :min="0" :precision="2" style="width: 100%" />
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="选填" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="submitting" @click="submitForm">保存</el-button>
</template>
</el-dialog>
<el-dialog
v-model="itemDialogVisible"
:title="itemEditId == null ? '新增明细' : '编辑明细'"
width="520px"
append-to-body
>
<el-form :model="itemForm" label-width="100px">
<el-form-item label="系统商品">
<el-select
v-model="itemForm.product_id"
filterable
remote
clearable
:remote-method="remoteSearchProduct"
:loading="productSearching"
placeholder="输入编码/货物编码/条码/名称搜索(仅启用商品)"
style="width: 100%"
@change="onProductChange"
>
<el-option
v-for="p in productOptions"
:key="p.id"
:label="`${p.item_code} ${p.item_name}`"
:value="p.id"
/>
</el-select>
</el-form-item>
<el-form-item label="商品名称">
<el-input v-model="itemForm.name" placeholder="商品名称" />
</el-form-item>
<el-form-item label="商品编码">
<el-input v-model="itemForm.code" placeholder="选填" />
</el-form-item>
<el-form-item label="规格">
<el-input v-model="itemForm.spec" placeholder="选填" />
</el-form-item>
<el-form-item label="单位">
<el-input v-model="itemForm.unit" placeholder="选填,如 件/箱" />
</el-form-item>
<el-form-item label="数量">
<el-input-number v-model="itemForm.quantity" :min="0" :precision="0" style="width: 100%" />
</el-form-item>
<el-form-item label="单价">
<el-input-number v-model="itemForm.price" :min="0" :precision="2" style="width: 100%" />
</el-form-item>
<el-form-item label="备注">
<el-input v-model="itemForm.remark" type="textarea" :rows="2" placeholder="选填" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="itemDialogVisible = false">取消</el-button>
<el-button type="primary" :loading="itemSubmitting" @click="submitItem">保存</el-button>
</template>
</el-dialog>
</el-card>
</template>
<style scoped>
.stock-card {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
}
.stock-card :deep(.el-card__body) {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
overflow: hidden;
padding: 10px 12px;
}
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
font-weight: 600;
}
.search-bar {
margin-bottom: 12px;
}
.top-table-wrap {
flex: 1;
min-height: 0;
overflow: hidden;
}
.items-toolbar {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 12px;
}
.status-actions {
display: flex;
align-items: center;
gap: 8px;
margin-left: auto;
}
.items-title {
font-weight: 600;
}
.bottom-table-wrap {
flex: 1;
min-height: 0;
overflow: hidden;
}
.detail-tabs {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
}
.detail-tabs :deep(.el-tabs__content) {
flex: 1;
min-height: 0;
overflow: hidden;
}
.detail-tabs :deep(.el-tab-pane) {
height: 100%;
display: flex;
flex-direction: column;
min-height: 0;
}
.fill-table {
height: 100%;
}
</style>