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
27declare 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 } ,
0 commit comments