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

278 lines
9.3 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 主区:停留列表、详情、发送记录、预警配置,以及管理员同步、扫描和发送。
依赖:停留列表、详情、发送记录、配置页、Element Plus、auth.store、equipment-acceptance.api、vue-router query。
逻辑:由 ChatViewContent 在 agent_code=equipment_acceptance 时替换消息列表与输入框。
本文件引入 Element 样式并包住 ElConfigProvider。标题和页签压到两行,下面整块留给当前页。
view=config 打开预警配置;view=sends 打开发送记录;orderId 只在停留列表下钻。
同步只在停留列表。扫描和发送只在发送记录,且仅平台管理员可见。
-->
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { Refresh } from '@element-plus/icons-vue';
import { useI18n } from 'vue-i18n';
import {
ElButton,
ElConfigProvider,
ElMessageBox,
ElTabPane,
ElTabs,
} from 'element-plus';
import 'element-plus/dist/index.css';
import 'element-plus/theme-chalk/dark/css-vars.css';
import '@/features/admin/styles/element-trial.css';
import { BusinessError } from '@/shared/api/client';
import { useAuthStore } from '@/features/auth/stores/auth.store';
import { useElementProvider } from '@/features/admin/composables/useElementProvider';
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 { elementLocale } = useElementProvider();
const syncing = ref(false);
const runningAction = 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 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);
}
/**
* 只接受本页三个页签,避免 Element 回传任意字符串。
* @param name - 被点中的页签名
*/
function onTabChange(name: string | number): void {
if (name === 'ledger' || name === 'sends' || name === 'config') selectView(name);
}
/**
* 管理员在发送记录页确认后立即扫描或发送。
* 核心逻辑:确认框取消会拒绝,这里直接返回。扫描会同步 E10;发送把待发送记录推到企业微信。
* @param action - scan 或 send
*/
async function askAlert(action: 'scan' | 'send'): Promise<void> {
if (runningAction.value) return;
const copy = action === 'send'
? { title: t('admin.equipmentAcceptance.sendAlerts'), message: t('admin.equipmentAcceptance.sendConfirm') }
: { title: t('admin.equipmentAcceptance.scanAlerts'), message: t('admin.equipmentAcceptance.scanConfirm') };
try {
await ElMessageBox.confirm(copy.message, copy.title, {
type: 'warning',
confirmButtonText: t('common.deleteDialog.confirm'),
cancelButtonText: t('common.cancel'),
});
} catch {
return;
}
runningAction.value = action;
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 {
runningAction.value = null;
}
}
/**
* 管理员从 OA 同步后刷新列表。
* 核心逻辑:成功提示写入、跳过和本次未见条数。失败展示业务错误。
*/
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>
<ElConfigProvider :locale="elementLocale">
<section class="ea-workspace">
<header class="ea-workspace__bar">
<h1 class="section-title">{{ t('admin.equipmentAcceptance.title') }}</h1>
<div v-if="isAdmin && activeView === 'sends'" class="ea-workspace__actions">
<ElButton :loading="runningAction === 'scan'" :disabled="runningAction !== null" @click="askAlert('scan')">
{{ t('admin.equipmentAcceptance.scanAlerts') }}
</ElButton>
<ElButton :loading="runningAction === 'send'" :disabled="runningAction !== null" @click="askAlert('send')">
{{ t('admin.equipmentAcceptance.sendAlerts') }}
</ElButton>
</div>
<ElButton
v-else-if="isAdmin && activeView === 'ledger' && !detailId"
:icon="Refresh"
:loading="syncing"
@click="runSync"
>
{{ t('admin.equipmentAcceptance.sync') }}
</ElButton>
</header>
<ElTabs :model-value="activeView" class="ea-tabs" @tab-change="onTabChange">
<ElTabPane v-for="tab in tabs" :key="tab.key" :name="tab.key" :label="tab.label" />
</ElTabs>
<div class="ea-workspace__body">
<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)"
/>
</div>
</section>
</ElConfigProvider>
</template>
<style scoped>
.ea-workspace {
display: flex;
flex: 1 1 auto;
flex-direction: column;
min-height: 0;
min-width: 0;
overflow: hidden;
background: var(--surface-muted, var(--el-fill-color-light));
}
.ea-workspace__bar {
display: flex;
flex: none;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 8px 16px 0;
}
.ea-workspace__actions {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.ea-tabs {
flex: none;
padding: 0 8px;
}
.ea-tabs :deep(.el-tabs__header) {
margin-bottom: 0;
}
.ea-tabs :deep(.el-tabs__nav-wrap::after) {
height: 1px;
background: var(--el-border-color-lighter);
}
.ea-tabs :deep(.el-tabs__content) {
display: none;
}
.ea-tabs :deep(.el-tabs__item) {
height: 36px;
padding: 0 12px;
font-size: var(--el-font-size-base);
line-height: 36px;
}
.ea-workspace__body {
display: flex;
flex: 1 1 auto;
flex-direction: column;
min-height: 0;
padding: 8px 12px 12px;
}
</style>