This commit is contained in:
2026-08-04 09:05:19 +08:00
parent fa922ca96f
commit 815d4e8daf
21 changed files with 994 additions and 128 deletions
@@ -51,7 +51,7 @@ export interface PlatformExpressCompanyImportResult {
const basePath = '/platform/v1/express-companies'
export async function listPlatformExpressCompanies(
tenantId: number,
tenantId: number | string,
page = 1,
size = 20,
keyword = '',
@@ -76,7 +76,7 @@ export async function listPlatformExpressCompanies(
}
}
export async function getPlatformExpressCompany(tenantId: number, id: number | string) {
export async function getPlatformExpressCompany(tenantId: number | string, id: number | string) {
const response = await http.get<ApiEnvelope<PlatformExpressCompanyResource>, ApiEnvelope<PlatformExpressCompanyResource>>(
`${basePath}/${id}`,
{ params: { tenantId } },
@@ -84,7 +84,7 @@ export async function getPlatformExpressCompany(tenantId: number, id: number | s
return unwrapApiResponse(response)
}
export async function createPlatformExpressCompany(tenantId: number, payload: SavePlatformExpressCompanyRequest) {
export async function createPlatformExpressCompany(tenantId: number | string, payload: SavePlatformExpressCompanyRequest) {
const response = await http.post<ApiEnvelope<number | string>, ApiEnvelope<number | string>>(basePath, payload, {
params: { tenantId },
})
@@ -92,7 +92,7 @@ export async function createPlatformExpressCompany(tenantId: number, payload: Sa
}
export async function updatePlatformExpressCompany(
tenantId: number,
tenantId: number | string,
id: number | string,
payload: SavePlatformExpressCompanyRequest,
) {
@@ -103,7 +103,7 @@ export async function updatePlatformExpressCompany(
}
export async function changePlatformExpressCompanyStatus(
tenantId: number,
tenantId: number | string,
id: number | string,
status: PlatformExpressCompanyStatus,
) {
@@ -117,7 +117,7 @@ export async function downloadPlatformExpressCompanyImportTemplate() {
return http.get<Blob, Blob>(`${basePath}/import-template`, { responseType: 'blob' })
}
export async function importPlatformExpressCompanies(tenantId: number, file: File) {
export async function importPlatformExpressCompanies(tenantId: number | string, file: File) {
const formData = new FormData()
formData.append('file', file)
const response = await http.post<ApiEnvelope<PlatformExpressCompanyImportResult>, ApiEnvelope<PlatformExpressCompanyImportResult>>(
@@ -5,6 +5,8 @@ import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vites
import { listPlatformExpressCompanies, type PlatformExpressCompanyResource } from '@/modules/master-data/express-companies/api/platformExpressCompanies'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { useAuthStore } from '@/core/auth/auth'
import { listPlatformTenants, type PlatformTenantResource } from '../../../iam/api/platformIam'
import PlatformTenantSelector from '../../../iam/components/PlatformTenantSelector.vue'
import ExpressCompaniesView from './ExpressCompaniesView.vue'
vi.mock('@/modules/master-data/express-companies/api/platformExpressCompanies', () => ({
@@ -14,6 +16,8 @@ vi.mock('@/modules/master-data/express-companies/api/platformExpressCompanies',
changePlatformExpressCompanyStatus: vi.fn(),
}))
vi.mock('../../../iam/api/platformIam', () => ({ listPlatformTenants: vi.fn() }))
class ResizeObserverStub implements ResizeObserver {
observe() {}
unobserve() {}
@@ -45,6 +49,16 @@ const company: PlatformExpressCompanyResource = {
updatedAt: '2026-07-31T10:20:00',
}
const tenant = (id: number | string, tenantName: string): PlatformTenantResource => ({
id, tenantCode: String(id), tenantName, status: 'ACTIVE', legalName: tenantName,
legalRepresentative: null, unifiedSocialCreditCode: null, contactName: null,
contactPhone: null, contactEmail: null, servicePackage: null, contactWechat: null,
salesOwnerName: null, paymentTermDays: null, enterpriseType: null, establishedDate: null,
registeredCapital: null, registeredCapitalCurrency: null, registeredAddress: null,
internalRemark: null, createdAt: null, approvedAt: null,
})
const snowflakeTenantId = '9223372036854775806'
async function mountView() {
vi.stubGlobal('localStorage', createMemoryStorage())
vi.stubGlobal('sessionStorage', createMemoryStorage())
@@ -69,6 +83,9 @@ describe('ExpressCompaniesView query and table migration', () => {
beforeEach(() => {
vi.stubGlobal('ResizeObserver', ResizeObserverStub)
document.body.innerHTML = ''
vi.mocked(listPlatformTenants).mockReset().mockResolvedValue({
records: [tenant(10001, '默认企业'), tenant(snowflakeTenantId, '雪花企业')], page: 1, size: 200, total: 2, pages: 1,
})
vi.mocked(listPlatformExpressCompanies).mockReset().mockResolvedValue({
records: [company], page: 1, size: 20, total: 1, pages: 1,
})
@@ -95,4 +112,13 @@ describe('ExpressCompaniesView query and table migration', () => {
expect(listPlatformExpressCompanies).toHaveBeenLastCalledWith(10001, 1, 20, '顺丰', '')
})
it('preserves a snowflake tenant id as a string when switching enterprises', async () => {
const wrapper = await mountView()
wrapper.findComponent(PlatformTenantSelector).vm.$emit('update:modelValue', snowflakeTenantId)
await flushPromises()
expect(listPlatformExpressCompanies).toHaveBeenLastCalledWith(snowflakeTenantId, 1, 20, '', '')
})
})
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { computed, reactive, ref, watch } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { Download, ExternalLink, FileSpreadsheet, Pencil, Plus, Power, Truck, Upload } from '@lucide/vue'
import ApiErrorMeta from '@/shared/components/ApiErrorMeta.vue'
@@ -21,8 +21,8 @@ import {
type SavePlatformExpressCompanyRequest,
} from '@/modules/master-data/express-companies/api/platformExpressCompanies'
import { useAccessPreviewStore } from '@/core/access/accessPreview'
import { PlatformTenantSelector } from '@/modules/iam'
const fixedTenantId = 10001
const access = useAccessPreviewStore()
const loading = ref(false)
const saving = ref(false)
@@ -34,6 +34,8 @@ const rows = ref<PlatformExpressCompanyResource[]>([])
const page = ref(1)
const pageSize = ref(20)
const total = ref(0)
const selectedTenantId = ref('')
const loadedTenantId = ref<string | null>(null)
const loadError = ref<ApiErrorDetails | null>(null)
const statusLoadingIds = ref(new Set<string>())
const editLoadingIds = ref(new Set<string>())
@@ -91,6 +93,16 @@ const { captureBaseline } = useUnsavedWorkRegistration({
requiredPermission: editorPermission,
})
const tenantReady = computed(() => Boolean(selectedTenantId.value)
&& loadedTenantId.value === selectedTenantId.value
&& !loading.value)
const tenantSwitchDisabled = computed(() => editorOpen.value
|| importOpen.value
|| saving.value
|| importing.value
|| editLoadingIds.value.size > 0
|| statusLoadingIds.value.size > 0)
watch(
() => access.can(editorPermission.value),
(canWrite) => {
@@ -131,13 +143,27 @@ const expressCompanyColumns: DataTableColumn[] = [
{ key: 'status', label: '状态', width: 100, slotName: 'status' },
]
async function loadExpressCompanies() {
function apiTenantId(value: string) {
const numeric = Number(value)
return Number.isSafeInteger(numeric) ? numeric : value
}
function requireCurrentTenant() {
if (!tenantReady.value) {
ElMessage.warning('快递公司数据尚未加载完成,请稍后重试')
return null
}
return apiTenantId(selectedTenantId.value)
}
async function loadExpressCompanies(tenantId = selectedTenantId.value) {
if (!tenantId) return
const sequence = ++loadSequence
loading.value = true
loadError.value = null
try {
const result = await listPlatformExpressCompanies(
fixedTenantId,
apiTenantId(tenantId),
page.value,
pageSize.value,
appliedFilters.value.keyword,
@@ -146,6 +172,7 @@ async function loadExpressCompanies() {
if (sequence !== loadSequence) return
rows.value = result.records
total.value = result.total
loadedTenantId.value = tenantId
} catch (error) {
if (sequence !== loadSequence) return
rows.value = []
@@ -169,6 +196,7 @@ function applyFilters(model: QueryModel) {
}
function openCreate() {
if (!requireCurrentTenant()) return
Object.assign(form, emptyForm())
captureBaseline()
editorOpen.value = true
@@ -192,12 +220,14 @@ function assignEditor(row: PlatformExpressCompanyResource) {
}
async function openEdit(row: PlatformExpressCompanyResource) {
const tenantId = requireCurrentTenant()
if (!tenantId) return
const id = String(row.id)
const pending = new Set(editLoadingIds.value)
pending.add(id)
editLoadingIds.value = pending
try {
assignEditor(await getPlatformExpressCompany(fixedTenantId, row.id))
assignEditor(await getPlatformExpressCompany(tenantId, row.id))
} catch (error) {
const details = getApiErrorDetails(error, '快递公司详情加载失败,请稍后重试')
ElMessage.error(details.requestId ? `${details.message}(requestId: ${details.requestId})` : details.message)
@@ -262,6 +292,8 @@ function selectImportFile(event: Event) {
}
async function submitImport() {
const tenantId = requireCurrentTenant()
if (!tenantId) return
if (!importFile.value) {
ElMessage.warning('请先选择填写完成的 XLSX 文件')
return
@@ -270,7 +302,7 @@ async function submitImport() {
importError.value = null
importResult.value = null
try {
const result = await importPlatformExpressCompanies(fixedTenantId, importFile.value)
const result = await importPlatformExpressCompanies(tenantId, importFile.value)
importResult.value = result
if (result.success) {
ElMessage.success(`成功导入 ${result.importedRows} 家快递公司`)
@@ -308,13 +340,15 @@ function buildPayload(): SavePlatformExpressCompanyRequest {
}
async function saveExpressCompany() {
const tenantId = requireCurrentTenant()
if (!tenantId) return
const valid = await formRef.value?.validate().catch(() => false)
if (!valid) return
saving.value = true
try {
const payload = buildPayload()
if (form.id) await updatePlatformExpressCompany(fixedTenantId, form.id, payload)
else await createPlatformExpressCompany(fixedTenantId, payload)
if (form.id) await updatePlatformExpressCompany(tenantId, form.id, payload)
else await createPlatformExpressCompany(tenantId, payload)
editorOpen.value = false
ElMessage.success(form.id ? '快递公司资料已更新' : '快递公司已创建')
await loadExpressCompanies()
@@ -327,6 +361,8 @@ async function saveExpressCompany() {
}
async function toggleStatus(row: PlatformExpressCompanyResource) {
const tenantId = requireCurrentTenant()
if (!tenantId) return
const nextStatus: PlatformExpressCompanyStatus = row.status === 'ACTIVE' ? 'INACTIVE' : 'ACTIVE'
const action = nextStatus === 'ACTIVE' ? '启用' : '停用'
try {
@@ -343,7 +379,7 @@ async function toggleStatus(row: PlatformExpressCompanyResource) {
pending.add(id)
statusLoadingIds.value = pending
try {
await changePlatformExpressCompanyStatus(fixedTenantId, row.id, nextStatus)
await changePlatformExpressCompanyStatus(tenantId, row.id, nextStatus)
ElMessage.success(`已${action}${row.companyName}`)
await loadExpressCompanies()
} catch (error) {
@@ -357,19 +393,28 @@ async function toggleStatus(row: PlatformExpressCompanyResource) {
}
watch([page, pageSize], () => { void loadExpressCompanies() })
onMounted(() => {
void loadExpressCompanies()
watch(selectedTenantId, (tenantId) => {
editorOpen.value = false
importOpen.value = false
importFile.value = null
importResult.value = null
importError.value = null
rows.value = []
total.value = 0
loadedTenantId.value = null
page.value = 1
void loadExpressCompanies(tenantId)
})
</script>
<template>
<div class="business-page express-company-page">
<PlatformTenantSelector v-model="selectedTenantId" :disabled="tenantSwitchDisabled" />
<el-alert v-if="loadError" type="error" :closable="false" show-icon class="state-alert">
<template #title>快递公司加载失败</template>
<p>{{ loadError.message }}</p>
<ApiErrorMeta :details="loadError" />
<el-button size="small" @click="loadExpressCompanies">重新查询</el-button>
<el-button size="small" @click="loadExpressCompanies()">重新查询</el-button>
</el-alert>
<QueryToolbar v-model="filters" class="surface-panel" ariaLabel="快递公司筛选" :fields="queryFields" @search="applyFilters" />
@@ -378,11 +423,11 @@ onMounted(() => {
<div class="panel-heading">
<div>
<h2>快递公司列表</h2>
<p>共 {{ total }} 家快递公司</p>
<p>租户 {{ selectedTenantId || '—' }} · 共 {{ total }} 家快递公司</p>
</div>
<div class="panel-actions">
<el-button v-if="access.can('platform:express:import')" @click="openImport"><Upload :size="16" />批量导入</el-button>
<el-button v-if="access.can('platform:express:create')" type="primary" @click="openCreate"><Plus :size="16" />新增快递公司</el-button>
<el-button v-if="access.can('platform:express:import')" :disabled="!tenantReady" @click="openImport"><Upload :size="16" />批量导入</el-button>
<el-button v-if="access.can('platform:express:create')" type="primary" :disabled="!tenantReady" @click="openCreate"><Plus :size="16" />新增快递公司</el-button>
</div>
</div>
<DataTable
@@ -403,12 +448,14 @@ onMounted(() => {
v-if="access.can('platform:express:edit')"
link
type="primary"
:disabled="!tenantReady"
:loading="editLoadingIds.has(String((row as PlatformExpressCompanyResource).id))"
@click="openEdit(row as PlatformExpressCompanyResource)"
><Pencil :size="15" />编辑</el-button>
<el-button
v-if="access.can('platform:express:status')"
link
:disabled="!tenantReady"
:type="(row as PlatformExpressCompanyResource).status === 'ACTIVE' ? 'danger' : 'success'"
:loading="statusLoadingIds.has(String((row as PlatformExpressCompanyResource).id))"
@click="toggleStatus(row as PlatformExpressCompanyResource)"