刘光辉
7 小时以前 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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<script lang="ts" setup>
import type { QuestionBankOption, QuestionFormModel } from './types';
 
import { computed, onMounted, reactive, ref, watch } from 'vue';
import { useRoute, useRouter } from 'vue-router';
 
import { useMessage } from '@jnpf/hooks';
 
import { createQuestion, getQuestionBanks, getQuestionInfo, updateQuestion } from '#/api/x/tms/question';
 
import AnswerSettings from './components/AnswerSettings.vue';
import {
  DIFFICULTY_OPTIONS,
  LIFE_TYPE_OPTIONS,
  QUESTION_TYPE_OPTIONS,
  SOURCE_OPTIONS,
  STATUS_OPTIONS,
  loadQuestionDics,
} from './constants';
import { TMS_DIC_FIELD_NAMES } from '#/views/x/tms/shared/dic';
import {
  createDefaultOptions,
  hydrateFormFromEntity,
  parseFormToEntity,
  resetAnswerForType,
  validateAnswerByType,
} from './parseAnswer';
 
defineOptions({ name: 'TmsQuestionForm' });
 
const route = useRoute();
const router = useRouter();
const { createMessage } = useMessage();
 
const banks = ref<QuestionBankOption[]>([]);
const loading = ref(false);
const submitting = ref(false);
const formRef = ref();
 
const isEdit = computed(() => !!route.params.id && route.params.id !== 'create');
 
const dataForm = reactive<QuestionFormModel>({
  questionType: 'single',
  bankId: undefined,
  difficulty: 'normal',
  sourceType: 'self',
  lifeType: 'long',
  expireTime: undefined,
  bizStatus: 'open',
  stem: '',
  analysis: '',
  options: createDefaultOptions('single'),
  judgeAnswer: true,
  essayAnswer: '',
  blankMixMode: false,
});
 
const statusTip = computed(
  () => STATUS_OPTIONS.value.find((x) => (x.enCode || x.id) === dataForm.bizStatus)?.tip,
);
const statusTipColor = computed(
  () => STATUS_OPTIONS.value.find((x) => (x.enCode || x.id) === dataForm.bizStatus)?.tipColor,
);
const isTempLife = computed(() => dataForm.lifeType === 'temp');
 
const rules = {
  questionType: [{ required: true, message: '请选择题型', trigger: 'change' }],
  bankId: [{ required: true, message: '请选择所属题库', trigger: 'change' }],
  lifeType: [{ required: true, message: '请选择时效类型', trigger: 'change' }],
  expireTime: [
    {
      validator: async (_rule: any, value: string) => {
        if (dataForm.lifeType === 'temp' && !value) {
          throw new Error('临时试题请填写过期时间');
        }
      },
      trigger: 'change',
    },
  ],
  stem: [{ required: true, message: '请填写题干内容', trigger: 'blur' }],
};
 
watch(
  () => dataForm.questionType,
  (type, prev) => {
    if (!prev || type === prev) return;
    Object.assign(dataForm, resetAnswerForType(type));
  },
);
 
watch(
  () => dataForm.lifeType,
  (type) => {
    if (type !== 'temp') {
      dataForm.expireTime = undefined;
    }
  },
);
 
onMounted(async () => {
  await loadQuestionDics();
  banks.value = (await getQuestionBanks()) || [];
  if (isEdit.value) {
    await loadDetail(String(route.params.id));
  }
});
 
async function loadDetail(id: string) {
  loading.value = true;
  try {
    const info = await getQuestionInfo(id);
    Object.assign(dataForm, hydrateFormFromEntity(info));
  } catch (e: any) {
    createMessage.error(e?.message || '加载失败');
  } finally {
    loading.value = false;
  }
}
 
function goList() {
  router.push('/tms/question');
}
 
async function handleSubmit() {
  try {
    await formRef.value?.validate();
  } catch {
    return;
  }
  const err = validateAnswerByType(dataForm);
  if (err) {
    createMessage.warning(err);
    return;
  }
  submitting.value = true;
  try {
    const payload = parseFormToEntity(dataForm);
    if (isEdit.value) {
      await updateQuestion(payload as any);
      createMessage.success('更新成功');
    } else {
      await createQuestion(payload as any);
      createMessage.success('创建成功');
    }
    goList();
  } catch (e: any) {
    createMessage.error(e?.message || '保存失败');
  } finally {
    submitting.value = false;
  }
}
 
function handleReset() {
  if (isEdit.value && dataForm.id) {
    loadDetail(dataForm.id);
    return;
  }
  Object.assign(dataForm, {
    questionType: 'single',
    bankId: undefined,
    difficulty: 'normal',
    sourceType: 'self',
    lifeType: 'long',
    expireTime: undefined,
    bizStatus: 'open',
    stem: '',
    analysis: '',
    ...resetAnswerForType('single'),
  });
  formRef.value?.clearValidate?.();
}
</script>
 
