zhouna 3 år sedan
förälder
incheckning
d63cd7beb6
1 ändrade filer med 34 tillägg och 11 borttagningar
  1. 34 11
      src/api/index.js

+ 34 - 11
src/api/index.js

@@ -1,8 +1,10 @@
 import api from './request';
 import { getCookie,setCookie } from '@utils/index'
-import {message} from 'antd'
+import { Modal } from 'antd';
 const axios=require('axios');
 axios.defaults.baseURL = 'http://192.168.2.237:8871';//'http://192.168.2.237:8871/';
+const CancelToken = axios.CancelToken;
+const source = CancelToken.source();
 const post=(url,data)=>{
     return axios({
         method:'post',
@@ -43,22 +45,29 @@ const get=(url,data)=>{
 
 function interceptors(){
 //拦截所有请求添加token
-    axios.interceptors.request.use(function (req) {
+    axios.interceptors.request.use(function (config) {
         const tokenStr = localStorage.getItem('token');
         const sysId = getCookie('systemId');
         const hisId = getCookie('hospitalId');
-        const isOverTime = localStorage.getItem("overTime");
+	    const isOverTime = localStorage.getItem("overTime");
+        if(isOverTime){
+		  config.cancelToken = source.token;
+		  return config;
+        }
         if (tokenStr) {
-            (req.headers.Authorization = `Bearer ${tokenStr}`);
-            sysId&&(req.headers.softwareId=sysId);
-            hisId&&(req.headers.hospitalId=hisId);
-            return req;
+            (config.headers.Authorization = `Bearer ${tokenStr}`);
+            sysId&&(config.headers.softwareId=sysId);
+            hisId&&(config.headers.hospitalId=hisId);
+            return config;
         } else {
-            return req;
+            return config;
         }
     }, function (error) {
         return Promise.reject(error);
     });
+    function toLogin(){
+	  window.location = '/';
+    }
     //拦截响应
     axios.interceptors.response.use(function (res) {
         const code = (res.data||{}).code;
@@ -68,13 +77,27 @@ function interceptors(){
             localStorage.removeItem("hospitalId");
             localStorage.removeItem("software");
 		    setCookie();
+		    const isOverTime = localStorage.getItem("overTime");
+		    if(!isOverTime){
+		        console.log(444,res)
+              const msg = res.data.message;
+			  Modal.confirm({
+                title:'提示',
+                content:msg||`登录信息失效,请重新登录`,
+				footer:`<Button key="submit" type="primary" onClick={()=>{ window.location = '/';}}>知道了</Button>`,
+				/*onOk:function(){
+				  window.location = '/';
+                }*/
+              })
+			  //message.error("登录信息失效,请重新登录");
+            }
             //标记登录失效被退出,后面的请求不发送/不显示相应消息
             localStorage.setItem("overTime",1);
             //localStorage.removeItem("hospitalId");
-            message.error("登录信息失效,请重新登录");
-            window.location = '/';
+            //window.location = '/';
         }else{
-            return res||{data:null};
+		  localStorage.removeItem("overTime");
+            return res;
         }
     }, function (error) {
         return Promise.reject(error);