Blame view

src/components/setAnswer.vue 14.3 KB
d5987f6a   阿宝   组件修改答案
1
  <template>
45504a95   阿宝   即时测页面,以及小题修改答案
2
3
4
5
6
7
8
    <div
      class="el-dialog-wrapper"
      v-show="diaVisible"
      width="400"
      center
      @click.self="cancel"
    >
dd5150c5   阿宝   数据同步
9
10
      <div class="el-dialog-content">
        <p class="title">
9309dc5d   梁保满   任课老师接口完成
11
          设置答案 <i class="fa fa-exchange"></i>
45504a95   阿宝   即时测页面,以及小题修改答案
12
          <!-- 设置答案 -->
dd5150c5   阿宝   数据同步
13
          <i class="el-icon-close" @click="cancel"></i>
d5987f6a   阿宝   组件修改答案
14
        </p>
9309dc5d   梁保满   任课老师接口完成
15
16
17
18
19
20
21
22
23
24
25
26
27
        <div class="dia-question-box">
          <p class="dia-tips">点击批量设置答案按钮,批量设置答案</p>
          <div v-for="(subQuestions, index) in FormQuestionList" :key="index">
            <p
              class="set-ans-btn"
              v-if="
                subQuestions.qusType &&
                subQuestions.subNum &&
                subQuestions.subNum > 4
              "
            >
              <el-button type="primary" @click="setFormAns(index)"
                >批量设置答案</el-button
dd5150c5   阿宝   数据同步
28
              >
9309dc5d   梁保满   任课老师接口完成
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
            </p>
            <div class="sub-questions" :key="index" v-else>
              <div class="qs-num">题{{ subQuestions.questionIndex }}</div>
              <div class="qs-options qs-options2">
                <p v-if="subQuestions.questionType == 4" class="answer-box">
                  <span
                    class="answer-s"
                    :class="subQuestions.correctAnswer == 1 ? 'active' : ''"
                    @click="subQuestions.correctAnswer = 1"
                    >✓</span
                  >
                  <span
                    class="answer-s"
                    :class="subQuestions.correctAnswer == 2 ? 'active' : ''"
                    @click="subQuestions.correctAnswer = 2"
                    >✗</span
                  >
                </p>
                <p v-if="subQuestions.questionType == 3" class="answer-box">
                  <span
                    class="answer-s"
                    v-for="option in subQuestions.answerOptions.split(',')"
                    :class="
                      subQuestions.correctAnswer.includes(option) ? 'active' : ''
                    "
                    :key="option"
                    @click="changAnswer(subQuestions, option)"
                    >{{ option }}</span
                  >
                </p>
                <p v-if="subQuestions.questionType == 2" class="answer-box">
                  <span
                    class="answer-s"
                    v-for="option in subQuestions.answerOptions.split(',')"
                    :class="subQuestions.correctAnswer == option ? 'active' : ''"
                    :key="option"
                    @click="subQuestions.correctAnswer = option"
                    >{{ option }}</span
                  >
                </p>
45504a95   阿宝   即时测页面,以及小题修改答案
69
              </div>
d5987f6a   阿宝   组件修改答案
70
71
            </div>
          </div>
45504a95   阿宝   即时测页面,以及小题修改答案
72
        </div>
dd5150c5   阿宝   数据同步
73
74
75
76
77
78
79
80
        <div class="footer-box">
          <el-button class="dia-btn" type="primary" @click="saveAnswer"
            >确 定</el-button
          >
          <el-button class="dia-btn" type="danger" @click="cancel"
            >取 消</el-button
          >
        </div>
d5987f6a   阿宝   组件修改答案
81
      </div>
