浏览代码

add httpWithoutAuth

Michael Wang 6 年之前
父节点
当前提交
2ae4b92241
共有 4 个文件被更改,包括 37 次插入15 次删除
  1. 1 1
      src/modules/questions/directives/directives.js
  2. 6 4
      src/modules/questions/views/PreviewPaper.vue
  3. 26 10
      src/plugins/axios.js
  4. 4 0
      src/router.js

+ 1 - 1
src/modules/questions/directives/directives.js

@@ -12,7 +12,7 @@ function addAudio(el, binding, vnode) {
     var questionAudioId = obj.getAttribute("id");
     if (questionAudioId) {
       console.log("come in");
-      vnode.context.$http
+      vnode.context.$httpWithoutAuth
         .get(QUESTION_API + "/questionAudio/" + questionAudioId)
         .then(response => {
           if (response && response.data) {

+ 6 - 4
src/modules/questions/views/PreviewPaper.vue

@@ -167,10 +167,12 @@ export default {
     //初始化试卷
     initPaper() {
       this.loading = true;
-      this.$http.get(QUESTION_API + "/paper/" + this.paperId).then(response => {
-        this.paper = response.data;
-        this.loading = false;
-      });
+      this.$httpWithoutAuth
+        .get(QUESTION_API + "/paper/" + this.paperId)
+        .then(response => {
+          this.paper = response.data;
+          this.loading = false;
+        });
     },
     back() {
       debugger;

+ 26 - 10
src/plugins/axios.js

@@ -15,8 +15,8 @@ let config = {
   withCredentials: true // Check cross-site Access-Control
 };
 
-const _axios = axios.create(config);
-const _axiosWithoutResponseInterceptors = axios.create(config);
+const _$httpWith500Msg = axios.create(config);
+const _$http = axios.create(config); // no auto 500 error UI
 
 /**
  * A. token lifecycle
@@ -37,7 +37,7 @@ function getRootOrgId() {
   }
 }
 
-_axios.interceptors.request.use(
+_$httpWith500Msg.interceptors.request.use(
   function(config) {
     // Do something before request is sent
     if (
@@ -84,7 +84,8 @@ _axios.interceptors.request.use(
   }
 );
 
-_axiosWithoutResponseInterceptors.interceptors.request.use(
+_$http.interceptors.request.use(
+  // no auto 500 error UI
   function(config) {
     // Do something before request is sent
     if (config.url.includes("/login") === false) {
@@ -124,7 +125,7 @@ _axiosWithoutResponseInterceptors.interceptors.request.use(
 );
 
 // Add a response interceptor
-_axios.interceptors.response.use(
+_$httpWith500Msg.interceptors.response.use(
   response => {
     return response;
   },
@@ -199,7 +200,8 @@ _axios.interceptors.response.use(
 );
 
 // Add a response interceptor
-_axiosWithoutResponseInterceptors.interceptors.response.use(
+_$http.interceptors.response.use(
+  // no auto 500 error UI
   response => {
     return response;
   },
@@ -246,19 +248,33 @@ _axiosWithoutResponseInterceptors.interceptors.response.use(
 );
 
 Plugin.install = function(Vue) {
-  Vue.$http = _axiosWithoutResponseInterceptors;
+  Vue.$http = _$http; // no auto 500 error UI
   Object.defineProperties(Vue.prototype, {
     $http: {
       get() {
-        return _axiosWithoutResponseInterceptors;
+        return _$http; // no auto 500 error UI
       }
     }
   });
-  Vue.$httpWithMsg = _axios;
+
+  Vue.$httpWithMsg = _$httpWith500Msg;
   Object.defineProperties(Vue.prototype, {
     $httpWithMsg: {
       get() {
-        return _axios;
+        return _$httpWith500Msg;
+      }
+    }
+  });
+
+  // for below request
+  // config.url.includes("/api/ecs_ques/paper/") === false &&
+  // config.url.includes("/api/ecs_ques/questionAudio") === false
+  const _a = axios.create(config);
+  Vue.$httpWithoutAuth = _a;
+  Object.defineProperties(Vue.prototype, {
+    $httpWithoutAuth: {
+      get() {
+        return _a;
       }
     }
   });

+ 4 - 0
src/router.js

@@ -26,6 +26,10 @@ let router = new Router({
 });
 
 router.beforeEach((to, from, next) => {
+  if (to.path.includes("/preview_paper/")) {
+    next();
+    return;
+  }
   if (!to.meta.privilegeCodes) {
     next();
   } else {