mixins.js 4.6 KB

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