Ver Fonte

提交两种网络请求

Michael Wang há 6 anos atrás
pai
commit
5af9b16667

+ 8 - 30
src/modules/portal/views/Login.vue

@@ -58,7 +58,6 @@ import { core_api } from "../constants/constants";
 export default {
   data() {
     return {
-      pending: false,
       errorInfo: "",
       title: "考试云平台",
       jwptCustomize: false,
@@ -113,37 +112,16 @@ export default {
       if (!this.checkPassword()) {
         return;
       }
-      if (this.pending) {
-        return;
-      }
-      this.pending = true;
       var url = core_api + "/auth/login";
-      this.$http
-        .post(url, this.loginInfo)
-        .then(response => {
-          console.log(response);
-          var user = response.data;
-          this.USER_SIGNIN(user);
-          user.loginUrl = "http://" + window.location.host + "/#/login";
-          user.indexUrl = "http://" + window.location.host + "/#/index";
-          window.name = JSON.stringify(user);
-          this.$router.replace({ path: "/home/overview" });
-          this.$notify({
-            message: "登录成功",
-            type: "success"
-          });
-          this.pending = false;
-        })
-        .catch(response => {
-          if (response.status == 500) {
-            this.$notify({
-              showClose: true,
-              message: response.body.desc,
-              type: "error"
-            });
-          }
-          this.pending = false;
+      this.$httpWithMsg.post(url, this.loginInfo).then(response => {
+        var user = response.data;
+        this.USER_SIGNIN(user);
+        this.$router.replace({ path: "/home/overview" });
+        this.$notify({
+          message: "登录成功",
+          type: "success"
         });
+      });
     },
     jwptCustomizeMethod() {
       // 普通高校考试综合管理平台 定制

+ 1 - 1
src/modules/portal/views/home/Home.vue

@@ -153,7 +153,7 @@ export default {
             userId +
             "&password=" +
             password;
-          this.$http.put(url).then(() => {
+          this.$httpWithMsg.put(url).then(() => {
             this.$notify({
               type: "success",
               message: "修改密码成功!"

+ 1 - 1
src/modules/portal/views/home/HomeMain.vue

@@ -39,7 +39,7 @@ export default {
       const params = new URLSearchParams();
       params.append("groupCode", groupCode);
       params.append("full", false);
-      const res = await this.$http.post(url, params, {
+      const res = await this.$httpWithMsg.post(url, params, {
         headers: { "content-type": "application/x-www-form-urlencoded" }
       });
 

+ 1 - 1
src/modules/portal/views/home/HomeSide.vue

@@ -97,7 +97,7 @@ export default {
       const params = new URLSearchParams();
       params.append("groupCode", groupCode);
       params.append("full", false);
-      const res = await this.$http.post(url, params, {
+      const res = await this.$httpWithMsg.post(url, params, {
         headers: { "content-type": "application/x-www-form-urlencoded" }
       });
 

+ 40 - 1
src/plugins/axios.js

@@ -14,6 +14,7 @@ let config = {
 };
 
 const _axios = axios.create(config);
+const _axiosWithoutResponseInterceptors = axios.create(config);
 
 /**
  * A. token lifecycle
@@ -56,6 +57,36 @@ _axios.interceptors.request.use(
   }
 );
 
+_axiosWithoutResponseInterceptors.interceptors.request.use(
+  function(config) {
+    // Do something before request is sent
+    if (config.url.includes("/login") === false) {
+      if (!wk_token) {
+        const user = JSON.parse(window.sessionStorage.getItem("user"));
+        wk_token = user.token;
+        wk_key = user.key;
+        wk_orgId = user.rootOrgId;
+      }
+      if (wk_token && config.headers.common["token"] == null) {
+        config.headers.common["token"] = wk_token;
+        config.headers.common["key"] = wk_key;
+      }
+    } else {
+      wk_token = null;
+    }
+    return config;
+  },
+  function(error) {
+    // Do something with request error
+    Vue.prototype.$notify({
+      showClose: true,
+      message: error,
+      type: "error"
+    });
+    return Promise.reject(error);
+  }
+);
+
 // Add a response interceptor
 _axios.interceptors.response.use(
   response => {
@@ -116,9 +147,17 @@ _axios.interceptors.response.use(
 );
 
 Plugin.install = function(Vue) {
-  Vue.$http = _axios;
+  Vue.$http = _axiosWithoutResponseInterceptors;
   Object.defineProperties(Vue.prototype, {
     $http: {
+      get() {
+        return _axiosWithoutResponseInterceptors;
+      }
+    }
+  });
+  Vue.$httpWithMsg = _axios;
+  Object.defineProperties(Vue.prototype, {
+    $httpWithMsg: {
       get() {
         return _axios;
       }