9309dc5d   梁保满   任课老师接口完成
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
      <el-dialog
        title="批量设置答案"
        :visible.sync="diaSetAns"
        width="400"
        :modal-append-to-body="false"
      >
        <div class="qs-options">
          <p>{{ setSubPro(formAns.qusType) }}:</p>
          <p class="ipt">
            <el-input
              v-model="formAns.answerList"
              @keydown.native="keydownAnswer($event)"
            ></el-input>
          </p>
          <p class="answer-box">
            <template v-if="formAns.qusType == 4">
              <span
                class="answer-s active"
                @click="
                  formAns.answerList.length < formAns.subNum
                    ? (formAns.answerList += '✓')
                    : ''
                "
                >✓</span
              >
              <span
                class="answer-s active"
                @click="
                  formAns.answerList.length < formAns.subNum
                    ? (formAns.answerList += '✗')
                    : ''
                "
                >✗</span
              >
            </template>
            <template v-if="formAns.qusType == 3">
              <span
                class="answer-s active"
                v-for="option in formAns.answerOptions.split(',')"
                :key="option"
                @click="setMultiple(formAns, option)"
                >{{ option }}</span
              >
              <span
                class="answer-s active"
                @click="
                  formAns.answerList.split(',').length < formAns.subNum
                    ? (formAns.answerList += ',')
                    : ''
                "
                >,</span
              >
            </template>
            <template v-if="formAns.qusType == 2" class="answer-box">
              <span
                class="answer-s active"
                v-for="option in formAns.answerOptions.split(',')"
                :key="option"
                @click="
                  formAns.answerList.length < formAns.subNum
                    ? (formAns.answerList += option)
                    : ''
                "
                >{{ option }}</span
              >
            </template>
            <span
              class="answer-s delButton"
              @click="formAns.answerList = formAns.answerList.slice(0, -1)"
              >x</span
            >
          </p>
        </div>
        <div class="dialog-footer" slot="footer">
          <el-button @click="saveFormAns">确 定</el-button>
          <el-button @click="diaSetAns = false">取 消</el-button>
        </div>
      </el-dialog>
dd5150c5   阿宝   数据同步
160
    </div>
d5987f6a   阿宝   组件修改答案
161
162
163
  </template>
  
  <script>
45504a95   阿宝   即时测页面,以及小题修改答案
164
  import { deepClone, checkAnswer } from "utils";
