ActivityEdit.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <template>
  2. <div>
  3. <h2>{{ "新增" + "场次" }}</h2>
  4. <el-row>
  5. <el-col :span="3"> 考试名称: </el-col>
  6. <el-col :span="21">
  7. {{ exam.name }}
  8. </el-col>
  9. </el-row>
  10. <el-row>
  11. <el-col :span="3"> 考试时间段: </el-col>
  12. <el-col :span="21">
  13. {{ exam.startTime | datetimeFilter }} ~
  14. {{ exam.endTime | datetimeFilter }}
  15. </el-col>
  16. </el-row>
  17. <el-row>
  18. <el-form
  19. :model="item"
  20. :ref="'form' + index"
  21. :rules="rules"
  22. label-position="right"
  23. inline
  24. v-for="(item, index) in form"
  25. :key="index"
  26. >
  27. <div class="d-flex">
  28. <el-form-item label="考试时间" prop="startTime">
  29. <el-date-picker
  30. v-model="item.startTime"
  31. type="datetime"
  32. placeholder="选择日期时间"
  33. style="width: 188px;"
  34. >
  35. </el-date-picker>
  36. </el-form-item>
  37. <el-form-item label="交卷时间" prop="finishTime">
  38. <el-date-picker
  39. v-model="item.finishTime"
  40. type="datetime"
  41. placeholder="选择日期时间"
  42. style="width: 188px;"
  43. >
  44. </el-date-picker>
  45. </el-form-item>
  46. <el-form-item label="考试时长" prop="prepareSeconds">
  47. <MinuteInput
  48. v-model="item.maxDurationSeconds"
  49. style="width: 125px;"
  50. />
  51. </el-form-item>
  52. <el-form-item label="候考时间" prop="prepareSeconds">
  53. <MinuteInput v-model="item.prepareSeconds" style="width: 125px;" />
  54. </el-form-item>
  55. <el-form-item label="迟到时长" prop="prepareSeconds">
  56. <MinuteInput v-model="item.openingSeconds" style="width: 125px;" />
  57. </el-form-item>
  58. </div>
  59. </el-form>
  60. </el-row>
  61. <el-row class="d-flex justify-content-center">
  62. <el-button type="primary" @click="submitForm">保 存</el-button>
  63. <el-button type="primary" @click="addActivity">新 增</el-button>
  64. <el-button @click="() => this.$router.back()">取 消</el-button>
  65. </el-row>
  66. </div>
  67. </template>
  68. <script>
  69. import { saveActivities } from "@/api/examwork-activity";
  70. import { getExamDetail } from "@/api/examwork-exam";
  71. import moment from "moment";
  72. export default {
  73. name: "ActivityEdit",
  74. computed: {
  75. examId() {
  76. return this.$route.params.examId;
  77. },
  78. activityId() {
  79. return this.$route.params.activityId;
  80. },
  81. isEdit() {
  82. return !!this.$route.params.activityId;
  83. },
  84. },
  85. data() {
  86. const that = this;
  87. return {
  88. form: [
  89. {
  90. id: "",
  91. startTime: null,
  92. finishTime: null,
  93. prepareSeconds: 0,
  94. openingSeconds: 0,
  95. maxDurationSeconds: 0,
  96. },
  97. ],
  98. rules: {
  99. startTime: [
  100. { required: true, message: "开始时间必填" },
  101. {
  102. validator(rule, value) {
  103. return new Promise((resolve, reject) => {
  104. if (
  105. moment(value).isBetween(
  106. moment(that.exam.startTime),
  107. moment(that.exam.endTime)
  108. )
  109. ) {
  110. resolve(); // reject with error message
  111. } else {
  112. reject("reject");
  113. }
  114. });
  115. },
  116. // type: "date",
  117. // asyncValidator: (rule, value) => {
  118. // console.log(value);
  119. // return new Promise((resolve, reject) => {
  120. // if (
  121. // moment(value).isBetween(
  122. // moment(that.exam.startTime),
  123. // moment(that.exam.endTime)
  124. // )
  125. // ) {
  126. // resolve(); // reject with error message
  127. // } else {
  128. // reject();
  129. // }
  130. // });
  131. // },
  132. message: "场次的开始时间不在考试的时间范围",
  133. },
  134. ],
  135. finishTime: [
  136. { required: true, message: "交卷时间必填" },
  137. {
  138. validator(rule, value) {
  139. return new Promise((resolve, reject) => {
  140. if (
  141. moment(value).isBetween(
  142. moment(that.exam.startTime),
  143. moment(that.exam.endTime)
  144. )
  145. ) {
  146. resolve(); // reject with error message
  147. } else {
  148. reject("reject");
  149. }
  150. });
  151. },
  152. message: "场次的交卷时间不在考试的时间范围",
  153. },
  154. ],
  155. maxDurationSeconds: [{ required: true, message: "考试时长必填" }],
  156. prepareSeconds: [{ required: true, message: "候考时间必填" }],
  157. openingSeconds: [{ required: true, message: "迟到时长必填" }],
  158. },
  159. exam: {},
  160. activity: {},
  161. };
  162. },
  163. async created() {
  164. try {
  165. this.exam = (await getExamDetail({ id: this.examId }))?.data.data;
  166. this.form[0].prepareSeconds = this.exam.prepareSeconds;
  167. this.form[0].openingSeconds = this.exam.openingSeconds;
  168. this.form[0].maxDurationSeconds = this.exam.maxDurationSeconds;
  169. } catch (error) {
  170. console.log(error);
  171. this.$notify({ type: "error", title: "获取考试详情失败" });
  172. }
  173. // if (this.isEdit) {
  174. // try {
  175. // this.activity = (
  176. // await getActivityDetail({ id: this.activityId })
  177. // )?.data.data.records[0];
  178. // this.form = this.activity;
  179. // } catch (error) {
  180. // console.log(error);
  181. // this.$notify({ type: "error", title: "获取场次详情失败" });
  182. // }
  183. // } else {
  184. // this.form = {
  185. // id: "",
  186. // startTime: null,
  187. // finishTime: null,
  188. // prepareSeconds: 0,
  189. // openingSeconds: 0,
  190. // maxDurationSeconds: 0,
  191. // enable: 0,
  192. // };
  193. // }
  194. },
  195. methods: {
  196. addActivity() {
  197. this.form.push({
  198. id: "",
  199. startTime: null,
  200. finishTime: null,
  201. prepareSeconds: this.exam.prepareSeconds,
  202. openingSeconds: this.exam.openingSeconds,
  203. maxDurationSeconds: this.exam.maxDurationSeconds,
  204. });
  205. },
  206. async submitForm() {
  207. let data = [];
  208. for (let i = 0; i < this.form.length; i++) {
  209. try {
  210. const valid = await this.$refs["form" + i][0].validate();
  211. data.push({
  212. examId: this.examId,
  213. startTime: new Date(this.form[i].startTime).valueOf(),
  214. finishTime: new Date(this.form[i].finishTime).valueOf(),
  215. prepareSeconds: this.form[i].prepareSeconds,
  216. openingSeconds: this.form[i].openingSeconds,
  217. maxDurationSeconds: this.form[i].maxDurationSeconds,
  218. enable: 1,
  219. });
  220. console.log(data);
  221. if (!valid) return;
  222. } catch (error) {
  223. console.log(error);
  224. return;
  225. }
  226. }
  227. await saveActivities(data);
  228. this.$emit("reload");
  229. },
  230. },
  231. };
  232. </script>
  233. <style></style>