Pārlūkot izejas kodu

处理登录过期的情况

任一存 2 gadi atpakaļ
vecāks
revīzija
cbd3b2cf8b
4 mainītis faili ar 18 papildinājumiem un 4 dzēšanām
  1. 1 1
      .env.test
  2. 14 0
      src/api.js
  3. 1 1
      src/views/HomeWeb.vue
  4. 2 2
      src/views/QuestionView.vue

+ 1 - 1
.env.test

@@ -1,4 +1,4 @@
 CLI_MODE=test
 NODE_ENV=production
-PUBLIC_PATH=/hotspot/
+PUBLIC_PATH=/wenlange/hotspot/
 VUE_APP_API_PREFIX=http://192.168.20.55:8043

+ 14 - 0
src/api.js

@@ -1,6 +1,20 @@
 import axios from "axios"
 import store from "@/store/index.js"
 
+axios.interceptors.response.use(function (response) {
+  // 2xx 范围内的状态码都会触发该函数。
+  // 对响应数据做点什么
+  if (response.data.code === 5001 || response.data.code === 5002) {
+    console.log('post message.....')
+    window.parent.postMessage('token invalid')
+  }
+  return response
+}, function (error) {
+  // 超出 2xx 范围的状态码都会触发该函数。
+  // 对响应错误做点什么
+  console.log(error)
+})
+
 export default {
   async getAnswerRecord() {
     const lastToken = localStorage.getItem('token')

+ 1 - 1
src/views/HomeWeb.vue

@@ -53,7 +53,7 @@ export default {
     const answerRecord = await globalApi.getAnswerRecord()
 
     let recordOnThisHotspot = []
-    if (answerRecord.length) {
+    if (answerRecord?.length) {
       recordOnThisHotspot = answerRecord.filter((item) => {
         return item.num.split('.')[0] === this.hotspotData.content
       })

+ 2 - 2
src/views/QuestionView.vue

@@ -70,7 +70,7 @@
       >
     </div>
     <button
-      v-show="!isSubmitted && (questionInfo?.questionType === '多选题' || questionInfo.questionType === '连线题') && $refs['question-inner'].canSubmit"
+      v-show="!isSubmitted && (questionInfo?.questionType === '多选题' || questionInfo.questionType === '连线题') && $refs['question-inner']?.canSubmit"
       class="submit"
       @click="onSubmit"
     >
@@ -177,7 +177,7 @@ export default {
       window.parent.document.getElementById('closepop').click()
     },
     onSubmit() {
-      if (this.$refs['question-inner'].canSubmit) {
+      if (this.$refs['question-inner']?.canSubmit) {
         this.setIsSubmitted(true)
         globalApi.submitAnswer(
           this.$refs['question-inner']?.isCorrect,