李志伟 hace 3 años
padre
commit
61c441d49f
Se han modificado 3 ficheros con 21 adiciones y 28 borrados
  1. 5
    14
      src/app.jsx
  2. 7
    14
      src/components/RightContent/AvatarDropdown.jsx
  3. 9
    0
      src/utils/myPath.js

+ 5
- 14
src/app.jsx Ver fichero

@@ -4,6 +4,7 @@ import RightContent from '@/components/RightContent';
4 4
 import Footer from '@/components/Footer';
5 5
 import { currentUser as queryCurrentUser } from './services/user';
6 6
 import { requestConfig } from '@/utils/request';
7
+import myPath from './utils/myPath';
7 8
 
8 9
 const isDev = process.env.NODE_ENV === 'development';
9 10
 let loginPath = '/user/orgLogin';
@@ -16,16 +17,11 @@ export const initialStateConfig = {
16 17
  * @see  https://umijs.org/zh-CN/plugins/plugin-initial-state
17 18
  * */
18 19
 export async function getInitialState() {
19
-  let role=localStorage.getItem('roleAlias');
20 20
   const fetchUserInfo = async () => {
21 21
     try {
22 22
       return await queryCurrentUser();
23
-    } catch (error) {      
24
-      if(role=='admin'){
25
-        history.push('/user/login');
26
-      }else {
27
-        history.push(loginPath);
28
-      }      
23
+    } catch (error) {
24
+      history.push(myPath());
29 25
     }
30 26
 
31 27
     return undefined;
@@ -58,20 +54,15 @@ export const layout = ({ initialState }) => {
58 54
     footerRender: () => <Footer />,
59 55
     onPageChange: () => {
60 56
       const { location } = history; // 如果没有登录,重定向到 login
61
-      let role=localStorage.getItem('roleAlias');
62 57
       if (!initialState?.currentUser &&
63 58
         location.pathname !== loginPath &&
64 59
         location.pathname !== '/user/login') {
65
-          if(role=='admin'){
66
-            history.push('/user/login');
67
-          }else {
68
-            history.push(loginPath);
69
-          }
60
+        history.push(myPath());
70 61
       }
71 62
     },
72 63
     menuHeaderRender: undefined,
73 64
     // 自定义 403 页面
74
-    unAccessible: history.push('/'),
65
+    // unAccessible: history.push('/'),
75 66
     ...initialState?.settings,
76 67
   };
77 68
 };

+ 7
- 14
src/components/RightContent/AvatarDropdown.jsx Ver fichero

@@ -6,31 +6,24 @@ import { stringify } from 'querystring';
6 6
 import HeaderDropdown from '../HeaderDropdown';
7 7
 import ChangePassword from './ChangePassword';
8 8
 import styles from './index.less';
9
+import myPath from '@/utils/myPath';
9 10
 
10 11
 /**
11 12
  * 退出登录,并且将当前的 url 保存
12 13
  */
13 14
 const loginOut = async () => {
14 15
   localStorage.removeItem('token');
15
-  let role=localStorage.getItem('roleAlias');
16 16
   const { query = {}, pathname } = history.location;
17 17
   const { redirect } = query;
18 18
   if (window.location.pathname !== '/user/login' && !redirect) {
19
-    if (role == 'org') {
20
-      goPage('/user/orgLogin',pathname);      
21
-    } else {
22
-      goPage('/user/login',pathname);
23
-    }
19
+    history.replace({
20
+      pathname: myPath(),
21
+      search: stringify({
22
+        redirect: pathname,
23
+      }),
24
+    });
24 25
   }
25 26
 };
26
-const goPage = (val,pathname) => {
27
-  history.replace({
28
-    pathname: val,
29
-    search: stringify({
30
-      redirect: pathname,
31
-    }),
32
-  });
33
-}
34 27
 const AvatarDropdown = ({ menu }) => {
35 28
   const [showPasswordModal, setShowPasswordModal] = useState(false);
36 29
   const { initialState, setInitialState } = useModel('@@initialState');

+ 9
- 0
src/utils/myPath.js Ver fichero

@@ -0,0 +1,9 @@
1
+
2
+export default function myPath(){
3
+  let role = localStorage.getItem('roleAlias');
4
+  if (role == 'admin') {
5
+    return '/user/login'
6
+  } else {
7
+    return '/user/orgLogin'
8
+  }
9
+}