348 lines
11 KiB
Vue
348 lines
11 KiB
Vue
<template>
|
|
<div class="ai-analysis-panel">
|
|
<div class="panel-box">
|
|
<div class="panel-box__header">
|
|
<span class="panel-box__title">AI课程分析</span>
|
|
<div class="panel-box__actions">
|
|
<t-select
|
|
v-if="canAggregate"
|
|
v-model="courseId"
|
|
filterable
|
|
clearable
|
|
placeholder="全部课程(汇总)"
|
|
style="width: 280px"
|
|
:loading="courseLoading"
|
|
>
|
|
<t-option label="全部课程(汇总)" value="" />
|
|
<t-option v-for="c in courses" :key="c.id" :value="c.id" :label="c.name" />
|
|
</t-select>
|
|
<t-button theme="primary" :loading="generating" @click="generate">
|
|
{{ generating ? 'AI 分析中…' : (result ? '重新分析' : '生成 AI 分析') }}
|
|
</t-button>
|
|
</div>
|
|
</div>
|
|
<p class="ai-hint">
|
|
汇总{{ scopeHint }}的课程学习、考试成绩、培训签到与错题情况,由大模型生成本次分析报告与指导建议。
|
|
分析约需数十秒,请耐心等待。
|
|
</p>
|
|
|
|
<!-- 统计指标卡(与报告同源,来自后端聚合) -->
|
|
<div v-if="result" class="metric-grid">
|
|
<div class="metric-card">
|
|
<div class="metric-card__label">学习完成率</div>
|
|
<div class="metric-card__value" :class="metricTone(result.stats.learning.completion_rate)">
|
|
{{ Math.round(result.stats.learning.completion_rate) }}%
|
|
</div>
|
|
<div class="metric-card__sub">已完成 {{ result.stats.learning.done }}/{{ result.stats.learning.total }}<template v-if="result.stats.learning.overdue"> · 逾期 {{ result.stats.learning.overdue }}</template></div>
|
|
</div>
|
|
<div class="metric-card">
|
|
<div class="metric-card__label">考试及格率</div>
|
|
<div class="metric-card__value" :class="metricTone(result.stats.exam.pass_rate)">
|
|
{{ Math.round(result.stats.exam.pass_rate) }}%
|
|
</div>
|
|
<div class="metric-card__sub">及格 {{ result.stats.exam.passed }}/已考 {{ result.stats.exam.taken }} · 均分 {{ result.stats.exam.avg_score }}</div>
|
|
</div>
|
|
<div class="metric-card">
|
|
<div class="metric-card__label">签到出勤率</div>
|
|
<div class="metric-card__value" :class="metricTone(signRate)">
|
|
{{ Math.round(signRate) }}%
|
|
</div>
|
|
<div class="metric-card__sub">实到 {{ result.stats.sign.signed }}/应到 {{ result.stats.sign.expected }}<template v-if="result.stats.sign.late"> · 迟到 {{ result.stats.sign.late }}</template></div>
|
|
</div>
|
|
<div class="metric-card metric-card--wide">
|
|
<div class="metric-card__label">薄弱知识点 Top{{ result.stats.wrong_questions.length || '—' }}</div>
|
|
<div v-if="result.stats.wrong_questions.length" class="metric-card__weak-list">
|
|
<t-tag v-for="w in result.stats.wrong_questions" :key="w.knowledge_point" theme="danger" variant="light" size="small">
|
|
{{ w.knowledge_point }} · 错 {{ w.wrong_count }} 次
|
|
</t-tag>
|
|
</div>
|
|
<div v-else class="metric-card__sub">暂无错题数据</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- 分析报告 -->
|
|
<div v-if="result" class="report">
|
|
<div class="report__meta">
|
|
<t-tag theme="primary" variant="light">{{ scopeLabel }}</t-tag>
|
|
<t-tag v-if="result.model_name === 'rule-fallback'" theme="warning" variant="light">规则引擎版</t-tag>
|
|
<span v-if="result.model_name === 'rule-fallback' && result.fallback_reason" class="report__fallback">{{ result.fallback_reason }}</span>
|
|
<span v-else-if="result.model_name" class="report__model">{{ result.model_name }}</span>
|
|
<span class="report__time">生成于 {{ fmtTime(result.generated_at) }}</span>
|
|
<t-button variant="text" size="small" class="report__copy" @click="copyReport">复制报告</t-button>
|
|
</div>
|
|
<!-- eslint-disable-next-line vue/no-v-html -- sanitizeHTML 已消毒 -->
|
|
<div class="report__body" v-html="analysisHtml"></div>
|
|
</div>
|
|
|
|
<div v-if="generating" class="generating">
|
|
<t-loading size="small" text="正在聚合学习/考试/签到/错题数据并调用大模型分析…" show-overlay />
|
|
</div>
|
|
<t-empty v-else-if="!result" description="点击「生成 AI 分析」获取汇总分析与指导建议" />
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { computed, onMounted, ref } from 'vue'
|
|
import { marked } from 'marked'
|
|
import { MessagePlugin } from 'tdesign-vue-next'
|
|
import { sanitizeHTML } from '@/utils/security'
|
|
import { fmtBeijingTime } from '@/utils/datetime'
|
|
import {
|
|
getCourseAIAnalysis,
|
|
listCourses,
|
|
type TrainingAIAnalysis,
|
|
type TrainingCourse,
|
|
unwrap,
|
|
} from '@/api/training'
|
|
|
|
// AI课程分析:后端聚合学习/考试/签到/错题统计,交租户默认对话模型出
|
|
// markdown 报告;管理角色为全员维度(全部课程/按课程聚焦),学员为个人维度。
|
|
// 未配置模型时后端回退规则引擎摘要(model_name=rule-fallback)。
|
|
|
|
const props = defineProps<{ roles: string[]; admin?: boolean }>()
|
|
// 全员维度分析:人事/课长/讲师(roles 已按权限组展开,经理自动命中)
|
|
const canAggregate = computed(() => props.admin || props.roles.some((r) => ['hr', 'supervisor', 'lecturer'].includes(r)))
|
|
|
|
const courses = ref<TrainingCourse[]>([])
|
|
const courseLoading = ref(false)
|
|
const courseId = ref('')
|
|
const generating = ref(false)
|
|
const result = ref<TrainingAIAnalysis | null>(null)
|
|
|
|
const scopeHint = computed(() =>
|
|
canAggregate.value ? '(选择课程可聚焦分析,默认汇总全部课程)' : '个人',
|
|
)
|
|
const scopeLabel = computed(() => {
|
|
if (!result.value) return ''
|
|
if (result.value.scope === 'personal') return '个人维度'
|
|
return result.value.scope === 'course' ? `课程:${result.value.course_name}` : '全部课程'
|
|
})
|
|
|
|
const analysisHtml = computed(() => {
|
|
if (!result.value?.analysis) return ''
|
|
try {
|
|
return sanitizeHTML(marked.parse(result.value.analysis, { async: false }) as string)
|
|
} catch {
|
|
return `<pre>${result.value.analysis}</pre>`
|
|
}
|
|
})
|
|
|
|
const signRate = computed(() => {
|
|
const s = result.value?.stats.sign
|
|
if (!s || s.expected <= 0) return 0
|
|
return Math.min((s.signed / s.expected) * 100, 100)
|
|
})
|
|
|
|
const metricTone = (rate: number) => (rate >= 80 ? 'tone-good' : rate >= 60 ? 'tone-warn' : 'tone-bad')
|
|
|
|
const fmtTime = (raw: string) => fmtBeijingTime(raw) || raw
|
|
|
|
// 区分失败原因给出可行动提示:超时(大模型长耗时)/接口不存在(后端未更新)/后端具体错误
|
|
const aiErrorMessage = (e: any): string => {
|
|
if (e?.code === 'ECONNABORTED' || /timeout/i.test(e?.message || '')) {
|
|
return '生成超时:大模型响应较慢,请重试,或改选单门课程聚焦分析'
|
|
}
|
|
if (e?.response?.status === 404) {
|
|
return 'AI 分析接口不存在,请确认后端服务已更新并重启'
|
|
}
|
|
return e?.response?.data?.message || 'AI 分析失败,请稍后重试'
|
|
}
|
|
|
|
const generate = async () => {
|
|
generating.value = true
|
|
try {
|
|
const res = await getCourseAIAnalysis(courseId.value || undefined)
|
|
const data = unwrap(res)
|
|
if (data) {
|
|
result.value = data
|
|
if (data.model_name !== 'rule-fallback') {
|
|
MessagePlugin.success('AI 分析报告已生成')
|
|
} else if (data.fallback_reason && !data.fallback_reason.includes('未配置')) {
|
|
// 模型存在但调用失败(网络/鉴权/超时等),给出后端带回的具体原因
|
|
MessagePlugin.error(`大模型调用失败:${data.fallback_reason.replace(/^大模型调用失败:/, '')}。本次为规则引擎版摘要,请检查模型服务的网络与配置`)
|
|
} else {
|
|
MessagePlugin.warning('未配置可用大模型,本次为规则引擎版摘要;请在「模型管理」配置后重试')
|
|
}
|
|
}
|
|
} catch (e: any) {
|
|
MessagePlugin.error(aiErrorMessage(e))
|
|
} finally {
|
|
generating.value = false
|
|
}
|
|
}
|
|
|
|
const copyReport = async () => {
|
|
if (!result.value?.analysis) return
|
|
try {
|
|
await navigator.clipboard.writeText(result.value.analysis)
|
|
MessagePlugin.success('报告已复制到剪贴板')
|
|
} catch {
|
|
MessagePlugin.error('复制失败,请手动选择文本复制')
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
if (!canAggregate.value) return
|
|
courseLoading.value = true
|
|
try {
|
|
const res = await listCourses({ page_size: 100 })
|
|
courses.value = (unwrap(res)?.items || []).filter((c: TrainingCourse) => c.status === 'published')
|
|
} catch {
|
|
courses.value = []
|
|
} finally {
|
|
courseLoading.value = false
|
|
}
|
|
})
|
|
</script>
|
|
|
|
<style lang="less" scoped>
|
|
@import (reference) '../training.less';
|
|
|
|
.panel-box {
|
|
.training-panel-box();
|
|
}
|
|
|
|
.ai-hint {
|
|
margin: 0 0 12px;
|
|
color: var(--td-text-color-secondary);
|
|
font-size: var(--app-text-sm);
|
|
}
|
|
|
|
// 统计指标卡
|
|
.metric-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, 1fr);
|
|
gap: 12px;
|
|
margin-bottom: 16px;
|
|
|
|
@media (max-width: 900px) {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
|
|
.metric-card {
|
|
background: var(--td-bg-color-secondarycontainer);
|
|
border-radius: var(--app-radius-md);
|
|
padding: 12px 14px;
|
|
|
|
&--wide {
|
|
grid-column: span 3;
|
|
|
|
@media (max-width: 900px) {
|
|
grid-column: span 1;
|
|
}
|
|
}
|
|
|
|
&__label {
|
|
color: var(--td-text-color-secondary);
|
|
font-size: var(--app-text-md);
|
|
}
|
|
|
|
&__value {
|
|
font-size: 26px;
|
|
font-weight: 700;
|
|
margin-top: 4px;
|
|
}
|
|
|
|
&__sub {
|
|
color: var(--td-text-color-placeholder);
|
|
font-size: var(--app-text-sm);
|
|
margin-top: 2px;
|
|
}
|
|
|
|
&__weak-list {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 6px;
|
|
margin-top: 8px;
|
|
}
|
|
}
|
|
|
|
// 报告区
|
|
.report {
|
|
border-top: 1px solid var(--td-component-stroke);
|
|
padding-top: 12px;
|
|
|
|
&__meta {
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
margin-bottom: 10px;
|
|
color: var(--td-text-color-placeholder);
|
|
font-size: var(--app-text-sm);
|
|
}
|
|
|
|
&__model {
|
|
color: var(--td-text-color-secondary);
|
|
}
|
|
|
|
&__fallback {
|
|
color: var(--td-warning-color);
|
|
}
|
|
|
|
&__time {
|
|
flex: 1;
|
|
}
|
|
|
|
// markdown 报告排版(紧凑版式,标题/列表/段落层级清晰)
|
|
&__body {
|
|
:deep(h1),
|
|
:deep(h2),
|
|
:deep(h3) {
|
|
margin: 14px 0 8px;
|
|
font-size: var(--app-text-lg);
|
|
font-weight: 600;
|
|
}
|
|
|
|
:deep(h1:first-child),
|
|
:deep(h2:first-child),
|
|
:deep(h3:first-child) {
|
|
margin-top: 0;
|
|
}
|
|
|
|
:deep(p) {
|
|
margin: 6px 0;
|
|
line-height: 1.7;
|
|
}
|
|
|
|
:deep(ul),
|
|
:deep(ol) {
|
|
margin: 6px 0;
|
|
padding-left: 22px;
|
|
|
|
li {
|
|
margin: 4px 0;
|
|
line-height: 1.6;
|
|
}
|
|
}
|
|
|
|
:deep(strong) {
|
|
font-weight: 600;
|
|
}
|
|
|
|
:deep(table) {
|
|
border-collapse: collapse;
|
|
margin: 8px 0;
|
|
|
|
th,
|
|
td {
|
|
border: 1px solid var(--td-component-stroke);
|
|
padding: 4px 10px;
|
|
font-size: var(--app-text-sm);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
.generating {
|
|
padding: 32px 0;
|
|
display: flex;
|
|
justify-content: center;
|
|
}
|
|
|
|
.tone-good { color: var(--td-success-color); }
|
|
.tone-warn { color: var(--td-warning-color); }
|
|
.tone-bad { color: var(--td-error-color); }
|
|
</style>
|