RemainTime.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <template>
  2. <div class="remain-time">剩余时间 {{remainTimeFormatted}}</div>
  3. </template>
  4. <script>
  5. import moment from "moment";
  6. import { createNamespacedHelpers } from "vuex";
  7. const { mapMutations } = createNamespacedHelpers("examingHomeModule");
  8. export default {
  9. name: "RemainTime",
  10. data() {
  11. return {
  12. remainTime: null
  13. };
  14. },
  15. async mounted() {
  16. this.heartbeatErrorNum = 0;
  17. this.first = true;
  18. this.getRemainTimeFromServer();
  19. this.intervalA = setInterval(() => {
  20. // if (this.first) {
  21. // // 跳过第一次
  22. // this.first = false;
  23. // return;
  24. // }
  25. this.getRemainTimeFromServer();
  26. }, 60 * 1000);
  27. this.intervalB = setInterval(
  28. () => (this.remainTime = this.remainTime - 1000),
  29. 1000
  30. );
  31. },
  32. beforeDestroy() {
  33. clearInterval(this.intervalA);
  34. clearInterval(this.intervalB);
  35. },
  36. methods: {
  37. ...mapMutations(["setShouldSubmitPaper"]),
  38. async getRemainTimeFromServer() {
  39. try {
  40. const res = await this.$http.get(
  41. "/api/ecs_oe_student/examControl/examHeartbeat"
  42. );
  43. this.remainTime = res.data;
  44. // if (this.first) {
  45. // this.remainTime += 60 * 1000; //补偿心跳
  46. // }
  47. this.heartbeatErrorNum = 0;
  48. } catch (e) {
  49. this.heartbeatErrorNum++;
  50. if (this.heartbeatErrorNum === 5) {
  51. // 心跳异常5次则退出考试
  52. this.$Modal.error({
  53. title: "网络连接异常",
  54. content: "退出考试",
  55. onOk: () => {
  56. this.$router.push("/login/" + localStorage.getItem("domain"));
  57. }
  58. });
  59. return;
  60. }
  61. this.getRemainTimeFromServer();
  62. }
  63. }
  64. },
  65. computed: {
  66. remainTimeFormatted: function() {
  67. return moment.utc(this.remainTime).format("HH:mm:ss");
  68. }
  69. },
  70. watch: {
  71. remainTime(val) {
  72. if (val < 0) {
  73. this.setShouldSubmitPaper();
  74. }
  75. }
  76. }
  77. };
  78. </script>