刘光辉
6 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<script lang="ts" setup>
import type { PersonTaskItem } from './types';
 
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
 
import { useMessage } from '@jnpf/hooks';
import {
  Descriptions as ADescriptions,
  DescriptionsItem as ADescriptionsItem,
  Modal as AModal,
} from 'ant-design-vue';
 
import {
  getPersonTaskInfo,
  getPersonTaskQuizAnswers,
  signPersonTask,
} from '#/api/x/tms/personTask';
import type { TmsQuizAnswerPaper } from '#/views/x/tms/record/types';
import QuizPaperView from '#/views/x/tms/shared/QuizPaperView.vue';
import QuizPopup from '#/views/x/tms/shared/QuizPopup.vue';
import { TMS_BTN } from '#/views/x/tms/shared/ui';
 
import {
  labelOfCategory,
  labelOfEvalMode,
  labelOfPassFlag,
  labelOfPersonStatus,
  labelOfTrainMode,
  labelOfYesNo,
  loadPersonTaskDics,
} from './constants';
 
import '#/views/x/tms/shared/page.css';
 
defineOptions({ name: 'TmsPersonTaskDetail' });
 
const route = useRoute();
const router = useRouter();
const { createMessage } = useMessage();
 
const loading = ref(false);
const signing = ref(false);
const detail = ref<PersonTaskItem | null>(null);
const tipRemainSeconds = ref(0);
const quizOpen = ref(false);
const quizQuestions = ref<Array<{ id?: string; sortNo?: number; question?: string }>>([]);
const quizPaperOpen = ref(false);
const quizPaperLoading = ref(false);
const quizPaper = ref<TmsQuizAnswerPaper | null>(null);
const hasQuizPaper = ref(false);
let tipTimer: ReturnType<typeof setInterval> | null = null;
 
const progressText = computed(() => {
  const learned = detail.value?.learnedSeconds || 0;
  const required = detail.value?.requiredSeconds || 0;
  return `${formatDuration(learned)} / ${required > 0 ? formatDuration(required) : '未配置'}`;
});
 
const isClosed = computed(() => detail.value?.bizStatus === 'cancelled' || detail.value?.bizStatus === 'expired');
const isSigned = computed(() => Boolean(detail.value?.signed || detail.value?.signTime));
const canLearn = computed(() => Boolean(detail.value?.id) && !isClosed.value && isSigned.value);
const showExam = computed(() => Boolean(detail.value?.canExam && detail.value?.paperId));
const tipReady = computed(() => tipRemainSeconds.value <= 0);
const statusAlert = computed(() => {
  if (!detail.value) return '';
  if (detail.value.bizStatus === 'cancelled') return '任务已取消,无法继续学习或考试。';
  if (detail.value.bizStatus === 'expired') return '任务已过期,无法继续学习或考试。';
  return '';
});
 
onMounted(async () => {
  await loadPersonTaskDics();
  await loadData();
});
 
onBeforeUnmount(() => {
  stopTipTimer();
});
 
watch(
  () => [detail.value?.canSign, detail.value?.tipSeconds, detail.value?.signed] as const,
  () => {
    startTipCountdown();
  },
);
 
function startTipCountdown() {
  stopTipTimer();
  if (!detail.value?.canSign || detail.value?.signed) {
    tipRemainSeconds.value = 0;
    return;
  }
  const tip = Number(detail.value?.tipSeconds || 0);
  if (tip <= 0) {
    tipRemainSeconds.value = 0;
    return;
  }
  tipRemainSeconds.value = tip;
  tipTimer = setInterval(() => {
    if (tipRemainSeconds.value <= 1) {
      tipRemainSeconds.value = 0;
      stopTipTimer();
      return;
    }
    tipRemainSeconds.value -= 1;
  }, 1000);
}
 
function stopTipTimer() {
  if (tipTimer) {
    clearInterval(tipTimer);
    tipTimer = null;
  }
}
 
async function loadData() {
  const id = String(route.params.id || '');
  if (!id) {
    router.replace('/tms/personTask');
    return;
  }
  loading.value = true;
  try {
    detail.value = await getPersonTaskInfo(id);
    hasQuizPaper.value = false;
    if (detail.value?.evalMode === 'quiz' && detail.value.passFlag) {
      try {
        await getPersonTaskQuizAnswers(id);
        hasQuizPaper.value = true;
      } catch {
        hasQuizPaper.value = false;
      }
    }
  } catch (e: any) {
    createMessage.error(e?.message || '加载失败');
    router.replace('/tms/personTask');
  } finally {
    loading.value = false;
  }
}
 
function goBack() {
  router.push('/tms/personTask');
}
 
function goLearn() {
  if (!detail.value?.id || !canLearn.value) return;
  router.push(`/tms/personTask/learn/${detail.value.id}`);
}
 
function goExam() {
  if (!detail.value?.id || !showExam.value) return;
  router.push(`/tms/personTask/learn/${detail.value.id}`);
}
 
async function handleSign() {
  if (!detail.value?.id || !detail.value.canSign || signing.value || !tipReady.value) return;
  signing.value = true;
  try {
    const result = await signPersonTask(detail.value.id);
    detail.value.signTime = result.signTime;
    detail.value.signed = result.signed ?? true;
    detail.value.canSign = result.canSign ?? false;
    detail.value.signTip = result.signTip || '已签到';
    detail.value.bizStatus = result.bizStatus || detail.value.bizStatus;
    detail.value.canExam = result.canExam;
    detail.value.examTip = result.examTip;
    tipRemainSeconds.value = 0;
    stopTipTimer();
    createMessage.success(result.message || '签到成功');
    if (result.needQuiz && result.quizQuestions?.length) {
      quizQuestions.value = result.quizQuestions;
      quizOpen.value = true;
    }
  } catch (e: any) {
    createMessage.error(e?.message || '签到失败');
  } finally {
    signing.value = false;
  }
}
 
