Files
moc/src/views/my/initiateChange/modules/changePre.vue
T

271 lines
12 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.
<!-- 变更预识别 -->
<script setup lang="ts">
import { inject, watch, ref, computed } from 'vue';
import { Icon } from '@iconify/vue'
import { useThemeStore } from '@/store/modules/theme';
import { getColorWithOpacity } from "@/utils/common";
import { AiError, aiApplication, aiRecognize, aiDeepRiskAnalysis, pollDeepAnalysisTask } from "@/service/api/ai";
import type { DeepAnalysisResult, OpenTask } from "@/service/api/ai";
import ChangeCompareTable from "./ChangeCompareTable.vue";
import PreRisk from "./preRisk.vue";
import DeepAnalysisPanel from "./DeepAnalysisPanel.vue";
const themeStore = useThemeStore();
const props = defineProps(['title','currentId','currentForm'])
const emit = defineEmits(['save', 'apply-generated'])
const generalLoading = inject('generalLoading', {saveLoading: ref<boolean>(false),btnDisabled: ref<boolean>(false)})
const {saveLoading,btnDisabled} = generalLoading
const aiFail = (e: any) => {
if (e instanceof AiError) window.$message?.warning(`AI 分析失败:${e.message},可手动填写`);
else window.$message?.warning("AI 分析失败,可手动填写");
};
// 变更识别loading
const preLoading = ref<boolean>(false);
// 生成变更申请单loading
const generateLoading = ref<boolean>(false);
// 风险预分析(异步深度分析)loading
const analyzing = ref<boolean>(false);
// 异步任务进度
const taskProgress = ref<{ progress: number; step: string } | null>(null);
// 深度分析错误信息(可重试)
const deepError = ref<string>('');
// 深度分析任务是否在跑(禁止重复提交)
const deepRunning = computed(() => analyzing.value);
// 是否显示险预分析(旧三段字段,读取兼容)
const preRiskShow = computed(() => !!(form.value.severity || form.value.probability || form.value.protection));
// 是否已生成深度分析结果(任一部分有内容即展示,避免法规/表格部分结果被隐藏)
const deepShow = computed(() => {
const data = form.value.deep_analysis;
return !!data && !!(data.risk_identification || data.risk_analysis || data.summary_text || data.conclusion_text || data.regulations_data?.length || data.risk_table?.length);
});
// 是否显示变更识别结果
const identifyShow = computed(() => form.value.compare_rows.length > 0);
// 任务步骤文案
const STEP_LABELS: Record<string, string> = {
regulations: '法规合规性分析',
risk_identification: '风险辨识',
risk_analysis: '专家研讨分析',
deep_summary: '深度总结'
};
const stepLabel = (step?: string) => STEP_LABELS[step ?? ''] ?? step ?? '';
// 表单
const form = ref<{
description: string,
compare_rows: {item: string, before_text: string, after_text: string}[],
severity: string,
probability: string,
protection: string,
deep_analysis: any
}>({
description: '',
compare_rows: [],
severity: "",
probability: "",
protection: "",
deep_analysis: null,
});
// AI 变更识别
const runIdentify = async () => {
if (!form.value.description.trim()) {
return window.$message?.warning("请先输入变更内容描述");
}
form.value.compare_rows = [];
btnDisabled.value = true;
preLoading.value = true;
try {
const res = await aiRecognize(form.value.description);
form.value.compare_rows = (res?.rows ?? []).map((x, i) => ({ item: x.item, before_text: x.before_text, after_text: x.after_text }));
window.$message?.success(`AI 变更识别完成,共 ${form.value.compare_rows.length} 项,请逐项人工确认(可直接修改)`);
} catch (e: any) {
form.value.compare_rows = [];
aiFail(e);
} finally {
preLoading.value = false;
btnDisabled.value = false;
}
};
// 运行智能风险分析(异步深度分析:识别→分析→法规→总结 四段;每完成一段即增量展示)
const runRiskAnalysis = async () => {
if(form.value.description.trim() === '') {
return window.$message?.warning('请先填写变更内容描述');
}
if (deepRunning.value) {
return window.$message?.warning('智能风险分析正在执行中,请勿重复提交');
}
analyzing.value = true;
btnDisabled.value = true;
deepError.value = '';
taskProgress.value = { progress: 0, step: '' };
// 增量回填骨架:保证法规/某一段先出时面板即显示(deepShow 依赖任一内容非空)
form.value.deep_analysis = {
...(form.value.deep_analysis ?? {}),
task_id: form.value.deep_analysis?.task_id ?? '',
status: 'processing',
risk_identification: form.value.deep_analysis?.risk_identification ?? '',
risk_analysis: form.value.deep_analysis?.risk_analysis ?? '',
regulations_data: form.value.deep_analysis?.regulations_data ?? [],
summary_text: form.value.deep_analysis?.summary_text ?? '',
risk_table: form.value.deep_analysis?.risk_table ?? [],
conclusion_text: form.value.deep_analysis?.conclusion_text ?? ''
};
try {
const created = await aiDeepRiskAnalysis(form.value.description, form.value.compare_rows);
const taskId = created?.task_id ?? '';
if (!taskId) throw new AiError(-1, '任务创建失败(未返回 task_id)');
form.value.deep_analysis.task_id = taskId;
const result: DeepAnalysisResult = await pollDeepAnalysisTask(taskId, {
onProgress: (t: OpenTask) => {
taskProgress.value = { progress: t?.progress ?? 0, step: t?.current_step ?? '' };
},
// 增量:服务端每完成一段(法规/辨识/分析)即回填展示,与 ai-web 分段体验一致
onPartial: (delta: any) => {
if (!form.value.deep_analysis) return;
if (delta?.report_risk_identification !== undefined) form.value.deep_analysis.risk_identification = delta.report_risk_identification;
if (delta?.report_risk_analysis !== undefined) form.value.deep_analysis.risk_analysis = delta.report_risk_analysis;
if (delta?.regulations_data !== undefined) form.value.deep_analysis.regulations_data = delta.regulations_data ?? [];
}
});
// 已生成内容不无故清空:逐字段回填,缺省保留原值
form.value.deep_analysis = {
...(form.value.deep_analysis ?? {}),
task_id: taskId,
status: 'completed',
risk_identification: result?.report_risk_identification ?? form.value.deep_analysis?.risk_identification ?? '',
risk_analysis: result?.report_risk_analysis ?? form.value.deep_analysis?.risk_analysis ?? '',
regulations_data: result?.regulations_data ?? form.value.deep_analysis?.regulations_data ?? [],
summary_text: result?.summary_text ?? form.value.deep_analysis?.summary_text ?? '',
risk_table: result?.risk_table ?? form.value.deep_analysis?.risk_table ?? [],
conclusion_text: result?.conclusion_text ?? form.value.deep_analysis?.conclusion_text ?? ''
};
window.$message?.success("智能风险分析完成:风险辨识 / 专家研讨 / 法规合规 / 风险总结 四段结果已生成,支持编辑");
} catch (e: any) {
deepError.value = e?.message || '智能风险分析失败';
// 失败/超时:保留已生成部分内容(含增量段落)仅提示;超时场景任务可能仍在后台执行
aiFail(e);
} finally {
analyzing.value = false;
btnDisabled.value = false;
taskProgress.value = null;
if (form.value.deep_analysis?.status === 'processing') {
form.value.deep_analysis.status = deepError.value ? 'partial' : 'processing';
}
}
};
// 生成变更申请单
const genForm = async () => {
if (!form.value.description.trim()) {
return window.$message?.warning("请先输入变更内容描述");
}
generateLoading.value = true;
btnDisabled.value = true;
try {
const res = await aiApplication(form.value.description, form.value.compare_rows);
// 由父组件把 AI 结果回填到「变更申请表」并切换标签
emit('apply-generated', res);
} catch (e: any) {
aiFail(e);
} finally {
generateLoading.value = false;
btnDisabled.value = false;
}
};
// 深度分析结果编辑回写
const onDeepUpdate = (val: any) => {
form.value.deep_analysis = { ...form.value.deep_analysis, ...val };
};
// 保存草稿
const saveDraft = () => {
emit('save');
}
defineExpose({form, runRiskAnalysis})
watch(() => props.currentForm, (newVal:any) => {
const {
attachments,
change_id,
created_at,
id,
updated_at,
...rest
} = newVal;
form.value = rest;
})
</script>
<template>
<div class="p-4">
<div class="space-y-4 scroll">
<!-- 变更内容描述 -->
<div class="border" :style="{ borderRadius: themeStore.themeRadius + 'px' }">
<div class="flex items-center justify-between px-4 py-2 border-b">
<div class="flex items-center">
<Icon icon="lucide:sparkles" class="size-16px text-violet-500" />
<span class="font-semibold text-base ml-2">变更内容描述</span>
</div>
<NButton ghost type="primary" @click="runIdentify" :disabled="btnDisabled || !form.description?.trim()">
<Icon v-if="preLoading" icon="ri:loader-4-fill" class="size-16px animate-spin" />
<Icon v-else icon="fluent:wand-24-regular" class="size-16px" />
变更识别
</NButton>
</div>
<div class="p-4">
<NInput type="textarea" v-model:value="form.description" rows="4" placeholder="请输入变更内容描述,例如:将反应釜R-101的搅拌速度从120rpm提高至180rpm,同时将反应温度从80℃提高至95℃…" />
</div>
</div>
<!-- 变更识别结果 -->
<template v-if="identifyShow">
<ChangeCompareTable
:data="form.compare_rows"
:editable="true"
@update:data="(d) => form.compare_rows = d"
/>
</template>
<!-- 异步任务进度 -->
<div v-if="analyzing" class="rounded-lg border border-blue-200 bg-blue-50 px-4 py-3">
<div class="flex items-center gap-2 text-xs text-blue-700">
<Icon icon="ri:loader-4-fill" class="size-14px animate-spin" />
<span>智能风险分析执行中{{ taskProgress?.step ? `:${stepLabel(taskProgress.step)}` : '' }}({{ taskProgress?.progress ?? 0 }}%),通常需要 2~5 分钟,请勿关闭页面</span>
</div>
<NProgress class="mt-2" type="line" :percentage="taskProgress?.progress ?? 0" :show-indicator="false" status="info" />
</div>
<!-- 失败重试提示 -->
<div v-if="deepError" class="rounded-lg border border-red-200 bg-red-50 px-4 py-2 text-xs text-red-600 flex items-center">
<Icon icon="mdi:alert-circle-outline" class="size-14px mr-1" />
<span>{{ deepError }};已生成内容已保留,可点击「智能风险分析」重试,或继续手动填写</span>
</div>
<!-- 深度分析四段结果 -->
<template v-if="deepShow">
<DeepAnalysisPanel :data="form.deep_analysis" :generating="analyzing" @update:data="onDeepUpdate" />
</template>
<!-- 风险预分析报告(旧三段字段,读取兼容) -->
<template v-if="preRiskShow">
<PreRisk :formInfo="form" @update:data="(key:'severity' | 'probability' | 'protection',val:string) => form[key] = val" />
</template>
</div>
</div>
<div class="flex flex-wrap items-center gap-x-4 gap-y-2 px-4 py-2.5" :style="{ backgroundColor: getColorWithOpacity(themeStore.themeColor, 0.15)}">
<span class="text-xs text-slate-500">预识别阶段不设审批人与提交:完成识别与智能风险分析后,请切换至「变更申请表」选择审批人并提交</span>
<div class="ml-auto flex shrink-0 gap-2">
<NButton ghost type="warning" size="small" @click="runRiskAnalysis" :disabled="btnDisabled">
<Icon v-if="analyzing" icon="ri:loader-4-fill" class="size-14px animate-spin" />
<Icon v-else icon="mdi:shield-alert-outline" class="size-14px" />智能风险分析
</NButton>
<NButton type="primary" size="small" :disabled="btnDisabled" :loading="saveLoading" @click="saveDraft">保存</NButton>
<NButton ghost type="success" size="small" :loading="generateLoading" :disabled="btnDisabled" @click="genForm">
<Icon icon="akar-icons:file" class="size-14px" /> 生成变更申请单
</NButton>
</div>
</div>
</template>
<style scoped lang="scss">
.scroll {
height: calc(100vh - 326px);
overflow-y: auto;
}
</style>