Files
syops/frontend/src/features/equipment-acceptance/components/EquipmentAcceptanceChatPanel.vue
T

226 lines
8.4 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!--
features/equipment-acceptance/components/EquipmentAcceptanceChatPanel.vue
用途:设备验收预警 Chat 主区:停留列表、详情、发送记录、预警配置,以及管理员同步、扫描和发送。
依赖:停留列表、详情、发送记录、配置页、auth.store、equipment-acceptance.api、DeleteConfirmDialog、vue-router query。
逻辑:由 ChatViewContent 在 agent_code=equipment_acceptance 时替换消息列表与输入框。
view=config 打开预警配置;view=sends 打开发送记录;orderId 只在停留列表下钻。
同步只在停留列表。扫描和发送只在发送记录,且仅平台管理员可见。
-->
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { RefreshCw } from 'lucide-vue-next';
import { useI18n } from 'vue-i18n';
import { BusinessError } from '@/shared/api/client';
import DeleteConfirmDialog from '@/shared/components/DeleteConfirmDialog.vue';
import { useAuthStore } from '@/features/auth/stores/auth.store';
import { useMessage } from '@/shared/composables/useMessage';
import {
scanEquipmentAcceptanceAlerts,
sendEquipmentAcceptanceAlerts,
syncEquipmentAcceptance,
} from '../api/equipment-acceptance.api';
import EquipmentAcceptanceConfigPage from '../views/EquipmentAcceptanceConfigPage.vue';
import EquipmentAcceptanceDetailPage from '../views/EquipmentAcceptanceDetailPage.vue';
import EquipmentAcceptancePage from '../views/EquipmentAcceptancePage.vue';
import EquipmentAcceptanceSendsPage from '../views/EquipmentAcceptanceSendsPage.vue';
type WorkspaceView = 'ledger' | 'sends' | 'config';
const { t } = useI18n();
const route = useRoute();
const router = useRouter();
const auth = useAuthStore();
const msg = useMessage();
const syncing = ref(false);
const alertRunning = ref(false);
const pendingAlert = ref<'scan' | 'send' | null>(null);
const listPage = ref<{ reload: () => Promise<void> } | null>(null);
const sendsPage = ref<{ reload: () => Promise<void> } | null>(null);
const isAdmin = computed(() => Boolean(auth.isAdmin));
const activeView = computed<WorkspaceView>(() => {
if (route.query.view === 'config') return 'config';
if (route.query.view === 'sends') return 'sends';
return 'ledger';
});
const showAlertConfirm = computed({
get: () => pendingAlert.value !== null,
set: (open: boolean) => {
if (!open) pendingAlert.value = null;
},
});
const alertConfirmCopy = computed(() => (
pendingAlert.value === 'send'
? { title: t('admin.equipmentAcceptance.sendAlerts'), message: t('admin.equipmentAcceptance.sendConfirm') }
: { title: t('admin.equipmentAcceptance.scanAlerts'), message: t('admin.equipmentAcceptance.scanConfirm') }
));
const detailId = computed(() => {
if (activeView.value !== 'ledger') return null;
const raw = Number(route.query.orderId);
return Number.isInteger(raw) && raw > 0 ? raw : null;
});
const tabs = computed(() => ([
{ key: 'ledger' as const, label: t('admin.equipmentAcceptance.tabLedger') },
{ key: 'sends' as const, label: t('admin.equipmentAcceptance.tabSends') },
{ key: 'config' as const, label: t('admin.equipmentAcceptance.tabConfig') },
]));
/**
* 把当前 Tab 和详情写入 Chat 路由。
* 核心逻辑:配置和发送记录清掉 orderId。停留列表不写 view,避免旧链接被改掉。
* @param view - ledger、sends 或 config
* @param orderId - 本地快照主键,空表示列表
*/
async function syncQuery(view: WorkspaceView, orderId: number | null): Promise<void> {
const query = { ...route.query };
if (view === 'config' || view === 'sends') {
query.view = view;
delete query.orderId;
} else {
delete query.view;
if (orderId) query.orderId = String(orderId);
else delete query.orderId;
}
const sameView = String(route.query.view || '') === String(query.view || '');
const sameOrder = String(route.query.orderId || '') === String(query.orderId || '');
if (sameView && sameOrder) return;
await router.replace({ params: { ...route.params }, query });
}
/**
* 切换停留列表、发送记录和预警配置。
* @param view - 目标 Tab
*/
function selectView(view: WorkspaceView): void {
void syncQuery(view, null);
}
/**
* 管理员在发送记录页立即扫描或发送。
* 核心逻辑:公共确认框点确定后才调用。先取出动作再关掉弹窗,避免关闭时把待执行动作清掉。
*/
function confirmAlertAction(): void {
const action = pendingAlert.value;
pendingAlert.value = null;
if (action) void runAlertAction(action);
}
/**
* 执行已经确认的扫描或发送。
* 核心逻辑:扫描会同步 E10;发送把待发送记录推到企业微信。
* @param action - scan 或 send
*/
async function runAlertAction(action: 'scan' | 'send'): Promise<void> {
if (alertRunning.value) return;
alertRunning.value = true;
try {
if (action === 'scan') {
const result = await scanEquipmentAcceptanceAlerts();
msg.success(t('admin.equipmentAcceptance.scanDone', {
created: result.created,
updated: result.updated,
cancelled: result.cancelled,
}));
} else {
const result = await sendEquipmentAcceptanceAlerts();
msg.success(t('admin.equipmentAcceptance.sendDone', {
sent: result.sent,
failed: result.failed,
}));
}
await sendsPage.value?.reload();
} catch (error) {
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.syncFailed'));
} finally {
alertRunning.value = false;
}
}
/**
* 管理员从 E10 同步后刷新列表。
* 核心逻辑:成功提示写入、跳过和本次未见条数。失败展示业务错误。
*/
async function runSync(): Promise<void> {
if (syncing.value) return;
syncing.value = true;
try {
const result = await syncEquipmentAcceptance();
msg.success(t('admin.equipmentAcceptance.syncDone', {
upserted: result.upserted,
skipped: result.skipped_without_request_id,
absent: result.absent,
}));
await listPage.value?.reload();
} catch (error) {
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.syncFailed'));
} finally {
syncing.value = false;
}
}
</script>
<template>
<section class="flex min-h-0 min-w-0 flex-1 flex-col overflow-hidden bg-[var(--surface-muted,#f4f7fb)]">
<div class="flex flex-wrap items-center justify-between gap-2 px-5 pt-3">
<div class="min-w-0">
<h1 class="text-lg font-bold text-primary md:text-xl">{{ t('admin.equipmentAcceptance.title') }}</h1>
</div>
<div v-if="isAdmin && activeView === 'sends'" class="flex flex-wrap items-center gap-2">
<button class="btn-secondary-ds" type="button" :disabled="alertRunning" @click="pendingAlert = 'scan'">
{{ t('admin.equipmentAcceptance.scanAlerts') }}
</button>
<button class="btn-secondary-ds" type="button" :disabled="alertRunning" @click="pendingAlert = 'send'">
{{ t('admin.equipmentAcceptance.sendAlerts') }}
</button>
</div>
<button
v-if="isAdmin && activeView === 'ledger' && !detailId"
class="btn-secondary-ds"
type="button"
:disabled="syncing"
@click="runSync"
>
<RefreshCw class="h-4 w-4" aria-hidden="true" />{{ t('admin.equipmentAcceptance.sync') }}
</button>
</div>
<div class="mt-2 flex gap-0 border-b border-default px-5">
<button
v-for="tab in tabs"
:key="tab.key"
type="button"
class="mb-[-1px] border-b-2 px-3 py-2 text-sm font-bold"
:class="activeView === tab.key
? 'border-brand-500 text-brand-600'
: 'border-transparent text-muted hover:text-secondary'"
@click="selectView(tab.key)"
>
{{ tab.label }}
</button>
</div>
<EquipmentAcceptanceDetailPage
v-if="detailId"
:order-id="detailId"
@back="syncQuery('ledger', null)"
/>
<EquipmentAcceptanceConfigPage
v-else-if="activeView === 'config'"
:editable="isAdmin"
/>
<EquipmentAcceptanceSendsPage v-else-if="activeView === 'sends'" ref="sendsPage" />
<EquipmentAcceptancePage
v-else
ref="listPage"
@open-detail="syncQuery('ledger', $event)"
/>
<DeleteConfirmDialog
v-if="isAdmin"
v-model="showAlertConfirm"
:title="alertConfirmCopy.title"
:message="alertConfirmCopy.message"
confirm-tone="primary"
@confirm="confirmAlertAction"
/>
</section>
</template>