243 lines
11 KiB
Vue
243 lines
11 KiB
Vue
<script setup lang="ts">
|
||
import { ref } from 'vue';
|
||
import { AlertCircle, CheckCircle2, Clock3, Download, Loader2 } from 'lucide-vue-next';
|
||
import { getAuthHeaders } from '@/shared/utils/auth';
|
||
import type { ToolRunProgress } from '../api/toolRunProgress.api';
|
||
|
||
const props = defineProps<{
|
||
run: ToolRunProgress | null;
|
||
loading?: boolean;
|
||
waiting?: boolean;
|
||
error?: string;
|
||
fallbackTitle?: string;
|
||
}>();
|
||
|
||
const downloadingExcel = ref(false);
|
||
const downloadError = ref('');
|
||
|
||
const statusLabel = (status?: string) => {
|
||
const map: Record<string, string> = {
|
||
running: '执行中',
|
||
success: '已完成',
|
||
partial_failed: '部分失败',
|
||
failed: '失败',
|
||
cancelled: '已取消',
|
||
canceled: '已取消',
|
||
};
|
||
return map[String(status || '')] || status || '等待中';
|
||
};
|
||
|
||
const formatTime = (value?: string | null) => {
|
||
if (!value) return '-';
|
||
const normalized = String(value).match(/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?$/)
|
||
? `${value}Z`
|
||
: String(value);
|
||
const date = new Date(normalized);
|
||
if (Number.isNaN(date.getTime())) return String(value).replace('T', ' ').slice(0, 19);
|
||
const pad = (num: number) => String(num).padStart(2, '0');
|
||
return [
|
||
date.getFullYear(),
|
||
pad(date.getMonth() + 1),
|
||
pad(date.getDate()),
|
||
].join('-') + ' ' + [
|
||
pad(date.getHours()),
|
||
pad(date.getMinutes()),
|
||
pad(date.getSeconds()),
|
||
].join(':');
|
||
};
|
||
|
||
const percent = () => {
|
||
const value = Number(props.run?.progress_percent ?? 0);
|
||
if (!Number.isFinite(value)) return 0;
|
||
return Math.max(0, Math.min(value, 100));
|
||
};
|
||
|
||
const kbSyncMeta = (status?: string | null) => {
|
||
const value = String(status || '').toLowerCase();
|
||
if (value === 'success') {
|
||
return { label: '知识库已同步', tone: 'border-emerald-200 bg-emerald-50 text-emerald-700', icon: 'success' };
|
||
}
|
||
if (value === 'failed') {
|
||
return { label: '知识库同步失败', tone: 'border-rose-200 bg-rose-50 text-rose-700', icon: 'failed' };
|
||
}
|
||
if (['queued', 'cleaning', 'syncing'].includes(value)) {
|
||
return { label: '知识库同步中', tone: 'border-sky-200 bg-sky-50 text-sky-700', icon: 'syncing' };
|
||
}
|
||
if (value === 'skipped') {
|
||
return { label: '知识库未同步', tone: 'border-slate-200 bg-slate-50 text-slate-600', icon: 'skipped' };
|
||
}
|
||
return { label: `知识库:${status || '未知'}`, tone: 'border-slate-200 bg-slate-50 text-slate-600', icon: 'skipped' };
|
||
};
|
||
|
||
const parseDownloadFilename = (disposition: string | null, fallback: string) => {
|
||
const value = disposition || '';
|
||
const utf8Match = value.match(/filename\*=UTF-8''([^;]+)/i);
|
||
const raw = utf8Match?.[1] || value.match(/filename="?([^";]+)"?/i)?.[1] || fallback;
|
||
try {
|
||
return decodeURIComponent(raw);
|
||
} catch {
|
||
return raw;
|
||
}
|
||
};
|
||
|
||
const stringifyDownloadError = (payload: any, fallback: string) => {
|
||
if (!payload) return fallback;
|
||
if (typeof payload === 'string') return payload;
|
||
if (typeof payload?.message === 'string') return payload.message;
|
||
if (typeof payload?.error === 'string') return payload.error;
|
||
if (typeof payload?.error?.message === 'string') return payload.error.message;
|
||
if (typeof payload?.code === 'string') return payload.code;
|
||
return fallback;
|
||
};
|
||
|
||
const downloadExcel = async () => {
|
||
const url = props.run?.excel?.download_url;
|
||
if (!url || downloadingExcel.value) return;
|
||
downloadingExcel.value = true;
|
||
downloadError.value = '';
|
||
try {
|
||
const res = await fetch(url, { headers: getAuthHeaders() });
|
||
if (!res.ok) {
|
||
const body = await res.json().catch(() => ({}));
|
||
throw new Error(stringifyDownloadError(body, `下载失败:${res.status}`));
|
||
}
|
||
const blob = await res.blob();
|
||
const filename = parseDownloadFilename(
|
||
res.headers.get('content-disposition'),
|
||
props.run?.excel?.file_name || `${props.run?.run_no || 'result'}.xlsx`,
|
||
);
|
||
const objectUrl = URL.createObjectURL(blob);
|
||
const link = document.createElement('a');
|
||
link.href = objectUrl;
|
||
link.download = filename;
|
||
document.body.appendChild(link);
|
||
link.click();
|
||
document.body.removeChild(link);
|
||
URL.revokeObjectURL(objectUrl);
|
||
} catch (e: any) {
|
||
downloadError.value = e?.message || '下载 Excel 失败';
|
||
} finally {
|
||
downloadingExcel.value = false;
|
||
}
|
||
};
|
||
</script>
|
||
|
||
<template>
|
||
<section
|
||
class="mx-auto mb-4 w-full max-w-4xl overflow-hidden rounded-2xl border border-sky-100 bg-gradient-to-br from-sky-50 via-white to-emerald-50 shadow-sm"
|
||
>
|
||
<div class="flex items-start justify-between gap-3 border-b border-sky-100/80 px-4 py-3">
|
||
<div>
|
||
<div class="flex items-center gap-2 text-sm font-semibold text-slate-800">
|
||
<Loader2 v-if="run?.status === 'running'" class="h-4 w-4 animate-spin text-sky-600" />
|
||
<CheckCircle2 v-else-if="run?.status === 'success'" class="h-4 w-4 text-emerald-600" />
|
||
<AlertCircle v-else-if="run?.status === 'failed' || run?.status === 'partial_failed'" class="h-4 w-4 text-amber-600" />
|
||
<Clock3 v-else class="h-4 w-4 text-sky-600" />
|
||
{{ run?.title || fallbackTitle || '任务进度' }}
|
||
</div>
|
||
<p class="mt-1 text-xs text-slate-500">
|
||
<template v-if="run">
|
||
批次:{{ run.run_no }} · 状态:{{ statusLabel(run.status) }}
|
||
<span v-if="run.error_message" class="ml-2 text-rose-700">错误信息:{{ run.error_message }}</span>
|
||
</template>
|
||
<template v-else-if="loading || waiting">正在等待检核任务启动...</template>
|
||
<template v-else-if="error">{{ error }}</template>
|
||
</p>
|
||
<p v-if="run?.status === 'running' && run.current_step" class="mt-1 text-xs text-sky-700">
|
||
当前阶段:{{ run.current_step.label || run.current_step.step }}
|
||
<span v-if="run.current_step.row_id"> · 当前记录:{{ run.current_step.row_id }}</span>
|
||
<span v-if="run.current_step.started_at"> · 阶段开始:{{ formatTime(run.current_step.started_at) }}</span>
|
||
</p>
|
||
</div>
|
||
<div class="flex shrink-0 flex-wrap items-center justify-end gap-2">
|
||
<span
|
||
v-if="run?.knowledge_base?.sync_status"
|
||
class="inline-flex items-center gap-1 rounded-full border px-2.5 py-1.5 text-xs font-medium"
|
||
:class="kbSyncMeta(run.knowledge_base.sync_status).tone"
|
||
:title="run.knowledge_base.error_message || kbSyncMeta(run.knowledge_base.sync_status).label"
|
||
>
|
||
<CheckCircle2 v-if="kbSyncMeta(run.knowledge_base.sync_status).icon === 'success'" class="h-3.5 w-3.5" />
|
||
<AlertCircle v-else-if="kbSyncMeta(run.knowledge_base.sync_status).icon === 'failed'" class="h-3.5 w-3.5" />
|
||
<Loader2 v-else-if="kbSyncMeta(run.knowledge_base.sync_status).icon === 'syncing'" class="h-3.5 w-3.5 animate-spin" />
|
||
<Clock3 v-else class="h-3.5 w-3.5" />
|
||
{{ kbSyncMeta(run.knowledge_base.sync_status).label }}
|
||
</span>
|
||
<button
|
||
v-if="run?.excel?.download_url"
|
||
type="button"
|
||
:disabled="downloadingExcel"
|
||
class="inline-flex items-center gap-1 rounded-full bg-sky-600 px-3 py-1.5 text-xs font-medium text-white transition hover:bg-sky-700 disabled:cursor-not-allowed disabled:opacity-60"
|
||
@click="downloadExcel"
|
||
>
|
||
<Loader2 v-if="downloadingExcel" class="h-3.5 w-3.5 animate-spin" />
|
||
<Download v-else class="h-3.5 w-3.5" />
|
||
{{ downloadingExcel ? '下载中...' : '下载 Excel' }}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-if="run" class="space-y-3 px-4 py-4">
|
||
<div v-if="downloadError" class="rounded-xl bg-rose-50 px-3 py-2 text-xs text-rose-700">
|
||
{{ downloadError }}
|
||
</div>
|
||
|
||
<div class="h-2.5 overflow-hidden rounded-full bg-slate-200">
|
||
<div
|
||
class="h-full rounded-full bg-gradient-to-r from-sky-500 to-emerald-500 transition-all duration-500"
|
||
:style="{ width: `${percent()}%` }"
|
||
></div>
|
||
</div>
|
||
<div class="flex flex-wrap items-center justify-between gap-2 text-xs text-slate-500">
|
||
<span>进度 {{ percent() }}%</span>
|
||
<span>开始:{{ formatTime(run.started_at) }} · 完成:{{ formatTime(run.finished_at) }}</span>
|
||
</div>
|
||
|
||
<div class="grid grid-cols-2 gap-1.5 sm:grid-cols-4 lg:grid-cols-8">
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">总数</div>
|
||
<div class="text-base font-bold text-slate-800">{{ run.total_count }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">已处理</div>
|
||
<div class="text-base font-bold text-sky-700">{{ run.processed_count }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">成功</div>
|
||
<div class="text-base font-bold text-emerald-700">{{ run.success_count }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">失败</div>
|
||
<div class="text-base font-bold text-rose-700">{{ run.failed_count }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">跳过</div>
|
||
<div class="text-base font-bold text-amber-700">{{ run.skipped_count }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">合格笔数</div>
|
||
<div class="text-base font-bold text-emerald-700">{{ run.judgement?.qualified_count ?? 0 }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">不合格笔数</div>
|
||
<div class="text-base font-bold text-rose-700">{{ run.judgement?.unqualified_count ?? 0 }}</div>
|
||
</div>
|
||
<div class="rounded-lg bg-white/80 px-2 py-1.5">
|
||
<div class="whitespace-nowrap text-xs text-slate-500">人工判定笔数</div>
|
||
<div class="text-base font-bold text-amber-700">{{ run.judgement?.manual_count ?? 0 }}</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-if="run.knowledge_base?.error_message" class="rounded-xl bg-amber-50 px-3 py-2 text-xs text-amber-700">
|
||
知识库同步错误:{{ run.knowledge_base.error_message }}
|
||
</div>
|
||
</div>
|
||
|
||
<div v-else-if="waiting || loading" class="space-y-3 px-4 py-4">
|
||
<div class="h-2.5 overflow-hidden rounded-full bg-slate-200">
|
||
<div class="h-full w-1/3 animate-pulse rounded-full bg-gradient-to-r from-sky-400 to-emerald-400" />
|
||
</div>
|
||
<p class="text-xs text-slate-500">检核启动后,这里会显示实时进度。</p>
|
||
</div>
|
||
</section>
|
||
</template>
|