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
@@ -5,16 +5,17 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vites
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import {
createPlatformLocation,
getPlatformLocation,
listPlatformLocations,
type PlatformLocationResource,
} from '../api/platformLocations'
import { listPlatformWarehouses, type PlatformWarehouseResource } from '../../../warehouses/api/platformWarehouses'
import { listAllPlatformWarehouses, type PlatformWarehouseResource } from '../../../warehouses/api/platformWarehouses'
import QueryToolbar, { type QueryField, type QueryModel } from '@/shared/components/QueryToolbar.vue'
import DataTable from '@/shared/components/DataTable.vue'
import LocationsView from './LocationsView.vue'
vi.mock('../../../warehouses/api/platformWarehouses', () => ({ listPlatformWarehouses: vi.fn() }))
vi.mock('../../../warehouses/api/platformWarehouses', () => ({ listAllPlatformWarehouses: vi.fn() }))
vi.mock('../api/platformLocations', () => ({
listPlatformLocations: vi.fn(),
getPlatformLocation: vi.fn(),
@@ -59,6 +60,22 @@ const warehouse: PlatformWarehouseResource = {
remark: null,
}
const fbaWarehouse: PlatformWarehouseResource = {
...warehouse,
id: '9223372036854775802',
warehouseCode: 'FBA-ONT8',
warehouseName: 'ONT8 FBA 仓',
warehouseType: 'FBA',
}
const overseasWarehouse: PlatformWarehouseResource = {
...warehouse,
id: '9223372036854775801',
warehouseCode: 'US-LAX-01',
warehouseName: '洛杉矶海外仓',
warehouseType: 'OVERSEAS',
}
const availableLocation: PlatformLocationResource = {
id: '9223372036854775804',
tenantId,
@@ -124,29 +141,33 @@ describe('LocationsView', () => {
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(listPlatformLocations).mockReset().mockResolvedValue({
records: [availableLocation, occupiedLocation], page: 1, size: 20, total: 2, pages: 1,
})
vi.mocked(getPlatformLocation).mockReset().mockResolvedValue({
location: availableLocation, warehouseCode: warehouse.warehouseCode, warehouseName: warehouse.warehouseName,
})
vi.mocked(createPlatformLocation).mockReset()
})
it('loads all warehouses and queries the full location list without requiring a customer or warehouse', async () => {
it('loads all warehouse candidates for querying while restricting write scope to domestic warehouses', async () => {
const wrapper = await mountView()
const toolbar = wrapper.findComponent(QueryToolbar)
const fields = toolbar.props('fields') as QueryField[]
expect(wrapper.find('[aria-label="所属企业"]').exists()).toBe(false)
expect(fields[0]?.placeholder).toBe('全部仓库')
expect(fields[0]?.options).toEqual([
{ value: warehouseId, label: '义乌收货仓(CN-YW-01)' },
{ value: fbaWarehouse.id, label: 'ONT8 FBA 仓(FBA-ONT8)' },
{ value: overseasWarehouse.id, label: '洛杉矶海外仓(US-LAX-01)' },
])
expect(fields.map(({ key, width }) => [key, width])).toEqual([
['warehouseId', 300], ['zoneCode', 210],
['locationCode', 220], ['locationType', 170], ['status', 160],
])
expect(listPlatformWarehouses).toHaveBeenCalledWith(1, 200)
expect(listAllPlatformWarehouses).toHaveBeenCalledWith()
expect(listPlatformLocations).toHaveBeenCalledWith({
page: 1,
size: 20,
@@ -175,7 +196,7 @@ describe('LocationsView', () => {
expect(panelHeading.text()).toContain('共 2 个库位')
expect(panelHeading.text()).toContain('新增库位')
expect(wrapper.findComponent(QueryToolbar).text()).not.toContain('新增库位')
expect(listPlatformWarehouses).toHaveBeenCalledWith(1, 200)
expect(listAllPlatformWarehouses).toHaveBeenCalledWith()
expect(listPlatformLocations).toHaveBeenLastCalledWith({
page: 1,
size: 20,
@@ -202,7 +223,23 @@ describe('LocationsView', () => {
expect(document.body.textContent).toContain('新增库位')
expect(document.body.textContent).toContain('所属仓库')
expect(document.body.textContent).toContain('请选择所属仓库')
expect(document.body.textContent).toContain('请选择国内仓')
})
it('does not use an FBA warehouse filter as the creation scope', async () => {
const wrapper = await mountView()
const toolbar = wrapper.findComponent(QueryToolbar)
toolbar.vm.$emit('update:modelValue', { ...(toolbar.props('modelValue') as QueryModel), warehouseId: fbaWarehouse.id })
await flushPromises()
const createButton = wrapper.find('.table-panel .panel-heading').findAll('button').find((button) => button.text().trim() === '新增库位')
await createButton!.trigger('click')
await flushPromises()
const saveButton = [...document.body.querySelectorAll('button')].find((button) => button.textContent?.trim() === '保存库位')
saveButton?.dispatchEvent(new MouseEvent('click', { bubbles: true }))
await flushPromises()
expect(createPlatformLocation).not.toHaveBeenCalled()
})
it('keeps the operation column first and hides edit actions for occupied resources', async () => {
@@ -218,6 +255,18 @@ describe('LocationsView', () => {
expect(bodyRows[1]?.text()).not.toContain('更多')
})
it('does not offer editing for a location outside a domestic warehouse', async () => {
vi.mocked(listPlatformLocations).mockResolvedValue({
records: [{ ...availableLocation, warehouseId: fbaWarehouse.id }, availableLocation], page: 1, size: 20, total: 2, pages: 1,
})
const wrapper = await mountView()
const bodyRows = wrapper.findAll('tbody tr')
expect(bodyRows[0]?.text()).toContain('详情')
expect(bodyRows[0]?.text()).not.toContain('编辑')
expect(bodyRows[1]?.text()).toContain('编辑')
})
it('opens an authenticated detail view with warehouse context', async () => {
const wrapper = await mountView()
const detailButton = wrapper.find('.el-table__body-wrapper tbody tr button')
@@ -23,7 +23,7 @@ import {
type PlatformLocationType,
type SavePlatformLocationRequest,
} from '../api/platformLocations'
import { listPlatformWarehouses, type PlatformWarehouseResource } from '../../../warehouses/api/platformWarehouses'
import { listAllPlatformWarehouses, type PlatformWarehouseResource } from '../../../warehouses/api/platformWarehouses'
type DrawerMode = 'view' | 'create' | 'edit'
type LocationFilters = QueryModel & {
@@ -119,7 +119,7 @@ const emptyForm = () => ({
})
const form = reactive(emptyForm())
const rules: FormRules<typeof form> = {
warehouseId: [{ required: true, message: '请选择所属仓库', trigger: 'change' }],
warehouseId: [{ required: true, message: '请选择国内仓', trigger: 'change' }],
zoneCode: [{ required: true, message: '请输入库区编码', trigger: 'blur' }],
zoneName: [{ required: true, message: '请输入库区名称', trigger: 'blur' }],
locationCode: [
@@ -137,6 +137,9 @@ const rules: FormRules<typeof form> = {
}
const selectedWarehouse = 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 editorOpen = computed(() => drawerOpen.value && drawerMode.value !== 'view')
const editorTitle = computed(() => drawerMode.value === 'create' ? '新增库位' : '编辑库位')
const drawerTitle = computed(() => drawerMode.value === 'view' ? '库位详情' : editorTitle.value)
@@ -197,6 +200,7 @@ function formatWeight(value: number | string | null) {
function canEdit(row: PlatformLocationResource) {
return access.can('platform:location:edit')
&& isDomesticWarehouse(row.warehouseId)
&& !row.sharedOverflow
&& (row.status === 'AVAILABLE' || row.status === 'DISABLED')
}
@@ -216,9 +220,20 @@ function actionError(error: unknown, fallback: string) {
ElMessage.error(details.requestId ? `${details.message}(requestId: ${details.requestId})` : details.message)
}
function isDomesticWarehouse(warehouseId: number | string) {
return warehouses.value.some((warehouse) => (
String(warehouse.id) === String(warehouseId)
&& warehouse.warehouseType === 'DOMESTIC'
))
}
function isActiveDomesticWarehouse(warehouseId: number | string) {
return domesticWarehouses.value.some((warehouse) => String(warehouse.id) === String(warehouseId))
}
function requireCreateScope() {
if (!selectedWarehouse.value || !form.warehouseId) {
ElMessage.warning('请选择所属仓库')
ElMessage.warning('请选择国内仓')
return null
}
return { customerId: apiId(String(selectedWarehouse.value.tenantId)), warehouseId: apiId(form.warehouseId) }
@@ -234,9 +249,9 @@ async function loadWarehouses() {
warehouseError.value = null
warehouses.value = []
try {
const result = await listPlatformWarehouses(1, 200)
const records = await listAllPlatformWarehouses()
if (sequence !== warehouseLoadSequence) return
warehouses.value = result.records
warehouses.value = records
} catch (error) {
if (sequence !== warehouseLoadSequence) return
warehouseError.value = getApiErrorDetails(error, '仓库列表加载失败,请稍后重试')
@@ -304,7 +319,7 @@ function assignForm(resource: PlatformLocationResource) {
function openCreate() {
Object.assign(form, emptyForm())
form.warehouseId = selectedWarehouseId.value
form.warehouseId = isActiveDomesticWarehouse(selectedWarehouseId.value) ? selectedWarehouseId.value : ''
form.customerId = selectedWarehouse.value ? String(selectedWarehouse.value.tenantId) : ''
form.status = selectedWarehouse.value?.status === 'INACTIVE' ? 'DISABLED' : 'AVAILABLE'
detail.value = null
@@ -316,6 +331,10 @@ function openCreate() {
}
async function loadDetail(row: PlatformLocationResource, mode: 'view' | 'edit') {
if (mode === 'edit' && !isDomesticWarehouse(row.warehouseId)) {
ElMessage.warning('仅支持编辑国内仓库位')
return
}
const scope = recordScope(row)
drawerMode.value = mode
drawerOpen.value = true
@@ -355,6 +374,13 @@ function buildPayload(): SavePlatformLocationRequest {
async function saveLocation() {
const valid = await formRef.value?.validate().catch(() => false)
if (!valid) return
const warehouseAllowed = form.id
? isDomesticWarehouse(form.warehouseId)
: isActiveDomesticWarehouse(form.warehouseId)
if (!warehouseAllowed) {
ElMessage.error(form.id ? '库位仅支持归属国内仓' : '新增库位仅支持选择启用的国内仓')
return
}
const scope = form.id
? { customerId: apiId(form.customerId), warehouseId: apiId(form.warehouseId) }
: requireCreateScope()
@@ -479,8 +505,8 @@ onMounted(() => {
<ApiErrorMeta :details="warehouseError" />
<el-button size="small" @click="loadWarehouses()">重新加载仓库</el-button>
</el-alert>
<el-alert v-else-if="!warehouseLoading && warehouses.length === 0" type="info" :closable="false" show-icon title="暂无可选仓库">
请先在仓库管理中建立仓库,再维护库位。
<el-alert v-else-if="!warehouseLoading && domesticWarehouses.length === 0" type="info" :closable="false" show-icon title="暂无可选国内仓">
请先在仓库管理中建立国内仓,再维护库位。
</el-alert>
<el-alert v-if="loadError" type="error" :closable="false" show-icon class="state-alert">
<template #title>库位列表加载失败</template>
@@ -650,9 +676,9 @@ onMounted(() => {
<div><strong>{{ detail?.warehouseName || selectedWarehouse?.warehouseName }}({{ detail?.warehouseCode || selectedWarehouse?.warehouseCode }})</strong><span>平台公共资源 · 保存后不可跨仓移动</span></div>
</div>
<el-form-item v-if="drawerMode === 'create'" 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)"