Commit 26dc3af8 authored by 陈帅's avatar 陈帅

fix login status error

parent 825425c5
...@@ -11,10 +11,11 @@ ...@@ -11,10 +11,11 @@
"url": "https://github.com/umijs/umi-blocks/ant-design-pro/userlogin" "url": "https://github.com/umijs/umi-blocks/ant-design-pro/userlogin"
}, },
"dependencies": { "dependencies": {
"@types/qs": "^6.5.3",
"antd": "^3.16.3", "antd": "^3.16.3",
"dva": "^2.4.0", "dva": "^2.4.0",
"moment": "^2.22.2", "moment": "^2.22.2",
"query-string": "^6.0.0", "qs": "^6.7.0",
"react": "^16.6.3", "react": "^16.6.3",
"umi-request": "^1.0.0" "umi-request": "^1.0.0"
}, },
......
import { routerRedux } from 'dva/router'; import { routerRedux } from 'dva/router';
import { getPageQuery } from './utils/utils'; import { getPageQuery, setAuthority } from './utils/utils';
import { fakeAccountLogin, getFakeCaptcha } from './service'; import { fakeAccountLogin, getFakeCaptcha } from './service';
import { stringify } from 'qs';
import { Reducer } from 'redux'; import { Reducer } from 'redux';
import { EffectsCommandMap } from 'dva'; import { EffectsCommandMap } from 'dva';
import { AnyAction } from 'redux'; import { AnyAction } from 'redux';
...@@ -23,6 +23,7 @@ export interface ModelType { ...@@ -23,6 +23,7 @@ export interface ModelType {
effects: { effects: {
login: Effect; login: Effect;
getCaptcha: Effect; getCaptcha: Effect;
logout: Effect;
}; };
reducers: { reducers: {
changeLoginStatus: Reducer<IStateType>; changeLoginStatus: Reducer<IStateType>;
...@@ -67,10 +68,33 @@ const Model: ModelType = { ...@@ -67,10 +68,33 @@ const Model: ModelType = {
*getCaptcha({ payload }, { call }) { *getCaptcha({ payload }, { call }) {
yield call(getFakeCaptcha, payload); yield call(getFakeCaptcha, payload);
}, },
*logout(_, { put }) {
yield put({
type: 'changeLoginStatus',
payload: {
status: false,
currentAuthority: 'guest',
},
});
const { redirect } = getPageQuery();
// redirect
if (window.location.pathname !== '/user/login' && !redirect) {
yield put(
routerRedux.replace({
pathname: '/user/login',
search: stringify({
redirect: window.location.href,
}),
}),
);
}
},
}, },
reducers: { reducers: {
changeLoginStatus(state, { payload }) { changeLoginStatus(state, { payload }) {
setAuthority(payload.currentAuthority);
return { return {
...state, ...state,
status: payload.status, status: payload.status,
......
...@@ -3,3 +3,8 @@ import { parse } from 'query-string'; ...@@ -3,3 +3,8 @@ import { parse } from 'query-string';
export function getPageQuery() { export function getPageQuery() {
return parse(window.location.href.split('?')[1]); return parse(window.location.href.split('?')[1]);
} }
export function setAuthority(authority: string | string[]) {
const proAuthority = typeof authority === 'string' ? [authority] : authority;
return localStorage.setItem('antd-pro-authority', JSON.stringify(proAuthority));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment