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
@@ -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)"