刘光辉
3 小时以前 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
<script lang="ts" setup>
import { computed, reactive, ref, watch } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { Modal as AModal } from 'ant-design-vue';
 
import { submitPersonTaskQuiz } from '#/api/x/tms/personTask';
 
export interface QuizQuestion {
  id?: string;
  sortNo?: number;
  question?: string;
}
 
const props = defineProps<{
  open: boolean;
  personTaskId?: string;
  questions?: QuizQuestion[];
}>();
 
const emit = defineEmits<{
  (e: 'update:open', v: boolean): void;
  (e: 'done', result: { score?: number; passFlag?: string; message?: string }): void;
}>();
 
const { createMessage } = useMessage();
const submitting = ref(false);
const answers = reactive<Record<string, string>>({});
 
const questionList = computed(() => (props.questions || []).filter((q) => q?.id && q.question));
 
watch(
  () => [props.open, props.questions] as const,
  () => {
    Object.keys(answers).forEach((k) => delete answers[k]);
    for (const q of questionList.value) {
      if (q.id) answers[q.id] = '';
    }
  },
  { immediate: true, deep: true },
);
 
async function handleOk() {
  if (!props.personTaskId) {
    createMessage.error('缺少个人任务');
    return;
  }
  for (const q of questionList.value) {
    if (!q.id || !String(answers[q.id] || '').trim()) {
      createMessage.warning('请完成全部提问后再提交');
      return;
    }
  }
  submitting.value = true;
  try {
    const result = await submitPersonTaskQuiz(props.personTaskId, {
      answers: questionList.value.map((q) => ({
        quizId: q.id!,
        answer: String(answers[q.id!] || '').trim(),
      })),
    });
    createMessage.success(result.message || '提交成功');
    emit('done', result);
    emit('update:open', false);
  } catch (e: any) {
    createMessage.error(e?.message || '提交失败');
  } finally {
    submitting.value = false;
  }
}
 
function handleCancel() {
  createMessage.warning('请完成提问后再关闭');
}
</script>
 
<template>
  <AModal
    :open="open"
    title="培训提问"
    :mask-closable="false"
    :keyboard="false"
    :closable="false"
    :confirm-loading="submitting"
    ok-text="提交答案"
    cancel-text="暂不关闭"
    width="640px"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <div class="quiz-popup">
      <div class="quiz-popup__tip">请完成以下提问后才能继续。答案将与参考答案自动比对判分。</div>
      <div v-for="(q, idx) in questionList" :key="q.id" class="quiz-item">
        <div class="quiz-item__q">{{ idx + 1 }}. {{ q.question }}</div>
        <a-textarea
          v-model:value="answers[q.id!]"
          :rows="2"
          :maxlength="500"
          placeholder="请输入答案"
        />
      </div>
      <div v-if="!questionList.length" class="quiz-empty">暂无题目</div>
    </div>
  </AModal>
</template>
 
<style scoped>
.quiz-popup__tip {
  margin-bottom: 12px;
  color: rgba(0, 0, 0, 0.65);
  font-size: 13px;
}
 
.quiz-item {
  margin-bottom: 12px;
}
 
.quiz-item__q {
  margin-bottom: 6px;
  font-weight: 500;
}
 
.quiz-empty {
  color: rgba(0, 0, 0, 0.45);
  text-align: center;
  padding: 24px 0;
}
</style>