This commit is contained in:
2026-08-06 14:55:17 +08:00
parent a813426588
commit 4ac0adac62
18 changed files with 379 additions and 352 deletions
@@ -16,9 +16,10 @@ vi.mock('@/shared/api/http', () => ({
const payload: SavePlatformPalletRequest = {
warehouseId: '19001',
palletCode: ' TP-001 ',
palletType: '木托盘',
capacityCbm: 1.2,
weightKg: 8,
color: '黄色',
remark: null,
}
@@ -7,6 +7,8 @@ export interface PlatformPalletResource {
palletCode: string
palletType: string
capacityCbm: number | string
weightKg: number | string | null
color: string | null
remark: string | null
createdAt: string | null
updatedAt: string | null
@@ -31,9 +33,10 @@ export interface PlatformPalletQuery {
export interface SavePlatformPalletRequest {
warehouseId: number | string
palletCode: string
palletType: string
capacityCbm: number
weightKg: number
color: string | null
remark: string | null
}
@@ -4,7 +4,7 @@ import { enableAutoUnmount, flushPromises, mount, type VueWrapper } from '@vue/t
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import { listPlatformWarehouses, type PlatformWarehouseResource } from '@/modules/warehouses'
import { listAllPlatformWarehouses, type PlatformWarehouseResource } from '@/modules/warehouses'
import DataTable from '@/shared/components/DataTable.vue'
import QueryToolbar from '@/shared/components/QueryToolbar.vue'
import {
@@ -15,7 +15,7 @@ import {
} from '../api/platformPallets'
import PalletsView from './PalletsView.vue'
vi.mock('@/modules/warehouses', () => ({ listPlatformWarehouses: vi.fn() }))
vi.mock('@/modules/warehouses', () => ({ listAllPlatformWarehouses: vi.fn() }))
vi.mock('../api/platformPallets', () => ({
listPlatformPallets: vi.fn(),
getPlatformPallet: vi.fn(),
@@ -48,9 +48,25 @@ const warehouse: PlatformWarehouseResource = {
contactPhone: null, contactEmail: null, status: 'ACTIVE', remark: null,
}
const fbaWarehouse: PlatformWarehouseResource = {
...warehouse,
id: '19002',
warehouseCode: 'FBA-ONT8',
warehouseName: 'ONT8 FBA 仓',
warehouseType: 'FBA',
}
const overseasWarehouse: PlatformWarehouseResource = {
...warehouse,
id: '19003',
warehouseCode: 'US-LAX-01',
warehouseName: '洛杉矶海外仓',
warehouseType: 'OVERSEAS',
}
const pallet: PlatformPalletResource = {
id: '20001', tenantId: 0, warehouseId: '19001', palletCode: 'TP-001', palletType: '木托盘',
capacityCbm: '1.200000', remark: '标准川字托盘', createdAt: '2026-08-04T09:00:00', updatedAt: '2026-08-04T10:00:00',
capacityCbm: '1.200000', weightKg: '8.000', color: '黄色', remark: '标准川字托盘', createdAt: '2026-08-04T09:00:00', updatedAt: '2026-08-04T10:00:00',
}
async function mountView(permissions = [
@@ -95,13 +111,13 @@ describe('PalletsView', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
document.body.innerHTML = ''
vi.mocked(listPlatformWarehouses).mockReset().mockResolvedValue({ records: [warehouse], page: 1, size: 200, total: 1, pages: 1 })
vi.mocked(listAllPlatformWarehouses).mockReset().mockResolvedValue([warehouse, fbaWarehouse, overseasWarehouse])
vi.mocked(listPlatformPallets).mockReset().mockResolvedValue({ records: [pallet], page: 1, size: 20, total: 1, pages: 1 })
vi.mocked(getPlatformPallet).mockReset().mockResolvedValue(pallet)
vi.mocked(createPlatformPallet).mockReset().mockResolvedValue('20002')
})
it('starts with all customers and all warehouses using compact filters', async () => {
it('starts with all warehouse candidates for querying using compact filters', async () => {
const wrapper = await mountView()
const toolbar = wrapper.findComponent(QueryToolbar)
const fields = toolbar.props('fields')
@@ -114,7 +130,12 @@ describe('PalletsView', () => {
])
expect(wrapper.find('.pallet-filter-toolbar').exists()).toBe(true)
expect(fields.find((field) => field.key === 'warehouseId')?.placeholder).toBe('全部仓库')
expect(listPlatformWarehouses).toHaveBeenCalledWith(1, 200)
expect(fields.find((field) => field.key === 'warehouseId')?.options).toEqual([
{ label: '义乌收货仓(CN-YW-01)', value: '19001' },
{ label: 'ONT8 FBA 仓(FBA-ONT8)', value: '19002' },
{ label: '洛杉矶海外仓(US-LAX-01)', value: '19003' },
])
expect(listAllPlatformWarehouses).toHaveBeenCalledWith()
expect(listPlatformPallets).toHaveBeenCalledWith({
page: 1, size: 20,
})
@@ -124,8 +145,8 @@ describe('PalletsView', () => {
const wrapper = await mountView()
await loadRows(wrapper)
expect(listPlatformWarehouses).toHaveBeenCalledTimes(1)
expect(listPlatformWarehouses).toHaveBeenCalledWith(1, 200)
expect(listAllPlatformWarehouses).toHaveBeenCalledTimes(1)
expect(listAllPlatformWarehouses).toHaveBeenCalledWith()
expect(listPlatformPallets).toHaveBeenCalledWith({
page: 1, size: 20,
})
@@ -179,8 +200,24 @@ describe('PalletsView', () => {
const wrapper = await mountView()
const warehouseField = wrapper.findComponent(QueryToolbar).props('fields').find((field) => field.key === 'warehouseId')
expect(listPlatformWarehouses).toHaveBeenCalledTimes(1)
expect(warehouseField?.options).toEqual([{ label: '义乌收货仓(CN-YW-01)', value: '19001' }])
expect(listAllPlatformWarehouses).toHaveBeenCalledTimes(1)
expect(warehouseField?.options).toEqual([
{ label: '义乌收货仓(CN-YW-01)', value: '19001' },
{ label: 'ONT8 FBA 仓(FBA-ONT8)', value: '19002' },
{ label: '洛杉矶海外仓(US-LAX-01)', value: '19003' },
])
})
it('does not offer editing for a pallet outside a domestic warehouse', async () => {
vi.mocked(listPlatformPallets).mockResolvedValue({
records: [{ ...pallet, id: '20002', warehouseId: fbaWarehouse.id }, pallet], page: 1, size: 20, total: 2, pages: 1,
})
const wrapper = await mountView()
const bodyRows = wrapper.findAll('.el-table__body-wrapper tbody tr')
expect(bodyRows[0]?.text()).toContain('查看')
expect(bodyRows[0]?.text()).not.toContain('编辑')
expect(bodyRows[1]?.text()).toContain('编辑')
})
it('applies pallet filters only after search', async () => {
@@ -206,18 +243,19 @@ describe('PalletsView', () => {
await createButton?.trigger('click')
await flushPromises()
await wrapper.find('input[placeholder="例如 TP-001"]').setValue('TP-002')
const palletTypeItem = wrapper.findAll('.el-form-item').find((item) => item.text().includes('托盘类型'))
palletTypeItem?.findComponent({ name: 'ElSelect' }).vm.$emit('update:modelValue', '塑料托盘')
const capacity = wrapper.findComponent({ name: 'ElInputNumber' })
capacity.vm.$emit('update:modelValue', 1.5)
await wrapper.find('textarea[placeholder="可填写规格、材质或现场识别说明"]').setValue('蓝色标准托盘')
const inputNumbers = wrapper.findAllComponents({ name: 'ElInputNumber' })
inputNumbers[0]?.vm.$emit('update:modelValue', 1.5)
inputNumbers[1]?.vm.$emit('update:modelValue', 10)
await wrapper.find('input[placeholder="例如 黄色"]').setValue('黑色')
await wrapper.find('textarea[placeholder="可填写其他说明"]').setValue('标准托盘')
const saveButton = wrapper.findAll('button').find((button) => button.text().trim() === '保存托盘')
await saveButton?.trigger('click')
await flushPromises()
expect(createPlatformPallet).toHaveBeenCalledWith(0, {
warehouseId: 19001, palletCode: 'TP-002', palletType: '塑料托盘', capacityCbm: 1.5, remark: '蓝色标准托盘',
warehouseId: 19001, palletType: '塑料托盘', capacityCbm: 1.5, weightKg: 10, color: '黑色', remark: '标准托盘',
})
})
@@ -264,7 +302,7 @@ describe('PalletsView', () => {
expect(wrapper.text()).not.toContain('客户列表暂时无法加载')
expect(wrapper.text()).not.toContain('所属企业')
expect(listPlatformWarehouses).toHaveBeenCalledWith(1, 200)
expect(listAllPlatformWarehouses).toHaveBeenCalledWith()
expect(listPlatformPallets).toHaveBeenCalledWith({
page: 1, size: 20,
})
@@ -4,7 +4,7 @@ import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'elem
import { Boxes, Eye, Pencil, Plus, Trash2, Warehouse as WarehouseIcon } from '@lucide/vue'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useUnsavedWorkRegistration } from '@/core/unsaved-work/useUnsavedWorkRegistration'
import { listPlatformWarehouses, type PlatformWarehouseResource } from '@/modules/warehouses'
import { listAllPlatformWarehouses, type PlatformWarehouseResource } from '@/modules/warehouses'
import { getApiErrorDetails, type ApiErrorDetails } from '@/shared/api/http'
import DataTable, { type DataTableColumn } from '@/shared/components/DataTable.vue'
import LoadErrorState from '@/shared/components/LoadErrorState.vue'
@@ -71,14 +71,10 @@ const queryFields = computed<QueryField[]>(() => [
},
])
const emptyForm = () => ({ id: '', customerId: '', warehouseId: '', palletCode: '', palletType: '', capacityCbm: null as number | null, remark: '' })
const emptyForm = () => ({ id: '', customerId: '', warehouseId: '', palletCode: '', palletType: '', capacityCbm: null as number | null, weightKg: null as number | null, color: '', remark: '' })
const form = reactive(emptyForm())
const rules: FormRules<typeof form> = {
warehouseId: [{ required: true, message: '请选择所属仓库', trigger: 'change' }],
palletCode: [
{ required: true, message: '请输入托盘编码', trigger: 'blur' },
{ max: 50, message: '托盘编码不能超过 50 个字符', trigger: 'blur' },
],
warehouseId: [{ required: true, message: '请选择国内仓', trigger: 'change' }],
palletType: [
{ required: true, message: '请输入托盘类型', trigger: 'blur' },
{ max: 32, message: '托盘类型不能超过 32 个字符', trigger: 'blur' },
@@ -90,10 +86,24 @@ const rules: FormRules<typeof form> = {
},
trigger: 'change',
}],
weightKg: [{
validator: (_rule, value, callback) => {
if (typeof value !== 'number' || !Number.isFinite(value) || value <= 0) callback(new Error('请输入大于 0 的托盘重量'))
else callback()
},
trigger: 'change',
}],
color: [
{ required: true, message: '请输入托盘颜色', trigger: 'blur' },
{ max: 32, message: '托盘颜色不能超过 32 个字符', trigger: 'blur' },
],
remark: [{ max: 500, message: '备注不能超过 500 个字符', trigger: 'blur' }],
}
const editorWarehouse = computed(() => warehouses.value.find((item) => String(item.id) === form.warehouseId) || null)
const domesticWarehouses = computed(() => warehouses.value.filter((warehouse) => (
warehouse.warehouseType === 'DOMESTIC' && warehouse.status === 'ACTIVE'
)))
const editorTitle = computed(() => form.id ? '编辑托盘' : '新增托盘')
const editorPermission = computed(() => form.id ? 'platform:pallet:edit' : 'platform:pallet:create')
const contextLocked = computed(() => editorOpen.value || detailOpen.value || saving.value || deletingIds.value.size > 0)
@@ -112,9 +122,9 @@ const columns: DataTableColumn[] = [
{ key: 'pallet', label: '托盘编码', minWidth: 190, slotName: 'pallet' },
{ prop: 'palletType', label: '托盘类型', minWidth: 140 },
{ key: 'capacity', label: '托盘容积(m³)', minWidth: 160, slotName: 'capacity' },
{ key: 'warehouse', label: '所属仓库', minWidth: 210, slotName: 'warehouse' },
{ key: 'weight', label: '托盘重量(kg)', minWidth: 160, slotName: 'weight' },
{ prop: 'color', label: '颜色', minWidth: 120 },
{ key: 'remark', label: '备注', minWidth: 220, slotName: 'remark' },
{ prop: 'updatedAt', label: '更新时间', minWidth: 160, formatter: (row: PlatformPalletResource) => formatDateTime(row.updatedAt) },
]
function apiId(value: string) {
@@ -132,10 +142,25 @@ function formatCapacity(value: number | string) {
return `${numeric.toLocaleString('zh-CN', { maximumFractionDigits: 6 })} m³`
}
function formatWeight(value: number | string | null) {
if (value === null) return '—'
const numeric = Number(value)
if (!Number.isFinite(numeric)) return String(value)
return `${numeric.toLocaleString('zh-CN', { maximumFractionDigits: 3 })} kg`
}
function warehouseFor(warehouseId: number | string) {
return warehouses.value.find((item) => String(item.id) === String(warehouseId)) || null
}
function isDomesticWarehouse(warehouseId: number | string) {
return warehouseFor(warehouseId)?.warehouseType === 'DOMESTIC'
}
function isActiveDomesticWarehouse(warehouseId: number | string) {
return domesticWarehouses.value.some((warehouse) => String(warehouse.id) === String(warehouseId))
}
function errorMessage(details: ApiErrorDetails) {
return details.requestId ? `${details.message}(requestId: ${details.requestId})` : details.message
}
@@ -146,9 +171,9 @@ async function loadWarehouses() {
warehouseError.value = null
warehouseLoading.value = true
try {
const result = await listPlatformWarehouses(1, 200)
const records = await listAllPlatformWarehouses()
if (sequence !== warehouseSequence) return
warehouses.value = result.records
warehouses.value = records
} catch (error) {
if (sequence !== warehouseSequence) return
warehouseError.value = getApiErrorDetails(error, '仓库列表加载失败,请稍后重试')
@@ -200,7 +225,7 @@ function resetFilters() {
function openCreate() {
Object.assign(form, emptyForm())
form.warehouseId = filters.value.warehouseId
form.warehouseId = isActiveDomesticWarehouse(filters.value.warehouseId) ? filters.value.warehouseId : ''
form.customerId = editorWarehouse.value ? String(editorWarehouse.value.tenantId) : ''
editorOpen.value = true
captureBaseline()
@@ -215,6 +240,8 @@ function assignForm(resource: PlatformPalletResource) {
palletCode: resource.palletCode,
palletType: resource.palletType,
capacityCbm: Number(resource.capacityCbm),
weightKg: resource.weightKg === null ? null : Number(resource.weightKg),
color: resource.color || '',
remark: resource.remark || '',
})
editorOpen.value = true
@@ -241,6 +268,10 @@ async function openDetail(row: PlatformPalletResource) {
}
async function openEdit(row: PlatformPalletResource) {
if (!isDomesticWarehouse(row.warehouseId)) {
ElMessage.warning('仅支持编辑国内仓托盘')
return
}
try {
assignForm(await fetchDetail(row))
} catch (error) {
@@ -249,19 +280,23 @@ async function openEdit(row: PlatformPalletResource) {
}
async function savePallet() {
if (!await formRef.value?.validate().catch(() => false) || form.capacityCbm === null) return
if (!await formRef.value?.validate().catch(() => false) || form.capacityCbm === null || form.weightKg === null) return
const warehouse = editorWarehouse.value
if (!warehouse) {
ElMessage.error('所选仓库已不存在,请重新选择')
const warehouseAllowed = form.id
? warehouse?.warehouseType === 'DOMESTIC'
: warehouse?.warehouseType === 'DOMESTIC' && warehouse.status === 'ACTIVE'
if (!warehouse || !warehouseAllowed) {
ElMessage.error(form.id ? '托盘仅支持归属国内仓' : '新增托盘仅支持选择启用的国内仓')
return
}
form.customerId = String(warehouse.tenantId)
saving.value = true
const payload: SavePlatformPalletRequest = {
warehouseId: apiId(form.warehouseId),
palletCode: form.palletCode.trim(),
palletType: form.palletType.trim(),
capacityCbm: form.capacityCbm,
weightKg: form.weightKg,
color: form.color.trim() || null,
remark: form.remark.trim() || null,
}
try {
@@ -364,7 +399,7 @@ onMounted(() => {
<template #actions="{ row }">
<div class="table-actions pallet-row-actions">
<el-button link type="primary" title="查看托盘" @click="openDetail(row as PlatformPalletResource)"><Eye :size="15" />查看</el-button>
<el-button v-if="access.can('platform:pallet:edit')" link type="primary" title="编辑托盘" @click="openEdit(row as PlatformPalletResource)"><Pencil :size="15" />编辑</el-button>
<el-button v-if="access.can('platform:pallet:edit') && isDomesticWarehouse((row as PlatformPalletResource).warehouseId)" link type="primary" title="编辑托盘" @click="openEdit(row as PlatformPalletResource)"><Pencil :size="15" />编辑</el-button>
<el-button
v-if="access.can('platform:pallet:delete')"
link
@@ -379,7 +414,7 @@ onMounted(() => {
<div class="pallet-cell"><span><Boxes :size="18" /></span><strong>{{ (row as PlatformPalletResource).palletCode }}</strong></div>
</template>
<template #capacity="{ row }"><strong>{{ formatCapacity((row as PlatformPalletResource).capacityCbm) }}</strong></template>
<template #warehouse="{ row }"><span>{{ warehouseFor((row as PlatformPalletResource).warehouseId)?.warehouseName || '—' }}</span></template>
<template #weight="{ row }"><strong>{{ formatWeight((row as PlatformPalletResource).weightKg) }}</strong></template>
<template #remark="{ row }"><span class="remark-text">{{ (row as PlatformPalletResource).remark || '—' }}</span></template>
</DataTable>
</section>
@@ -398,6 +433,8 @@ onMounted(() => {
<el-descriptions-item label="托盘编码">{{ detail.palletCode }}</el-descriptions-item>
<el-descriptions-item label="托盘类型">{{ detail.palletType }}</el-descriptions-item>
<el-descriptions-item label="托盘容积">{{ formatCapacity(detail.capacityCbm) }}</el-descriptions-item>
<el-descriptions-item label="托盘重量">{{ formatWeight(detail.weightKg) }}</el-descriptions-item>
<el-descriptions-item label="颜色">{{ detail.color || '—' }}</el-descriptions-item>
<el-descriptions-item label="备注">{{ detail.remark || '—' }}</el-descriptions-item>
</el-descriptions>
<h3 class="detail-section-title">记录信息</h3>
@@ -430,9 +467,9 @@ onMounted(() => {
<div><strong>{{ editorWarehouse?.warehouseName || '当前仓库' }}</strong><span>{{ editorWarehouse?.warehouseCode || form.warehouseId }} · 平台公共资源</span></div>
</div>
<el-form-item v-else label="所属仓库" prop="warehouseId" required>
<el-select v-model="form.warehouseId" filterable placeholder="请选择所属仓库" :loading="warehouseLoading">
<el-select v-model="form.warehouseId" filterable placeholder="请选择国内仓" :loading="warehouseLoading">
<el-option
v-for="warehouse in warehouses"
v-for="warehouse in domesticWarehouses"
:key="String(warehouse.id)"
:label="`${warehouse.warehouseName}(${warehouse.warehouseCode})`"
:value="String(warehouse.id)"
@@ -444,11 +481,11 @@ onMounted(() => {
<section class="editor-section" aria-labelledby="pallet-spec-heading">
<div class="editor-section-heading">
<Boxes :size="18" aria-hidden="true" />
<div><h3 id="pallet-spec-heading">托盘规格</h3><p>完善编码、类型与实际可用容积。</p></div>
<div><h3 id="pallet-spec-heading">托盘规格</h3><p>编码由系统自动生成,请维护类型、容积、重量和颜色。</p></div>
</div>
<div class="editor-field-grid">
<el-form-item label="托盘编码" prop="palletCode" required>
<el-input v-model="form.palletCode" maxlength="50" show-word-limit placeholder="例如 TP-001" />
<el-form-item label="托盘编码">
<el-input :model-value="form.palletCode || '保存后自动生成'" disabled aria-label="托盘编码(系统生成)" />
</el-form-item>
<el-form-item label="托盘类型" prop="palletType" required>
<el-select
@@ -463,12 +500,19 @@ onMounted(() => {
</el-select>
</el-form-item>
</div>
<el-form-item label="托盘容积(m³)" prop="capacityCbm" required>
<el-input-number v-model="form.capacityCbm" :min="0.000001" :max="999999999999.999999" :precision="6" :step="0.1" controls-position="right" aria-label="标准容积" />
<p class="field-help">用于服务端按总体积计算所需托盘数量,请填写托盘实际可用容积。</p>
<div class="editor-field-grid">
<el-form-item label="托盘容积(m³)" prop="capacityCbm" required>
<el-input-number v-model="form.capacityCbm" :min="0.000001" :max="999999999999.999999" :precision="6" :step="0.1" controls-position="right" aria-label="托盘容积" />
</el-form-item>
<el-form-item label="托盘重量(kg)" prop="weightKg" required>
<el-input-number v-model="form.weightKg" :min="0.001" :max="999999999999999.999" :precision="3" :step="0.5" controls-position="right" aria-label="托盘重量" />
</el-form-item>
</div>
<el-form-item label="颜色" prop="color" required>
<el-input v-model="form.color" maxlength="32" show-word-limit placeholder="例如 黄色" />
</el-form-item>
<el-form-item label="备注" prop="remark" class="editor-remark-field">
<el-input v-model="form.remark" type="textarea" :rows="4" maxlength="500" show-word-limit placeholder="可填写规格、材质或现场识别说明" />
<el-input v-model="form.remark" type="textarea" :rows="4" maxlength="500" show-word-limit placeholder="可填写其他说明" />
</el-form-item>
</section>
</el-form>