async function onQuizDone(result: { passFlag?: string }) {
  if (detail.value) {
    detail.value.passFlag = result.passFlag || detail.value.passFlag;
    await loadData();
  }
}
 
async function openMyQuizPaper() {
  const id = String(detail.value?.id || route.params.id || '');
  if (!id) return;
  quizPaperLoading.value = true;
  quizPaperOpen.value = true;
  quizPaper.value = null;
  try {
    quizPaper.value = await getPersonTaskQuizAnswers(id);
    hasQuizPaper.value = true;
  } catch (e: any) {
    quizPaperOpen.value = false;
    createMessage.error(e?.message || '加载提问答卷失败');
  } finally {
    quizPaperLoading.value = false;
  }
}
 
function formatDuration(seconds: number) {
  const h = Math.floor(seconds / 3600);
  const m = Math.floor((seconds % 3600) / 60);
  const s = seconds % 60;
  if (h > 0) return `${h}小时${m}分`;
  if (m > 0) return `${m}分${s}秒`;
  return `${s}秒`;
}
</script>
 
<template>
  <div class="jnpf-content-wrapper tms-person-task-page" v-loading="loading">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content tms-person-task-wrap" v-if="detail">
        <div class="tms-page-header">
          <div>
            <div class="tms-page-header__title">个人培训任务</div>
            <div class="tms-page-header__sub">{{ detail.taskNo || '-' }} · {{ detail.subject || '-' }}</div>
          </div>
          <div class="tms-page-header__actions">
            <a-button @click="goBack">{{ TMS_BTN.back }}</a-button>
            <a-button
              v-if="detail.canSign"
              type="primary"
              :loading="signing"
              :disabled="!tipReady"
              @click="handleSign"
            >
              {{ tipReady ? TMS_BTN.sign : `签到(${tipRemainSeconds}s)` }}
            </a-button>
            <a-button v-if="canLearn" type="primary" :ghost="showExam" @click="goLearn">
              {{ TMS_BTN.enterLearn }}
            </a-button>
            <a-button v-if="showExam" type="primary" @click="goExam">
              {{ detail.passFlag === '0' || detail.passFlag === 'fail' ? TMS_BTN.retake : TMS_BTN.enterExam }}
            </a-button>
            <a-button v-if="hasQuizPaper" @click="openMyQuizPaper">查看提问答卷</a-button>
          </div>
        </div>
 
        <a-alert v-if="statusAlert" class="tms-page-tip" type="warning" show-icon :message="statusAlert" />
 
        <ADescriptions bordered :column="2" size="small">
          <ADescriptionsItem label="培训编号">{{ detail.taskNo || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="状态">{{ labelOfPersonStatus(detail.bizStatus) }}</ADescriptionsItem>
          <ADescriptionsItem label="培训主题" :span="2">{{ detail.subject || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="培训分类">{{ labelOfCategory(detail.category) }}</ADescriptionsItem>
          <ADescriptionsItem label="培训方式">{{ labelOfTrainMode(detail.trainMode) }}</ADescriptionsItem>
          <ADescriptionsItem label="考核方式">{{ labelOfEvalMode(detail.evalMode) }}</ADescriptionsItem>
          <ADescriptionsItem label="签到状态">
            {{
              detail.signed || detail.signTime
                ? `已签到${detail.signTime ? `(${detail.signTime})` : ''}`
                : '未签到'
            }}
          </ADescriptionsItem>
          <ADescriptionsItem label="是否可考">
            {{ labelOfYesNo(detail.canExam) }}
            <span v-if="isSigned && !detail.canExam && detail.examTip" class="tip-inline">({{ detail.examTip }})</span>
          </ADescriptionsItem>
          <ADescriptionsItem label="学习进度">{{ detail.learnStatus || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="要求课时">{{ detail.requiredHours ?? '-' }} 小时</ADescriptionsItem>
          <ADescriptionsItem label="已学/需学">{{ progressText }}</ADescriptionsItem>
          <ADescriptionsItem label="开始时间">{{ detail.startTime || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="结束时间">{{ detail.endTime || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="关闭时间">{{ detail.closeTime || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="地点">{{ detail.placeName || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="试卷">{{ detail.paperName || '-' }}</ADescriptionsItem>
          <ADescriptionsItem label="成绩">{{ detail.examScore ?? '-' }}</ADescriptionsItem>
          <ADescriptionsItem v-if="detail.evalMode === 'quiz'" label="是否合格">
            {{ labelOfPassFlag(detail.passFlag) }}
          </ADescriptionsItem>
          <ADescriptionsItem label="培训要点" :span="2">{{ detail.keyPoints || '-' }}</ADescriptionsItem>
        </ADescriptions>
      </div>
    </div>
 
    <QuizPopup
      v-model:open="quizOpen"
      :person-task-id="detail?.id"
      :questions="quizQuestions"
      @done="onQuizDone"
    />
 
    <AModal
      v-model:open="quizPaperOpen"
      title="我的提问答卷"
      width="800px"
      :footer="null"
      :destroy-on-close="true"
      wrap-class-name="tms-quiz-paper-modal"
    >
      <QuizPaperView :paper="quizPaper" :loading="quizPaperLoading" answer-label="我的答案" />
    </AModal>
  </div>
</template>
 
<style scoped>
.tms-person-task-page,
.tms-person-task-wrap {
  height: 100%;
  min-height: 0;
}
 
.tms-person-task-wrap {
  overflow: auto;
  background: #fff;
  padding: 16px 20px 24px;
  box-sizing: border-box;
}
 
.tip-inline {
  color: #fa8c16;
  margin-left: 4px;
}
</style>