<template>
  <div class="jnpf-content-wrapper tms-question-form-page">
    <div class="jnpf-content-wrapper-center">
      <div class="jnpf-content-wrapper-content tms-question-form-wrap">
        <div class="tms-question-form-header">
          <div>
            <div class="text-base font-medium">{{ isEdit ? '编辑试题' : '创建试题' }}</div>
            <div class="mt-1 text-gray-400 text-sm">填写下列基本信息,{{ isEdit ? '保存试题' : '创建试题' }}。</div>
          </div>
          <a-space>
            <a-button @click="goList">管理试题</a-button>
            <a-button type="primary" :loading="submitting" @click="handleSubmit">
              {{ isEdit ? '保存试题' : '创建试题' }}
            </a-button>
          </a-space>
        </div>
 
        <div class="tms-question-form-body">
          <a-spin :spinning="loading">
            <a-form
              ref="formRef"
              :model="dataForm"
              :rules="rules"
              :label-col="{ style: { width: '100px' } }"
              class="max-w-[1100px] pb-8"
            >
              <a-row :gutter="16">
                <a-col :span="12">
                  <a-form-item label="试题类型" name="questionType">
                    <jnpf-select
                      v-model:value="dataForm.questionType"
                      :options="QUESTION_TYPE_OPTIONS"
                      :field-names="TMS_DIC_FIELD_NAMES"
                      :allow-clear="false"
                      placeholder="请选择题型"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="所属题库" name="bankId">
                    <jnpf-select
                      v-model:value="dataForm.bankId"
                      :options="banks"
                      show-search
                      placeholder="请选择所属题库"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="试题难度" name="difficulty">
                    <jnpf-select
                      v-model:value="dataForm.difficulty"
                      :options="DIFFICULTY_OPTIONS"
                      :field-names="TMS_DIC_FIELD_NAMES"
                      :allow-clear="false"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="试题状态" name="bizStatus">
                    <div class="flex items-center gap-3">
                      <jnpf-select
                        v-model:value="dataForm.bizStatus"
                        :options="STATUS_OPTIONS"
                        :field-names="TMS_DIC_FIELD_NAMES"
                        :allow-clear="false"
                        class="!w-[200px]"
                      />
                      <span
                        v-if="statusTip"
                        class="text-sm"
                        :class="statusTipColor === 'red' ? 'text-red-500' : 'text-green-600'"
                      >{{ statusTip }}</span>
                    </div>
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="试题来源" name="sourceType">
                    <jnpf-select
                      v-model:value="dataForm.sourceType"
                      :options="SOURCE_OPTIONS"
                      :field-names="TMS_DIC_FIELD_NAMES"
                      :allow-clear="false"
                    />
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="时效类型" name="lifeType">
                    <jnpf-select
                      v-model:value="dataForm.lifeType"
                      :options="LIFE_TYPE_OPTIONS"
                      :field-names="TMS_DIC_FIELD_NAMES"
                      :allow-clear="false"
                      placeholder="请选择时效类型"
                    />
                  </a-form-item>
                </a-col>
                <a-col v-if="isTempLife" :span="12">
                  <a-form-item label="过期时间" name="expireTime">
                    <jnpf-date-picker
                      v-model:value="dataForm.expireTime"
                      format="YYYY-MM-DD HH:mm:ss"
                      placeholder="请选择过期时间"
                      style="width: 100%"
                    />
                  </a-form-item>
                </a-col>
              </a-row>
 
              <a-form-item label="题干内容" name="stem">
                <div class="tms-question-editor">
                  <jnpf-editor v-model:value="dataForm.stem" />
                </div>
              </a-form-item>
 
              <a-form-item label="答案设置" required>
                <AnswerSettings v-model="dataForm" />
              </a-form-item>
 
              <a-form-item label="试题解析" name="analysis">
                <a-textarea v-model:value="dataForm.analysis" :rows="4" placeholder="请输入试题解析(可选)" />
              </a-form-item>
 
              <div class="mt-6 flex justify-center gap-3">
                <a-button type="primary" :loading="submitting" @click="handleSubmit">提交</a-button>
                <a-button @click="handleReset">重置</a-button>
              </div>
            </a-form>
          </a-spin>
        </div>
      </div>
    </div>
  </div>
</template>
 
<style scoped>
.tms-question-form-page {
  height: 100%;
  min-height: 0;
}
 
.tms-question-form-wrap {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  overflow: hidden;
  background: #fff;
  padding: 16px;
}
 
.tms-question-form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}
 
.tms-question-form-body {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
}
 
.tms-question-editor :deep(.tox-tinymce),
.tms-question-editor :deep(.tox),
.tms-question-editor :deep(.ql-container),
.tms-question-editor :deep(.tox-edit-area),
.tms-question-editor :deep(iframe) {
  max-height: 280px;
}
 
.tms-question-editor :deep(.tox-tinymce) {
  height: 280px !important;
}
</style>