fix(training-agent): 修复员工培训工作区布局交互与HR统计兜底

This commit is contained in:
runny committed 2026-07-09 20:35:29 +08:00
1 parent 39ef8a49c9
commit c81717df30
4 files changed
+165 -96

No files matched your search

@@ -3,7 +3,7 @@
* features/chat/components/TrainingWorkspacePanel.vue
* 用途:在培训 Agent 的聊天页顶部提供“员工培训 + 培训管理”一体化工作区。
* 依赖:useTrainingWorkspace、lucide 图标、聊天页 currentAgent/currentUser。
* 逻辑:只在培训 Agent 下渲染;普通员工首次进入默认展开工作区,但新建/切换会话后保持收起,避免聊天过程中被自动展开打断。
* 逻辑:只在培训 Agent 下渲染;员工与 HR 管理员首次进入默认展开工作区,但新建/切换会话后保持收起,避免聊天过程中被自动展开打断。
*/
import { computed, reactive, ref, watch, type ShallowUnwrapRef } from 'vue';
import { useI18n } from 'vue-i18n';
@@ -62,7 +62,8 @@ const stageItems = computed(() => ([
]));
const userExpandedOverride = ref<boolean | null>(null);
const shouldDefaultExpanded = computed(() => workspace.isTrainingAgent && !workspace.isAdminViewer);
// HR 管理员与普通员工首次进入培训 Agent 时都默认展开工作区;新建对话仍按既有约束强制收起。
const shouldDefaultExpanded = computed(() => workspace.isTrainingAgent);
const handleVideoTimeUpdate = (e: Event) => {
const video = e.target as HTMLVideoElement;
@@ -109,7 +110,7 @@ const applyDefaultExpandedState = () => {
emit('update:expanded', nextExpanded);
};
// 监听 Agent/角色切换,重置本地展开态:员工默认展开工作区,培训管理角色默认保留问答视图。
// 监听 Agent/角色切换,重置本地展开态:培训角色首次进入默认展开工作区。
watch(
() => [props.currentAgent?.id, workspace.isAdminViewer],
() => {
@@ -130,6 +131,20 @@ watch(
);
const isCompactMode = computed(() => !workspaceExpanded.value);
const isLearnerExamStage = computed(() => !isCompactMode.value && workspace.activeView === 'learner' && workspace.stage === 'exam');
const learnerWorkspaceGridClass = computed(() => (
'grid gap-2.5 p-3 md:grid-cols-[minmax(0,1.7fr)_208px] md:p-4 lg:gap-3 lg:grid-cols-[minmax(0,1.85fr)_220px] 2xl:grid-cols-[minmax(0,1.95fr)_240px]'
));
const learnerWorkspaceMainClass = computed(() => 'min-w-0 space-y-3');
const progressCardClass = computed(() => 'rounded-2xl border border-default bg-white p-2.5 shadow-sm');
const examPanelClass = computed(() => (
isLearnerExamStage.value
? 'rounded-2xl border border-default bg-white p-3.5 shadow-sm'
: 'rounded-2xl border border-default bg-white p-4 shadow-sm'
));
const questionNavPanelClass = computed(() => 'rounded-2xl border border-default bg-white p-2.5 shadow-sm');
const questionNavGridClass = computed(() => 'mt-2 grid grid-cols-4 gap-1');
const questionNavButtonClass = computed(() => 'flex h-7 items-center justify-center rounded-lg border text-[11px] font-semibold transition');
const compactSummary = computed(() => {
if (workspace.activeView === 'admin') {
@@ -146,7 +161,6 @@ const compactSummary = computed(() => {
return workspace.isPassed ? t('chat.training.result.passTitle') : t('chat.training.result.failTitle');
});
const trainingTitle = computed(() => String(t('chat.training.title') || '').trim());
const ruleItems = computed(() => ([
t('chat.training.rules.rule1', { total: workspace.totalQuestionCount }),
t('chat.training.rules.rule2'),
@@ -158,11 +172,24 @@ const learnerIdentityItems = computed(() => ([
{ key: 'employee-id', label: t('chat.training.learnerCard.employeeId'), value: workspace.currentUserEmployeeId },
{ key: 'department', label: t('chat.training.learnerCard.department'), value: workspace.currentUserDepartment },
]));
const examQuestionItems = computed(() => Array.from({ length: workspace.totalQuestionCount }, (_, index) => ({
index,
label: String(index + 1).padStart(2, '0'),
isActive: workspace.currentQuestionIndex === index,
})));
const answeredQuestionMap = computed(() => new Map(
workspace.answerRecords.map((item) => [item.questionId, item])
));
const examQuestionItems = computed(() => Array.from({ length: workspace.totalQuestionCount }, (_, index) => {
const isActive = workspace.stage === 'exam' && workspace.currentQuestionIndex === index;
const questionId = workspace.questionBank[index]?.id;
const answerRecord = questionId ? answeredQuestionMap.value.get(questionId) ?? null : null;
return {
index,
label: String(index + 1).padStart(2, '0'),
isActive,
status: isActive
? 'active'
: answerRecord
? answerRecord.correct ? 'correct' : 'wrong'
: 'pending',
};
}));
const canNavigateExamQuestions = computed(() => workspace.stage === 'exam');
const jumpToExamQuestion = (index: number) => {
if (!canNavigateExamQuestions.value) return;
@@ -186,20 +213,11 @@ const handleStageCardClick = (stageKey: string) => {
>
<div
v-if="isCompactMode"
class="flex flex-col gap-2 bg-white/90 px-4 py-2.5 backdrop-blur-sm md:px-5"
class="flex flex-col gap-1.5 bg-white/90 px-4 py-2 backdrop-blur-sm md:px-5"
>
<div class="flex flex-col gap-2 xl:flex-row xl:items-center xl:justify-between">
<div class="flex flex-col gap-1.5 xl:flex-row xl:items-center xl:justify-between">
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<div class="inline-flex items-center gap-2 rounded-full bg-brand-50 px-3 py-1 text-[11px] font-semibold text-brand-700">
<BookOpen class="h-3.5 w-3.5" />
{{ $t('chat.training.badge') }}
</div>
<div v-if="trainingTitle" class="text-sm font-semibold text-primary md:text-base">
{{ trainingTitle }}
</div>
</div>
<div class="mt-1.5 flex flex-wrap gap-2">
<div class="flex flex-wrap gap-2">
<button
type="button"
class="rounded-full px-3 py-1.5 text-xs font-medium transition"
@@ -250,7 +268,7 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div class="flex flex-col gap-2 rounded-2xl border border-brand-100 bg-brand-50/40 px-4 py-2.5 md:flex-row md:items-center md:justify-between">
<div class="flex flex-col gap-2 rounded-2xl border border-brand-100 bg-brand-50/40 px-3.5 py-2 md:flex-row md:items-center md:justify-between">
<div class="min-w-0">
<div class="truncate text-sm font-semibold text-primary">
{{ compactSummary }}
@@ -282,20 +300,10 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div v-else class="flex flex-col gap-2 bg-white/90 px-4 py-2.5 backdrop-blur-sm md:px-5">
<div class="flex flex-col gap-2 xl:flex-row xl:items-center xl:justify-between">
<div v-else class="flex flex-col gap-1.5 bg-white/90 px-4 py-2 backdrop-blur-sm md:px-5">
<div class="flex flex-col gap-1.5 xl:flex-row xl:items-center xl:justify-between">
<div class="min-w-0">
<div class="flex flex-wrap items-center gap-2">
<div class="inline-flex items-center gap-2 rounded-full bg-brand-50 px-3 py-1 text-[11px] font-semibold text-brand-700">
<BookOpen class="h-3.5 w-3.5" />
{{ $t('chat.training.badge') }}
</div>
<div v-if="trainingTitle" class="text-sm font-semibold text-primary md:text-base">
{{ trainingTitle }}
</div>
</div>
<div class="mt-1.5 flex flex-wrap gap-2">
<div class="flex flex-wrap gap-2">
<button
type="button"
class="rounded-full px-3 py-1.5 text-xs font-medium transition"
@@ -337,7 +345,7 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div class="flex flex-col gap-2 rounded-2xl border border-brand-100 bg-brand-50/40 px-4 py-2.5 md:flex-row md:items-center md:justify-between">
<div class="flex flex-col gap-2 rounded-2xl border border-brand-100 bg-brand-50/40 px-3.5 py-2 md:flex-row md:items-center md:justify-between">
<div class="min-w-0">
<div class="truncate text-sm font-semibold text-primary">
{{ compactSummary }}
@@ -367,29 +375,29 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div v-if="!isCompactMode && workspace.activeView === 'learner'" class="grid gap-3 p-4 md:grid-cols-[minmax(0,1.25fr)_232px] md:p-5 lg:gap-4 lg:grid-cols-[minmax(0,1.4fr)_260px] 2xl:grid-cols-[minmax(0,1.55fr)_300px]">
<div class="min-w-0 space-y-4">
<div class="rounded-2xl border border-default bg-white p-4 shadow-sm">
<div class="flex flex-col gap-2.5 lg:flex-row lg:items-start lg:justify-between">
<div v-if="!isCompactMode && workspace.activeView === 'learner'" :class="learnerWorkspaceGridClass">
<div :class="learnerWorkspaceMainClass">
<div :class="progressCardClass">
<div class="flex flex-col gap-1.5 lg:flex-row lg:items-start lg:justify-between">
<div>
<div class="text-xs font-semibold uppercase tracking-[0.18em] text-brand-600">
{{ $t('chat.training.progressLabel') }}
</div>
<div class="mt-1 text-xs leading-5 text-secondary md:text-sm">
<div class="mt-0.5 text-[11px] leading-4.5 text-secondary md:text-xs">
{{ $t('chat.training.progressHint') }}
</div>
</div>
<div class="rounded-xl bg-surface-soft px-3 py-2 text-center">
<div class="rounded-lg bg-surface-soft px-2.5 py-1 text-center">
<div class="text-xs text-muted">{{ $t('chat.training.progressPercent') }}</div>
<div class="text-lg font-semibold leading-none text-primary">{{ workspace.learnerProgressPercent }}%</div>
<div class="text-base font-semibold leading-none text-primary">{{ workspace.learnerProgressPercent }}%</div>
</div>
</div>
<div class="mt-3 grid grid-cols-2 gap-2 lg:grid-cols-4">
<div class="mt-2 grid grid-cols-2 gap-1 lg:grid-cols-4">
<div
v-for="(item, index) in stageItems"
:key="item.key"
class="rounded-xl border px-3 py-2.5 transition cursor-pointer hover:border-brand-300"
class="cursor-pointer rounded-xl border px-2 py-1.5 transition hover:border-brand-300"
:class="[
stageStep >= index + 1 ? 'border-brand-200 bg-brand-50/70' : 'border-default bg-surface-soft/70',
canSwitchToStage(String(item.key)) ? 'cursor-pointer hover:border-brand-300' : 'cursor-not-allowed opacity-60',
@@ -398,16 +406,16 @@ const handleStageCardClick = (stageKey: string) => {
>
<div class="flex items-center gap-2">
<div
class="flex h-8 w-8 shrink-0 items-center justify-center rounded-lg"
class="flex h-6 w-6 shrink-0 items-center justify-center rounded-lg"
:class="stageStep >= index + 1 ? 'bg-brand-600 text-white' : 'bg-white text-muted'"
>
<component :is="item.icon" class="h-4 w-4" />
<component :is="item.icon" class="h-3 w-3" />
</div>
<div class="min-w-0">
<div class="text-[11px] font-semibold uppercase tracking-[0.16em] text-muted">
<div class="text-[10px] font-semibold uppercase tracking-[0.14em] text-muted">
{{ String(index + 1).padStart(2, '0') }}
</div>
<div class="whitespace-normal text-sm font-medium leading-5 text-primary">{{ $t(item.label) }}</div>
<div class="whitespace-normal text-[12px] font-medium leading-4 text-primary">{{ $t(item.label) }}</div>
</div>
</div>
</div>
@@ -415,20 +423,11 @@ const handleStageCardClick = (stageKey: string) => {
</div>
<div v-if="workspace.stage === 'video'" class="rounded-2xl border border-default bg-white p-4 shadow-sm">
<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
<div>
<div>
<h3 class="text-base font-semibold text-primary md:text-lg">{{ $t('chat.training.video.title') }}</h3>
<p class="mt-1 text-sm leading-6 text-secondary">{{ $t('chat.training.video.description') }}</p>
</div>
<button
v-if="trainingExamPdfGuide"
type="button"
class="inline-flex items-center justify-center gap-2 rounded-xl border border-default bg-white px-4 py-2.5 text-sm font-medium text-secondary transition hover:bg-surface-soft"
@click="emit('open-guide', Number(trainingExamPdfGuide.id))"
>
<BookOpen class="h-4 w-4" />
{{ $t('chat.training.actions.viewExamPdf') }}
</button>
</div>
<div class="mt-3 relative group cursor-pointer overflow-hidden rounded-2xl border border-default bg-slate-900 shadow-lg aspect-video flex items-center justify-center" @click="showVideoModal = true">
<img src="https://coresg-normal.trae.ai/api/ide/v1/text_to_image?prompt=Professional%20corporate%20training%20video%20cover%2C%20high-quality%2C%20modern%20office%20background%2C%20play%20button%20overlay&image_size=landscape_16_9" class="absolute inset-0 w-full h-full object-cover opacity-60 group-hover:scale-105 transition-transform duration-500" />
@@ -448,15 +447,6 @@ const handleStageCardClick = (stageKey: string) => {
<p class="mt-1 text-sm leading-6 text-secondary">{{ $t('chat.training.summary.description') }}</p>
</div>
<div class="flex flex-wrap gap-2">
<button
v-if="trainingExamPdfGuide"
type="button"
class="inline-flex items-center justify-center gap-2 rounded-xl border border-default bg-white px-4 py-2.5 text-sm font-medium text-secondary transition hover:bg-surface-soft"
@click="emit('open-guide', Number(trainingExamPdfGuide.id))"
>
<BookOpen class="h-4 w-4" />
{{ $t('chat.training.actions.viewExamPdf') }}
</button>
<button
type="button"
class="inline-flex items-center justify-center gap-2 rounded-xl bg-brand-600 px-4 py-2.5 text-sm font-medium text-white transition hover:bg-brand-700"
@@ -491,38 +481,38 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div v-else-if="workspace.stage === 'exam' && workspace.currentQuestion" class="rounded-2xl border border-default bg-white p-4 shadow-sm">
<div class="flex flex-col gap-3 md:flex-row md:items-center md:justify-between">
<div v-else-if="workspace.stage === 'exam' && workspace.currentQuestion" :class="examPanelClass">
<div class="flex flex-col gap-2.5 md:flex-row md:items-center md:justify-between">
<div>
<div class="text-xs font-semibold uppercase tracking-[0.18em] text-brand-600">
{{ $t('chat.training.exam.title') }}
</div>
<h3 class="mt-1 text-lg font-semibold text-primary">
<h3 class="mt-1 text-base font-semibold text-primary md:text-lg">
{{ $t('chat.training.exam.questionCounter', { current: workspace.currentQuestionNumber, total: workspace.totalQuestionCount }) }}
</h3>
</div>
<div class="flex flex-wrap gap-2 text-sm text-secondary">
<div class="rounded-xl bg-surface-soft px-3 py-1.5">
<div class="flex flex-wrap gap-1.5 text-xs text-secondary md:text-sm">
<div class="rounded-xl bg-surface-soft px-2.5 py-1.5">
{{ $t('chat.training.exam.currentScore', { score: workspace.score }) }}
</div>
<div class="rounded-xl bg-surface-soft px-3 py-1.5">
<div class="rounded-xl bg-surface-soft px-2.5 py-1.5">
{{ $t('chat.training.exam.passLine', { passScore: workspace.passScore }) }}
</div>
</div>
</div>
<div class="mt-4 rounded-2xl border border-brand-100 bg-brand-50/40 p-3.5">
<div class="text-base font-semibold leading-7 text-primary">
<div class="mt-3 rounded-2xl border border-brand-100 bg-brand-50/40 p-3">
<div class="text-[15px] font-semibold leading-6 text-primary md:text-base">
{{ workspace.currentQuestion.prompt }}
</div>
</div>
<div class="mt-3 grid gap-2.5">
<div class="mt-2.5 grid gap-2">
<button
v-for="option in workspace.currentQuestion.options"
:key="option.id"
type="button"
class="rounded-2xl border px-4 py-2.5 text-left text-sm transition"
class="rounded-2xl border px-3.5 py-2 text-left text-sm leading-5 transition"
:class="[
workspace.selectedOptionId === option.id
? workspace.answerLocked
@@ -548,7 +538,7 @@ const handleStageCardClick = (stageKey: string) => {
<div
v-if="workspace.answerLocked"
class="mt-4 rounded-2xl border p-3.5"
class="mt-3 rounded-2xl border p-3"
:class="workspace.currentFeedbackCorrect ? 'border-emerald-200 bg-emerald-50/70' : 'border-red-200 bg-red-50/70'"
>
<div class="flex items-center gap-2 text-sm font-semibold" :class="workspace.currentFeedbackCorrect ? 'text-emerald-700' : 'text-red-700'">
@@ -568,7 +558,7 @@ const handleStageCardClick = (stageKey: string) => {
<div class="mt-3">
<button
type="button"
class="inline-flex items-center gap-2 rounded-xl bg-brand-600 px-4 py-2.5 text-sm font-medium text-white transition hover:bg-brand-700"
class="inline-flex items-center gap-2 rounded-xl bg-brand-600 px-4 py-2 text-sm font-medium text-white transition hover:bg-brand-700"
@click="workspace.examReadyToSubmit ? workspace.submitExam() : workspace.moveToNextQuestion()"
>
<ClipboardCheck class="h-4 w-4" />
@@ -640,27 +630,30 @@ const handleStageCardClick = (stageKey: string) => {
</div>
</div>
<div class="min-w-0 md:sticky md:top-4 md:self-start">
<div class="rounded-2xl border border-default bg-white p-3 shadow-sm">
<div class="min-w-0 md:sticky md:top-3 md:self-start">
<div :class="questionNavPanelClass">
<div class="flex items-center gap-2">
<ClipboardCheck class="h-4 w-4 text-brand-600" />
<div class="text-sm font-semibold text-primary">{{ $t('chat.training.examQuestionCard.title') }}</div>
</div>
<div class="mt-2 text-xs leading-5 text-secondary">
<div class="mt-1.5 text-[11px] leading-4.5 text-secondary">
{{ $t('chat.training.examQuestionCard.hint') }}
</div>
<div class="mt-3 grid grid-cols-4 gap-2">
<div :class="questionNavGridClass">
<button
v-for="item in examQuestionItems"
:key="item.index"
type="button"
class="flex h-10 items-center justify-center rounded-xl border text-sm font-semibold transition"
:class="[
item.isActive
questionNavButtonClass,
item.status === 'active'
? 'border-brand-500 bg-brand-600 text-white shadow-sm'
: canNavigateExamQuestions
? 'border-brand-200 bg-brand-50/60 text-brand-700 hover:bg-brand-100'
: 'cursor-not-allowed border-default bg-surface-soft text-muted',
: item.status === 'correct'
? 'border-emerald-200 bg-emerald-50 text-emerald-700 hover:bg-emerald-100'
: item.status === 'wrong'
? 'border-red-200 bg-red-50 text-red-700 hover:bg-red-100'
: 'border-default bg-surface-soft text-muted hover:border-slate-300 hover:bg-slate-100',
!canNavigateExamQuestions ? 'cursor-not-allowed opacity-80' : '',
]"
:disabled="!canNavigateExamQuestions"
@click="jumpToExamQuestion(item.index)"
@@ -669,10 +662,10 @@ const handleStageCardClick = (stageKey: string) => {
</button>
</div>
<div class="my-3 h-px bg-slate-100" />
<div class="my-2.5 h-px bg-slate-100" />
<div class="text-sm font-semibold text-primary">{{ $t('chat.training.rules.title') }}</div>
<ul class="mt-2 grid gap-1 text-xs leading-5 text-secondary">
<ul class="mt-1.5 grid gap-1 text-[11px] leading-4.5 text-secondary">
<li v-for="rule in ruleItems" :key="rule" class="flex gap-2">
<CheckCircle2 class="mt-0.5 h-3.5 w-3.5 shrink-0 text-emerald-500" />
<span>{{ rule }}</span>
@@ -706,6 +706,7 @@ export function useTrainingWorkspace(options: UseTrainingWorkspaceOptions) {
currentQuestionIndex,
currentQuestionNumber,
currentQuestion,
questionBank,
totalQuestionCount,
passScore,
answeredCount,