Skip to content
This repository was archived by the owner on Jan 26, 2022. It is now read-only.

Commit 2c99aae

Browse files
committed
Update global model
1 parent e55714b commit 2c99aae

File tree

6 files changed

+160
-17
lines changed

6 files changed

+160
-17
lines changed

src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const app: any = dva({
1818
errorHandle(error, app._store.dispatch);
1919
},
2020

21-
onAction: logger,
21+
onAction: [logger],
2222

2323
// 其他第三方 Reducer 配置
2424
extraReducers: {

src/models/global.ts

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
import { routerRedux } from 'dva/router';
2+
import { GlobalService } from '../services/GlobalService';
3+
import { exceptionRoutes, localStorageKey } from '../utils/constant';
4+
import { isEmpty } from '../utils/utils';
5+
16
// Global declare
27
declare global {
38
interface Window { ga: any; }
@@ -30,7 +35,7 @@ export default {
3035

3136
state: { // 应用的状态数据
3237
auth: {
33-
isAuthenticated: true,
38+
isAuthenticated: false,
3439
permissions: [],
3540
},
3641
userInfo: {
@@ -45,11 +50,49 @@ export default {
4550
},
4651

4752
effects: { // 异步请求处理和业务逻辑操作
53+
*login(action: any, { call, put }: any) {
54+
try {
55+
const { payload } = action;
56+
const res = yield call(GlobalService.auth, payload);
57+
yield put({
58+
type: 'authorize',
59+
payload: {
60+
isAuthenticated: true,
61+
permissions: [],
62+
},
63+
});
64+
} catch (error) {
65+
throw error;
66+
}
67+
},
4868

69+
*logout(action: any, { put }: any) {
70+
yield put({
71+
type: 'deauthorize',
72+
payload: {
73+
isAuthenticated: false,
74+
permissions: [],
75+
collapsed: false,
76+
},
77+
});
78+
yield put(routerRedux.push('/login'));
79+
},
4980
},
5081

5182
reducers: { // Redux reducers
83+
authorize(state: GlobalState, { payload }: any) {
84+
localStorage.setItem(localStorageKey.APP_KEY_STORE, JSON.stringify(payload));
85+
return { ...state, auth: payload };
86+
},
87+
88+
deauthorize(state: GlobalState, { payload }: any) {
89+
localStorage.removeItem(localStorageKey.APP_KEY_STORE);
90+
localStorage.removeItem(localStorageKey.USER_KEY_STORE);
91+
return { ...state, auth: payload };
92+
},
93+
5294
changeLayoutCollapsed(state: GlobalState, { payload }: any) {
95+
localStorage.setItem(localStorageKey.APP_VIEW_STORE, JSON.stringify({ collapsed: payload }));
5396
return { ...state, collapsed: payload };
5497
},
5598

@@ -60,15 +103,45 @@ export default {
60103
loadNotices(state: GlobalState, { payload, hasMore }: any) {
61104
return { ...state, notices: payload, hasMore };
62105
},
106+
107+
saveAuthData(state: GlobalState, { payload }: any) {
108+
localStorage.setItem(localStorageKey.APP_KEY_STORE, JSON.stringify(payload));
109+
return state;
110+
},
63111
},
64112

65113
subscriptions: { // 用于订阅一个数据源, 然后根据需要 dispatch 相应的 action
66114
setup({ dispatch, history }: any) {
67-
// TODO: Need to add auth and permissions
68115
return history.listen(({ pathname, search }: Location) => {
69-
if (typeof window.ga !== 'undefined') {
70-
// TODO: Google Analytics config, 埋点配置
71-
window.ga('send', 'PageView', pathname + search);
116+
if (exceptionRoutes.includes(pathname)) {
117+
return;
118+
}
119+
120+
const appData = JSON.parse(localStorage.getItem(localStorageKey.APP_KEY_STORE) || '{}');
121+
const appView = JSON.parse(localStorage.getItem(localStorageKey.APP_VIEW_STORE) || '{}');
122+
123+
try {
124+
if (!isEmpty(appData)) {
125+
dispatch({
126+
type: 'changeLayoutCollapsed',
127+
payload: !isEmpty(appView) ? appView.collapsed : false,
128+
});
129+
130+
dispatch({
131+
type: 'authorize',
132+
payload: appData,
133+
});
134+
135+
if (pathname === '/login') {
136+
dispatch(routerRedux.push('/'));
137+
}
138+
} else {
139+
if (pathname === '/login') {
140+
return;
141+
}
142+
}
143+
} catch (error) {
144+
throw error;
72145
}
73146
});
74147
},

src/pages/Login/Login.module.scss

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,76 @@
11
.login {
2-
display: flex;
3-
justify-content: center;
4-
align-items: center;
5-
height: 100%;
6-
font-size: 72px;
7-
overflow: hidden;
8-
background-color: #ffffff;
2+
display: flex;
3+
justify-content: center;
4+
align-items: center;
5+
height: 100%;
6+
font-size: 72px;
7+
overflow: hidden;
8+
background-color: #ffffff;
9+
// background-image: url('/images/background.svg');
10+
}
11+
12+
.Header__svg {
13+
position: absolute;
14+
width: 100%;
15+
top: 50%;
16+
transform: translateY(-50%);
17+
z-index: -1;
18+
will-change: transform;
19+
}
20+
.Header__title {
21+
font-family: Avenir, Futura, 'Open Sans', 'Gill Sans', 'Helvetica Neue', Ariel, sans-serif;
22+
font-weight: bold;
23+
font-size: 6vw;
24+
margin: 0;
25+
}
26+
27+
.bigSquare {
28+
animation-name: bigSquare;
29+
}
30+
@keyframes bigSquare {
31+
from { transform: translateY(10%) rotate(-80deg) scale(0); }
32+
to { transform: translateY(0) rotate(0deg) scale(1); }
33+
}
34+
.littleSquare {
35+
animation-name: littleSquare;
36+
}
37+
@keyframes littleSquare {
38+
from { transform: translate(226%, 183%) rotate(140deg) scale(0) ; }
39+
to { transform: translate(0%, 0%) rotate(0deg) scale(1); }
40+
}
41+
.triangle {
42+
animation-name: triangle;
43+
}
44+
@keyframes triangle {
45+
from { transform: rotate(-140deg) scale(0) ; }
46+
to { transform: rotate(0deg) scale(1); }
47+
}
48+
.hoop {
49+
animation-name: hoop;
50+
}
51+
@keyframes hoop {
52+
from { transform: translate(-160%, -33%) scale(0) ; }
53+
to { transform: translate(0%, 0%) scale(1); }
54+
}
55+
.bigCircle {
56+
animation-name: bigCircle;
57+
}
58+
@keyframes bigCircle {
59+
from { transform: scale(0) translate(60%, 3%); }
60+
to { transform: scale(1) translate(0%, 0%); }
61+
}
62+
.littleCircle {
63+
animation-name: littleCircle;
64+
}
65+
@keyframes littleCircle {
66+
from { transform: scale(0) }
67+
to { transform: scale(1) }
68+
}
69+
70+
// some lovely sass fun to stagger the animation
71+
72+
@for $i from 1 to 12 {
73+
.Header__shape:nth-child(#{$i}) {
74+
animation-delay: $i * 0.16s;
75+
}
976
}

src/routes/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ export default class AppRoutes extends Component<InternalProps> {
8686
};
8787

8888
return config.component ? route(config) :
89-
config.children && config.children.map((d: RouteConfig) => route(d));
89+
config.children && config.children.map((d: RouteConfig) => route(d));
9090
})
9191
)}
9292
<Route path="/app/exception/403" component={NoPermission} />
9393
<Route path="/app/exception/404" component={NotFound} />
9494
<Route path="/app/exception/500" component={ServerError} />
95-
<Route render={() => <Redirect to="/exception/404" />} />
95+
<Route render={() => <Redirect to="/app/exception/404" />} />
9696
</Switch>
9797
);
9898
}

src/services/ApiConfig.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
// API url config
2-
export const ApiUrl = {};
2+
export const ApiUrl = {
3+
auth: '/auth',
4+
dashboard: '/dashboard',
5+
};

src/utils/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export function formatSeconds(num: number): string {
8282

8383
/**
8484
* 判断对象或者数组是否为空
85-
* @param obj
85+
* @param {array | object} obj
8686
*/
8787
export function isEmpty(obj: any): boolean {
8888
return [Object, Array].includes((obj || {}).constructor) && !Object.entries((obj || {})).length;

0 commit comments

Comments
 (0)