ソースを参照

401拦截器,步骤-3

zhouna 3 年 前
コミット
1a5756fdfe
4 ファイル変更15 行追加10 行削除
  1. 0 2
      src/App.js
  2. 10 0
      src/api/index.js
  3. 2 7
      src/components/SysChoose/index.js
  4. 3 1
      src/index.js

+ 0 - 2
src/App.js

@@ -1,8 +1,6 @@
 import React from 'react';
 import './App.less';
-import PageLayout from '@components/PageLayout';
 import AuthIndex from '@components/AuthIndex';
-/*import { useSelector } from 'react-redux'*/
 
 function App(){
    //const tokenInfo =localStorage.getItem('token');

+ 10 - 0
src/api/index.js

@@ -55,6 +55,16 @@ function interceptors(param){
     }, function (error) {
         return Promise.reject(error);
     });
+    //拦截响应
+    axios.interceptors.response.use(function (res) {
+        const code = res.data.code;
+        if (code===401) {   //401返回登录
+            localStorage.removeItem("token");
+            window.location = '/';
+        }
+    }, function (error) {
+        return Promise.reject(error);
+    });
 }
 
 const obj = {

+ 2 - 7
src/components/SysChoose/index.js

@@ -14,7 +14,7 @@ const {api,xPost,interceptors} = apiObj;
 
 const { Step } = Steps;
 function SysChoose({history}) {
-    const [current, setCurrent] = useState(0);
+    //const [current, setCurrent] = useState(1);
     const [sysId, setSysId] = useState(1);
     const [orgList, setOrgList] = useState([]);
     const [hisList, setHisList] = useState([]);
@@ -28,14 +28,9 @@ function SysChoose({history}) {
             title: '选择组织',
             content: 'Second-content',
         },
-        {
-            title: '进入组织',
-            content: 'Last-content',
-        },
     ];
     //系统单选
     function onChange(e){
-        setCurrent(1);
         const id = e.target.value;
         const checkOrg = orgList.find((it)=>it.id===id);
         const his =checkOrg.hospitals;
@@ -79,7 +74,7 @@ function SysChoose({history}) {
                 </div>
             </div>
             <div className="content">
-                <Steps current={current} className='steps-bar'>
+                <Steps current={1} className='steps-bar'>
                     {steps.map(item => (
                         <Step key={item.title} title={item.title} />
                     ))}

+ 3 - 1
src/index.js

@@ -4,9 +4,11 @@ import './index.less';
 import App from './App';
 import { Provider } from 'react-redux'
 import configureStore from './store/reducerExtras/configureStore'
+import utils from '@api/index';
 
+const { interceptors } = utils;
 const store = configureStore();
-
+interceptors({});       //拦截所有请求,401跳登录
 const renderApp = () =>
     render(
         <Provider store={store}>