d5987f6a   阿宝   组件修改答案
165
166
167
168
169
170
171
172
173
  export default {
    name: "SetAnswer",
    props: {
      diaVisible: false, //修改答案
      paperId: "",
      questionList: Array,
    },
    data() {
      return {
9309dc5d   梁保满   任课老师接口完成
174
        diaSetAns: false,
d5987f6a   阿宝   组件修改答案
175
176
        editType: true, //修改答案模式
        FormQuestionList: [],
9309dc5d   梁保满   任课老师接口完成
177
178
179
180
181
182
183
184
        FormQuestionList: [],
        formAns: {
          endIndex: 0, //相同题目最后一位题目的questionIndex
          qusType: "", //题目类型
          subNum: 0, //数量
          answerOptions: [], //答案选项
          answerList: "", //答案列表-字符串
        },
d5987f6a   阿宝   组件修改答案
185
186
187
188
      };
    },
    watch: {
      questionList: {
9309dc5d   梁保满   任课老师接口完成
189
190
191
192
193
194
195
196
        handler: function (val) {
          const questions = deepClone(val).sort((a, b) => {
            return a.questionIndex - b.questionIndex;
          });
          this.FormQuestionList = deepClone(questions);
          let types = [{}];
          let addndex = 0;
          this.FormQuestionList?.map((sub, index) => {
533a17d8   梁保满   备题组卷添加批量设置答案
197
            if (!!sub.questionType && sub.questionType!=5) {
9309dc5d   梁保满   任课老师接口完成
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
              if (sub.questionType == types[addndex].qusType) {
                //同类型批量答案+1
                types[addndex].subNum += 1;
                if (
                  types[addndex].answerOptions.length < sub.answerOptions.length
                ) {
                  types[addndex].answerOptions = sub.answerOptions;
                }
                types[addndex].answerList += this.setAnswer(
                  sub.questionType,
                  sub.correctAnswer
                );
                if (index == this.questionList.length - 1) {
                  //循环最后类型数量大于等于5,保存批量答案
                  if (types[addndex].subNum && types[addndex].subNum >= 5) {
                    types[addndex].endIndex = sub.questionIndex;
                    types[addndex].index = index;
                  }
                }
45504a95   阿宝   即时测页面,以及小题修改答案
217
              } else {
9309dc5d   梁保满   任课老师接口完成
218
219
220
221
                if (types[addndex].subNum && types[addndex].subNum >= 5) {
                  //不同类型时如果原有类型数量大于等于5,保存批量答案
                  types[addndex].endIndex =
                    this.questionList[index - 1].questionIndex;
533a17d8   梁保满   备题组卷添加批量设置答案
222
                  types[addndex].index = index-1;
9309dc5d   梁保满   任课老师接口完成
223
224
                  addndex += 1;
                  types[addndex] = {};
45504a95   阿宝   即时测页面,以及小题修改答案
225
                }
9309dc5d   梁保满   任课老师接口完成
226
227
228
229
230
231
232
233
                //不同类型初始化批量答案
                types[addndex].qusType = sub.questionType;
                types[addndex].subNum = 1;
                types[addndex].answerOptions = sub.answerOptions;
                types[addndex].answerList = this.setAnswer(
                  sub.questionType,
                  sub.correctAnswer
                );
45504a95   阿宝   即时测页面,以及小题修改答案
234
              }
9309dc5d   梁保满   任课老师接口完成
235
            }
45504a95   阿宝   即时测页面,以及小题修改答案
236
          });
9309dc5d   梁保满   任课老师接口完成
237
238
239
240
241
242
243
244
245
246
247
248
          for (let i = 0; i < types.length; i++) {
            if (types[i].qusType == 3) {
              types[i].answerList = types[i].answerList.slice(0, -1);
            }
            if (types[i].subNum >= 5) {
              item.subQuestions.splice(
                types[i].index + i + 1,
                0,
                deepClone(types[i])
              );
            }
          }
d5987f6a   阿宝   组件修改答案
249
250
251
252
253
        },
        deep: true,
      },
    },
    methods: {
9309dc5d   梁保满   任课老师接口完成
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
      setFormAns(indexs) {
        //初始化要修改的答案
        this.formAns = { ...this.FormQuestionList[indexs] };
        this.diaSetAns = true;
      },
      saveFormAns() {
        //批量修改答案
        let EndIndex;
        let subNum = this.formAns.subNum - 1;
        this.FormQuestionList.some((item, index) => {
          if (this.formAns.endIndex == item.questionIndex) {
            EndIndex = index;
            return;
          }
        });
        for (let i = 0; i <= subNum; i++) {
          let correctAnswer = "";
          if (this.formAns.qusType == 2) {
            correctAnswer = this.formAns.answerList[subNum - i];
          } else if (this.formAns.qusType == 3) {
            correctAnswer = this.formAns.answerList.split(",")[subNum - i];
  
            console.log(this.formAns.answerList.split(",")[subNum - i]);
          } else if (this.formAns.qusType == 4) {
            correctAnswer = this.formAns.answerList[subNum - i] == "✓" ? 1 : 2;
          }
          this.questionList[EndIndex - i].correctAnswer = correctAnswer;
        }
        this.diaSetAns = false;
      },
d5987f6a   阿宝   组件修改答案
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
      setSubPro(type) {
        let tit;
        switch (type) {
          case 2:
            tit = "单选题";
            break;
          case 3:
            tit = "多选题";
            break;
          case 4:
            tit = "判断题";
            break;
          case 5:
            tit = "主观题";
            break;
        }
        return tit;
      },
      keydownAnswer(event) {
        //快速答案设置禁止输入
        if (
          event.key == "Meta" ||
          event.key == "CapsLock" ||
          event.key == "Shift" ||
          event.key == "Enter" ||
          event.key == "Alt" ||
          event.key == "Backspace" ||
          event.key == "Delete" ||
          event.key == "ArrowUp" ||
          event.key == "ArrowDown" ||
          event.key == "ArrowLeft" ||
          event.key == "v" ||
          event.key == "V" ||
          event.key == "ArrowRight"
        ) {
          return;
        } else {
          event.returnValue = "";
        }
      },
9309dc5d   梁保满   任课老师接口完成
324
325
326
327
328
329
330
331
332
333
334
335
      setAnswer(type, ans) {
        let txt = "";
        if (type == 2) {
          txt = ans;
        } else if (type == 3) {
          txt = ans + ",";
        } else if (type == 4) {
          txt = ans == 1 ? "✓" : "✗";
        }
        return txt;
      },
      setMultiple(obj, answer) {
d5987f6a   阿宝   组件修改答案
336
337
338
339
340
341
        //多选答案设置
        obj.answerList += answer;
        let str = obj.answerList;
        let str2 = checkAnswer(
          str,
          3,
9309dc5d   梁保满   任课老师接口完成
342
343
          obj.answerOptions.split(",").length,
          obj.answerList.length
d5987f6a   阿宝   组件修改答案
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
        );
        obj.answerList = str2;
      },
      changAnswer(sub, option) {
        //设置多选答案
        let str = new RegExp(option, "g");
        if (sub.correctAnswer?.includes(option)) {
          sub.correctAnswer = sub.correctAnswer.replace(str, "");
        } else {
          let arrs = (sub.correctAnswer && sub.correctAnswer.split("")) || [];
          arrs.push(option);
          sub.correctAnswer = arrs.sort().join("");
        }
      },
      async saveAnswer() {
8ea67428   梁保满   飞书bug
359
360
361
362
        let questionList = [...this.FormQuestionList];
        for (let i = 0; i < questionList.length; i++) {
          if (questionList[i].qusType) {
            questionList.splice(i, 1);
9309dc5d   梁保满   任课老师接口完成
363
364
            i--;
          }
45504a95   阿宝   即时测页面,以及小题修改答案
365
366
        }
        console.log(questionList);
9309dc5d   梁保满   任课老师接口完成
367
368
        const { data, status, message } = await this.$request.setPeriodAnswer({
          periodId: this.paperId,
d5987f6a   阿宝   组件修改答案
369
370
371
          questionList: questionList,
        });
        if (status == 0) {
dd5150c5   阿宝   数据同步
372
          this.$emit("saveSuccess");
d5987f6a   阿宝   组件修改答案
373
374
375
376
377
        } else {
          this.$message.error(message);
        }
      },
      cancel() {
dd5150c5   阿宝   数据同步
378
        this.$emit("cancel");
d5987f6a   阿宝   组件修改答案
379
380
381
382
383
384
      },
    },
  };
  </script>
  
  <style lang="scss" scoped>
