mixins.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import Vue from "vue";
  2. import { createLog } from "@/utils/logger";
  3. import moment from "moment";
  4. Vue.mixin({
  5. beforeCreate() {
  6. if (document.getElementById("app-placeholder"))
  7. document.getElementById("app-placeholder").remove();
  8. },
  9. mounted() {
  10. // console.log("mixin hook called");
  11. [...document.getElementsByTagName("a")].forEach(e => {
  12. e.addEventListener("click", function(event) {
  13. event.preventDefault();
  14. });
  15. });
  16. },
  17. methods: {
  18. logger(logs) {
  19. createLog(logs);
  20. },
  21. async checkExamInProgress() {
  22. try {
  23. // 断点续考
  24. // const examingRes = (await this.$http.get(
  25. // "/api/ecs_oe_student/examControl/checkExamInProgress"
  26. // )).data;
  27. let examingRes;
  28. for (let i = 0; i < 4; i++) {
  29. examingRes = await this.$http.get(
  30. "/api/ecs_oe_student/examControl/checkExamInProgress"
  31. );
  32. if (
  33. examingRes.status === 503 ||
  34. examingRes.data.code === "S-101000"
  35. ) {
  36. await new Promise(resolve => setTimeout(() => resolve(), 2000));
  37. continue;
  38. } else if (examingRes.data.code === "000000") {
  39. break;
  40. }
  41. }
  42. if (examingRes.status !== 200 || examingRes.data.code !== "000000") {
  43. window._hmt.push([
  44. "_trackEvent",
  45. "断点续考处理异常",
  46. "调用断点续考接口超过次数",
  47. ]);
  48. throw new Error("调用断点续考接口超过次数");
  49. }
  50. this.$store.commit(
  51. "updateTimeDifference",
  52. moment(examingRes.headers.date).diff(moment())
  53. );
  54. examingRes = examingRes.data.data; // 保持和原接口一致
  55. if (examingRes && examingRes.isExceed) {
  56. // 超出断点续考次数的逻辑,仅此block
  57. this.$Message.info({
  58. content: `超出最大断点续考次数(${examingRes.maxInterruptNum}),正在自动交卷...`,
  59. duration: 15,
  60. closable: true,
  61. });
  62. this.$Spin.show({
  63. render: () => {
  64. return (
  65. <div style="font-size: 24px">
  66. 超出最大断点续考次数({examingRes.maxInterruptNum}
  67. ),正在自动交卷...
  68. </div>
  69. );
  70. },
  71. });
  72. // 断点续考消息一闪而过,此处可以加延迟
  73. // await new Promise(resolve => setTimeout(() => resolve(), 3000));
  74. const res = await this.$http.get(
  75. "/api/ecs_oe_student/examControl/endExam"
  76. );
  77. this.$Spin.hide();
  78. if (res.status === 200) {
  79. this.$router.replace({
  80. path: `/online-exam/exam/${examingRes.examId}/examRecordData/${examingRes.examRecordDataId}/end`,
  81. });
  82. } else {
  83. window._hmt.push(["_trackEvent", "断点续考处理异常", "交卷失败"]);
  84. this.$Message.error({
  85. content: "交卷失败",
  86. duration: 15,
  87. closable: true,
  88. });
  89. throw new Error("交卷失败");
  90. }
  91. return true;
  92. }
  93. if (examingRes) {
  94. this.$Spin.show({
  95. render: () => {
  96. return <div style="font-size: 24px">正在进入断点续考...</div>;
  97. },
  98. });
  99. this.$router.push(
  100. `/online-exam/exam/${examingRes.examId}/examRecordData/${examingRes.examRecordDataId}/order/1`
  101. );
  102. setTimeout(() => this.$Spin.hide(), 1000);
  103. return true;
  104. }
  105. } catch (error) {
  106. this.$Spin.hide();
  107. window._hmt.push(["_trackEvent", "断点续考处理异常"]);
  108. this.$Message.error({
  109. content: "断点续考处理异常",
  110. duration: 15,
  111. closable: true,
  112. });
  113. throw error;
  114. }
  115. },
  116. logout(cause = "") {
  117. console.log("退出登录:", cause);
  118. this.logger({ action: "退出登录", cause });
  119. localStorage.removeItem("phoneVerified");
  120. window._hmt.push(["_trackEvent", "退出", cause]);
  121. const redirectUrl = sessionStorage.getItem("redirectUrl");
  122. if (redirectUrl) {
  123. this.$Modal.error({
  124. title: "确认返回",
  125. content: "",
  126. onOk: () => {
  127. window.location = redirectUrl;
  128. },
  129. });
  130. } else {
  131. this.$router.push("/login/" + localStorage.getItem("domain") + cause);
  132. }
  133. },
  134. },
  135. });