ExamBatchDialog.vue 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <template>
  2. <el-dialog
  3. class="exam-batch-dialog"
  4. :visible.sync="dialogVisible"
  5. width="600px"
  6. title="请选择考试批次"
  7. :close-on-press-escape="false"
  8. :close-on-click-modal="false"
  9. append-to-body
  10. >
  11. <el-radio-group size="medium" v-model="examBatchId">
  12. <el-radio v-for="item in examBatchs" :key="item.id" :label="item.id">{{
  13. item.label
  14. }}</el-radio>
  15. </el-radio-group>
  16. <div slot="footer" class="dialog-footer">
  17. <el-button @click="cancel" plain>取消</el-button>
  18. <el-button type="primary" @click="confirm">确认</el-button>
  19. </div>
  20. </el-dialog>
  21. </template>
  22. <script>
  23. import { examMonitorBatchList } from "@/api/invigilation";
  24. import { dateFormatForAPI } from "@/utils/utils";
  25. export default {
  26. name: "exam-batch-dialog",
  27. props: {
  28. initExamid: String,
  29. },
  30. data() {
  31. return {
  32. dialogVisible: false,
  33. examBatchId: null,
  34. examBatchs: [],
  35. userId: this.$store.state.user.id,
  36. };
  37. },
  38. mounted() {
  39. this.getExamList();
  40. },
  41. methods: {
  42. async getExamList() {
  43. const res = await examMonitorBatchList({
  44. userId: this.userId,
  45. pageNumber: 1,
  46. pageSize: 100,
  47. });
  48. const dnow = Date.now();
  49. this.examBatchs = res.data.data.records.map((item) => {
  50. return {
  51. ...item,
  52. label: `${item.name}【${dateFormatForAPI(
  53. item.startTime
  54. )} - ${dateFormatForAPI(item.endTime)}】`,
  55. isExaming: dnow > item.startTime && dnow < item.endTime,
  56. };
  57. });
  58. let selectedExam = null;
  59. if (this.initExamid) {
  60. selectedExam = this.examBatchs.find(
  61. (item) => item.id === this.initExamid
  62. );
  63. } else {
  64. // 优先取当前第一个正在考试的考试,没有则取记录第一条数据
  65. selectedExam = this.examBatchs.find((item) => item.isExaming);
  66. selectedExam = selectedExam || this.examBatchs[0];
  67. }
  68. this.examBatchId = selectedExam && selectedExam.id;
  69. this.$emit("confirm", selectedExam);
  70. },
  71. cancel() {
  72. this.dialogVisible = false;
  73. },
  74. open() {
  75. this.dialogVisible = true;
  76. },
  77. confirm() {
  78. if (!this.examBatchId) {
  79. this.$message.error("请选择考试批次!");
  80. return;
  81. }
  82. const exam = this.examBatchs.find((item) => item.id === this.examBatchId);
  83. const dnow = Date.now();
  84. exam.isExaming = dnow > exam.startTime && dnow < exam.endTime;
  85. this.$emit("confirm", exam);
  86. this.cancel();
  87. },
  88. },
  89. };
  90. </script>