230 lines
9.2 KiB
Vue
230 lines
9.2 KiB
Vue
<!--
|
||
features/equipment-acceptance/views/EquipmentAcceptanceSendsPage.vue
|
||
用途:设备验收预警发送记录页,列出每张验收单某一档的主送、抄送、Excel 附件和结果。
|
||
依赖:equipment-acceptance.api、PaginationBar、统一消息提示、vue-i18n、lucide 图标。
|
||
逻辑:由 Chat 作业面的「发送记录」Tab 嵌入。Excel 附件列点击文件名下载。发送失败时悬停图标看原因。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { onMounted, ref } from 'vue';
|
||
import { CircleAlert, Download } from 'lucide-vue-next';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { BusinessError } from '@/shared/api/client';
|
||
import PaginationBar from '@/shared/components/PaginationBar.vue';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import { formatLocalDateTime } from '@/shared/utils/dateTime';
|
||
import {
|
||
downloadEquipmentAcceptanceSendFile,
|
||
listEquipmentAcceptanceSends,
|
||
type EquipmentAcceptanceSendRecord,
|
||
} from '../api/equipment-acceptance.api';
|
||
|
||
const { t, te } = useI18n();
|
||
const msg = useMessage();
|
||
const records = ref<EquipmentAcceptanceSendRecord[]>([]);
|
||
const downloadingId = ref<number | null>(null);
|
||
const loading = ref(false);
|
||
const page = ref(1);
|
||
const pageSize = ref(20);
|
||
const total = ref(0);
|
||
const pages = ref(1);
|
||
const failureTip = ref<{ text: string; left: number; top: number } | null>(null);
|
||
|
||
/**
|
||
* 拉取当前页发送记录。
|
||
* 核心逻辑:失败时清空表格并提示。空列表留给模板显示「还没有发送记录」。
|
||
*/
|
||
async function load(): Promise<void> {
|
||
loading.value = true;
|
||
try {
|
||
const result = await listEquipmentAcceptanceSends({ page: page.value, pageSize: pageSize.value });
|
||
records.value = result.items || [];
|
||
total.value = result.total || 0;
|
||
pages.value = Math.max(1, Math.ceil(total.value / pageSize.value));
|
||
} catch (error) {
|
||
records.value = [];
|
||
total.value = 0;
|
||
pages.value = 1;
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.sendsFailed'));
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 轮次列写成「超过 N 天」。
|
||
* @param item - 一条发送记录
|
||
* @returns 轮次文案
|
||
*/
|
||
function tierText(item: EquipmentAcceptanceSendRecord): string {
|
||
return t('admin.equipmentAcceptance.ruleOver', { days: item.threshold_days });
|
||
}
|
||
|
||
/**
|
||
* 结果用已知状态文案。未知状态保留原值。
|
||
* @param status - pending、sent、failed、cancelled
|
||
* @returns 结果文案
|
||
*/
|
||
function statusText(status: string): string {
|
||
const key = `admin.equipmentAcceptance.sendStatus.${status}`;
|
||
return te(key) ? t(key) : status;
|
||
}
|
||
|
||
/**
|
||
* 已发送显示本地时间,待发送没有时间时显示横线。
|
||
* @param value - 接口返回的发送时间
|
||
* @returns 时间或横线
|
||
*/
|
||
function sentText(value?: string | null): string {
|
||
if (!value) return '—';
|
||
const text = formatLocalDateTime(value);
|
||
return text === '-' ? '—' : text;
|
||
}
|
||
|
||
/**
|
||
* 在失败图标下方显示原因。用固定定位,避免被表格滚动容器裁掉。
|
||
* @param event - 悬停或聚焦事件
|
||
* @param message - 发送失败原因
|
||
*/
|
||
function openFailureTip(event: Event, message: string): void {
|
||
const target = event.currentTarget;
|
||
if (!(target instanceof HTMLElement)) return;
|
||
const rect = target.getBoundingClientRect();
|
||
const width = 280;
|
||
failureTip.value = {
|
||
text: message || t('admin.equipmentAcceptance.failureReasonEmpty'),
|
||
left: Math.max(8, Math.min(rect.left, window.innerWidth - width - 8)),
|
||
top: rect.bottom + 6,
|
||
};
|
||
}
|
||
|
||
/**
|
||
* 鼠标离开或失焦时关掉失败原因提示。
|
||
*/
|
||
function closeFailureTip(): void {
|
||
failureTip.value = null;
|
||
}
|
||
|
||
/**
|
||
* 下载一份已归档的收件人 Excel。
|
||
* 核心逻辑:接口返回当时发出的字节,由浏览器保存。
|
||
* @param file - 这一行上的 Excel 摘要
|
||
*/
|
||
async function downloadFile(file: { id: number; excel_file_name?: string | null }): Promise<void> {
|
||
if (downloadingId.value === file.id) return;
|
||
downloadingId.value = file.id;
|
||
try {
|
||
const { blob, filename } = await downloadEquipmentAcceptanceSendFile(file.id);
|
||
const objectUrl = URL.createObjectURL(blob);
|
||
const anchor = document.createElement('a');
|
||
anchor.href = objectUrl;
|
||
anchor.download = filename || file.excel_file_name || `equipment-acceptance-alert-${file.id}.xlsx`;
|
||
document.body.appendChild(anchor);
|
||
anchor.click();
|
||
document.body.removeChild(anchor);
|
||
URL.revokeObjectURL(objectUrl);
|
||
} catch (error) {
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.excelDownloadFailed'));
|
||
} finally {
|
||
downloadingId.value = null;
|
||
}
|
||
}
|
||
|
||
onMounted(() => {
|
||
void load();
|
||
});
|
||
|
||
defineExpose({ reload: load });
|
||
</script>
|
||
|
||
<template>
|
||
<section class="flex min-h-0 flex-1 flex-col gap-3 px-5 pb-4 pt-3">
|
||
<p class="text-xs text-[#64748b]">{{ t('admin.equipmentAcceptance.sendsLead') }}</p>
|
||
<div class="flex min-h-0 flex-1 flex-col overflow-hidden rounded-xl border border-[#dce4ef] bg-white">
|
||
<div class="min-h-0 flex-1 overflow-auto">
|
||
<table class="min-w-[960px] w-full border-collapse text-left text-xs">
|
||
<thead class="sticky top-0 bg-[#f8fbff] text-[#64748b]">
|
||
<tr>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.acceptanceNo') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.purchaseOrderNo') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.ruleTier') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.ruleTo') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.ruleCc') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.sendResult') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.excelColumn') }}</th>
|
||
<th class="px-3 py-2.5 font-bold">{{ t('admin.equipmentAcceptance.sentAt') }}</th>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr v-for="item in records" :key="item.id" class="border-b border-[#dce4ef]">
|
||
<td class="px-3 py-2.5 font-bold text-[#142033]">{{ item.acceptance_no || '—' }}</td>
|
||
<td class="px-3 py-2.5">{{ item.purchase_order_no || '—' }}</td>
|
||
<td class="px-3 py-2.5">{{ tierText(item) }}</td>
|
||
<td class="px-3 py-2.5">{{ item.to_text || '—' }}</td>
|
||
<td class="px-3 py-2.5">{{ item.cc_text || '—' }}</td>
|
||
<td class="px-3 py-2.5">
|
||
<span class="inline-flex items-center gap-1" :class="item.status === 'failed' ? 'font-bold text-[#b42318]' : ''">
|
||
{{ statusText(item.status) }}
|
||
<button
|
||
v-if="item.status === 'failed'"
|
||
class="inline-flex h-[22px] w-[22px] items-center justify-center rounded-full text-[#b42318] hover:bg-[#fff1f2]"
|
||
type="button"
|
||
:aria-label="t('admin.equipmentAcceptance.failureReasonAria')"
|
||
@mouseenter="openFailureTip($event, item.error_message || '')"
|
||
@mouseleave="closeFailureTip"
|
||
@focus="openFailureTip($event, item.error_message || '')"
|
||
@blur="closeFailureTip"
|
||
>
|
||
<CircleAlert class="h-4 w-4" />
|
||
</button>
|
||
</span>
|
||
</td>
|
||
<td class="px-3 py-2.5">
|
||
<div v-if="item.files?.length" class="flex max-w-[280px] flex-col gap-1">
|
||
<button
|
||
v-for="file in item.files"
|
||
:key="file.id"
|
||
class="inline-flex min-w-0 items-center gap-1 font-semibold text-[#2563eb] disabled:opacity-50"
|
||
type="button"
|
||
:disabled="downloadingId === file.id"
|
||
:title="t('admin.equipmentAcceptance.excelColumn')"
|
||
@click="downloadFile(file)"
|
||
>
|
||
<Download class="h-3.5 w-3.5 shrink-0" />
|
||
<span class="truncate">{{ file.excel_file_name }}</span>
|
||
</button>
|
||
</div>
|
||
<span v-else>—</span>
|
||
</td>
|
||
<td class="px-3 py-2.5">{{ sentText(item.sent_at) }}</td>
|
||
</tr>
|
||
<tr v-if="!loading && !records.length">
|
||
<td colspan="8" class="px-3 py-10 text-center text-[#64748b]">
|
||
{{ t('admin.equipmentAcceptance.sendsEmpty') }}
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</div>
|
||
<PaginationBar
|
||
class="shrink-0"
|
||
v-model:page="page"
|
||
v-model:page-size="pageSize"
|
||
:total="total"
|
||
:pages="pages"
|
||
@update:page="() => load()"
|
||
@update:page-size="() => load()"
|
||
/>
|
||
<Teleport to="body">
|
||
<div
|
||
v-if="failureTip"
|
||
class="pointer-events-none fixed z-[80] max-w-[280px] whitespace-normal break-words rounded-lg bg-slate-900 px-2.5 py-1.5 text-xs leading-relaxed text-white shadow-lg"
|
||
role="tooltip"
|
||
:style="{ left: `${failureTip.left}px`, top: `${failureTip.top}px` }"
|
||
>
|
||
{{ failureTip.text }}
|
||
</div>
|
||
</Teleport>
|
||
</section>
|
||
</template>
|