李志伟 3 lat temu
rodzic
commit
61c441d49f
3 zmienionych plików z 21 dodań i 28 usunięć
  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 Wyświetl plik

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

+ 7
- 14
src/components/RightContent/AvatarDropdown.jsx Wyświetl plik

6
 import HeaderDropdown from '../HeaderDropdown';
6
 import HeaderDropdown from '../HeaderDropdown';
7
 import ChangePassword from './ChangePassword';
7
 import ChangePassword from './ChangePassword';
8
 import styles from './index.less';
8
 import styles from './index.less';
9
+import myPath from '@/utils/myPath';
9
 
10
 
10
 /**
11
 /**
11
  * 退出登录,并且将当前的 url 保存
12
  * 退出登录,并且将当前的 url 保存
12
  */
13
  */
13
 const loginOut = async () => {
14
 const loginOut = async () => {
14
   localStorage.removeItem('token');
15
   localStorage.removeItem('token');
15
-  let role=localStorage.getItem('roleAlias');
16
   const { query = {}, pathname } = history.location;
16
   const { query = {}, pathname } = history.location;
17
   const { redirect } = query;
17
   const { redirect } = query;
18
   if (window.location.pathname !== '/user/login' && !redirect) {
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
 const AvatarDropdown = ({ menu }) => {
27
 const AvatarDropdown = ({ menu }) => {
35
   const [showPasswordModal, setShowPasswordModal] = useState(false);
28
   const [showPasswordModal, setShowPasswordModal] = useState(false);
36
   const { initialState, setInitialState } = useModel('@@initialState');
29
   const { initialState, setInitialState } = useModel('@@initialState');

+ 9
- 0
src/utils/myPath.js Wyświetl plik

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
+}