dd5150c5   阿宝   数据同步
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
  .el-dialog-wrapper {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: auto;
    margin: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2022;
    .el-dialog-content {
      width: 600px;
45504a95   阿宝   即时测页面,以及小题修改答案
400
      background: #fff;
dd5150c5   阿宝   数据同步
401
      border-radius: 10px;
45504a95   阿宝   即时测页面,以及小题修改答案
402
      padding: 16px 20px 20px;
dd5150c5   阿宝   数据同步
403
    }
45504a95   阿宝   即时测页面,以及小题修改答案
404
    .title {
dd5150c5   阿宝   数据同步
405
406
      font-size: 18px;
      position: relative;
45504a95   阿宝   即时测页面,以及小题修改答案
407
408
      margin-bottom: 20px;
      .el-icon-close {
dd5150c5   阿宝   数据同步
409
        position: absolute;
45504a95   阿宝   即时测页面,以及小题修改答案
410
411
412
        top: 0;
        right: 12px;
        padding: 2px;
dd5150c5   阿宝   数据同步
413
414
        font-size: 16px;
        cursor: pointer;
45504a95   阿宝   即时测页面,以及小题修改答案
415
416
417
418
419
420
421
422
        &:hover {
          color: #667ffa;
        }
      }
      .fa-exchange {
        color: #999;
        cursor: pointer;
        &:hover {
dd5150c5   阿宝   数据同步
423
424
425
426
427
428
429
          color: #667ffa;
        }
      }
    }
  }
  .footer-box {
    text-align: center;
45504a95   阿宝   即时测页面,以及小题修改答案
430
    padding-top: 20px;
dd5150c5   阿宝   数据同步
431
  }
d5987f6a   阿宝   组件修改答案
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
  .sub-questions {
    width: 100%;
    display: flex;
    &:hover {
      background: #f8f8f8;
    }
    & > div {
      min-height: 40px;
      padding: 5px;
      flex-shrink: 0;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .qs-num {
      width: 80px;
      font-size: 16px;
      color: #333;
      font-weight: 500;
    }
    .qs-options {
      flex: 1;
    }
    .qs-options2 {
      text-align: left;
      justify-content: flex-start;
      padding-left: 20px;
      .answer-s {
        cursor: pointer;
        border-color: #667ffd;
        color: #667ffd;
        margin: 0 15px;
        &.active {
          color: #fff;
        }
      }
    }
    :deep(.el-select) {
      .el-input__inner {
        border-radius: 20px;
        border-color: #667ffd;
        width: 150px;
        height: 32px;
        line-height: 32px;
        background: rgba($color: #667ffd, $alpha: 0.05);
      }
      .el-input__icon {
        line-height: 32px;
      }
    }
  }
45504a95   阿宝   即时测页面,以及小题修改答案
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
  .dia-tips {
    line-height: 18px;
    padding-bottom: 10px;
  }
  .set-questions {
    display: flex;
    padding-right: 20px;
    margin-bottom: 20px;
    .qs-num {
      width: 80px;
      padding-top: 6px;
      .txt {
        font-size: 16px;
        line-height: 18px;
        margin-bottom: 6px;
      }
    }
    .qs-options {
      flex: 1;
      .ipt {
        margin-bottom: 5px;
      }
      .answer-box {
        .answer-s {
          cursor: pointer;
          user-select: none;
          &:first-of-type {
            margin-left: 0;
          }
        }
      }
    }
    .delButton {
      border-color: #ff6868;
      background: #ff6868 url("../assets/images/arrow.png") no-repeat center;
      background-size: 19px;
      color: transparent;
    }
    .ac {
      border-color: #ff6868;
      background: #ff6868;
      color: #fff;
    }
  }
d5987f6a   阿宝   组件修改答案
527
  </style>