Skip to content

Commit e0626c1

Browse files
committed
chore(platform): optimize platform
1 parent 4f4f31b commit e0626c1

File tree

9 files changed

+51
-112
lines changed

9 files changed

+51
-112
lines changed

packages/platform/src/app/App.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ export function App() {
5454
}
5555
});
5656

57-
useEffect(() => {
58-
document.documentElement.lang = languageStorage.value;
59-
}, [languageStorage.value]);
60-
6157
useEffect(() => {
6258
if (loading === false) {
6359
const loader = document.querySelector('.fp-loader') as HTMLElement;
@@ -68,6 +64,10 @@ export function App() {
6864
}
6965
}, [async, loading]);
7066

67+
useEffect(() => {
68+
document.documentElement.lang = languageStorage.value;
69+
}, [languageStorage.value]);
70+
7171
useEffect(() => {
7272
for (const t of ['light', 'dark']) {
7373
document.body.classList.toggle(t, themeStorage.value === t);

packages/platform/src/app/core/http/useHttp.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export function useHttp() {
105105
for (const abort of dataRef.current.abortFns) {
106106
abort();
107107
}
108+
dataRef.current.abortFns = new Set();
108109
};
109110

110111
useUnmount(() => {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export { useHttp } from './http';
2-
export { TOKEN, useRefreshToken } from './token';
2+
export { TOKEN } from './token';
33
export { GlobalStore } from './store';
44
export { useInit } from './useInit';
55
export { useMenu } from './useMenu';

packages/platform/src/app/core/token/token.ts renamed to packages/platform/src/app/core/token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { isNull } from 'lodash';
22

33
import { useStorage } from '@react-devui/hooks';
44

5-
import { environment } from '../../../environments';
6-
import { base64url } from '../../utils';
5+
import { environment } from '../../environments';
6+
import { base64url } from '../utils';
77

88
export const TOKEN_KEY = 'token';
99
export const TOKEN_TYPE: 'JWT' | 'CUSTOM' = 'JWT';

packages/platform/src/app/core/token/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/platform/src/app/core/token/useRefreshToken.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.
Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
11
import type { AppNotification, AppUser } from './store';
22

3+
import { isNull } from 'lodash';
4+
35
import { useACL } from '@react-devui/hooks';
46

57
import { ROLE_ACL } from '../config/acl';
68
import { useHttp } from './http';
79
import { GlobalStore } from './store';
8-
import { useRefreshToken } from './token';
10+
import { TOKEN, TOKEN_REFRESH, TOKEN_REFRESH_OFFSET } from './token';
11+
12+
let CLEAR_TOKEN_REFRESH: (() => void) | undefined;
913

1014
export function useInit() {
1115
const http = useHttp();
1216
const acl = useACL();
1317

14-
const refreshToken = useRefreshToken();
18+
return (user: AppUser) => {
19+
acl.setFull(user.permission.includes(ROLE_ACL.super_admin));
20+
acl.set(user.permission);
1521

16-
const handleUser = (user: AppUser) => {
1722
GlobalStore.set('appUser', user);
1823

19-
//#region ACL
20-
acl.setFull(user.permission.includes(ROLE_ACL.super_admin));
21-
acl.set(user.permission);
22-
//#endregion
23-
};
24+
GlobalStore.set('appMenu', (draft) => {
25+
draft.expands = undefined;
26+
});
2427

25-
const getNotification = () => {
2628
GlobalStore.set('appNotifications', undefined);
27-
2829
http<AppNotification[]>(
2930
{
3031
url: '/notification',
@@ -36,18 +37,37 @@ export function useInit() {
3637
GlobalStore.set('appNotifications', res);
3738
},
3839
});
39-
};
4040

41-
const resetMenu = () => {
42-
GlobalStore.set('appMenu', (draft) => {
43-
draft.expands = undefined;
44-
});
45-
};
41+
CLEAR_TOKEN_REFRESH?.();
42+
if (TOKEN_REFRESH) {
43+
const refresh = () => {
44+
const expiration = TOKEN.expiration;
45+
if (!isNull(expiration) && !TOKEN.expired) {
46+
const tid = window.setTimeout(() => {
47+
const refreshTokenReq = http<string>(
48+
{
49+
url: '/auth/refresh',
50+
method: 'post',
51+
},
52+
{ unmount: false }
53+
);
54+
refreshTokenReq.subscribe({
55+
next: (res) => {
56+
TOKEN.set(res);
4657

47-
return (user: AppUser) => {
48-
refreshToken();
49-
handleUser(user);
50-
getNotification();
51-
resetMenu();
58+
refresh();
59+
},
60+
});
61+
CLEAR_TOKEN_REFRESH = () => {
62+
refreshTokenReq.abort();
63+
};
64+
}, expiration - Date.now() - TOKEN_REFRESH_OFFSET);
65+
CLEAR_TOKEN_REFRESH = () => {
66+
clearTimeout(tid);
67+
};
68+
}
69+
};
70+
refresh();
71+
}
5272
};
5373
}

packages/platform/src/app/hooks/useTable.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

packages/platform/src/app/routes/login/Login.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ export default AppRoute(() => {
7272
{ authorization: false }
7373
).subscribe({
7474
next: (res) => {
75-
setLoginLoading(false);
7675
TOKEN.set(res.token);
76+
77+
setLoginLoading(false);
7778
init(res.user);
7879
navigate(isString(from) && from !== LOGIN_PATH ? from : '/', { replace: true });
7980
},

0 commit comments

Comments
 (0)