From 26dc3af86a931453bd9019ead19029aa3bd10cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E5=B8=85?= Date: Thu, 23 May 2019 02:34:01 +0800 Subject: [PATCH] fix login status error --- UserLogin/package.json | 3 ++- UserLogin/src/model.ts | 28 ++++++++++++++++++++++++++-- UserLogin/src/utils/utils.ts | 5 +++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/UserLogin/package.json b/UserLogin/package.json index b958db30..ba65d83e 100644 --- a/UserLogin/package.json +++ b/UserLogin/package.json @@ -11,10 +11,11 @@ "url": "https://github.com/umijs/umi-blocks/ant-design-pro/userlogin" }, "dependencies": { + "@types/qs": "^6.5.3", "antd": "^3.16.3", "dva": "^2.4.0", "moment": "^2.22.2", - "query-string": "^6.0.0", + "qs": "^6.7.0", "react": "^16.6.3", "umi-request": "^1.0.0" }, diff --git a/UserLogin/src/model.ts b/UserLogin/src/model.ts index 9c327d3b..417380c1 100644 --- a/UserLogin/src/model.ts +++ b/UserLogin/src/model.ts @@ -1,7 +1,7 @@ import { routerRedux } from 'dva/router'; -import { getPageQuery } from './utils/utils'; +import { getPageQuery, setAuthority } from './utils/utils'; import { fakeAccountLogin, getFakeCaptcha } from './service'; - +import { stringify } from 'qs'; import { Reducer } from 'redux'; import { EffectsCommandMap } from 'dva'; import { AnyAction } from 'redux'; @@ -23,6 +23,7 @@ export interface ModelType { effects: { login: Effect; getCaptcha: Effect; + logout: Effect; }; reducers: { changeLoginStatus: Reducer; @@ -67,10 +68,33 @@ const Model: ModelType = { *getCaptcha({ payload }, { call }) { 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: { changeLoginStatus(state, { payload }) { + setAuthority(payload.currentAuthority); return { ...state, status: payload.status, diff --git a/UserLogin/src/utils/utils.ts b/UserLogin/src/utils/utils.ts index 2a206ecc..a63d0f8b 100644 --- a/UserLogin/src/utils/utils.ts +++ b/UserLogin/src/utils/utils.ts @@ -3,3 +3,8 @@ import { parse } from 'query-string'; export function getPageQuery() { 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)); +} -- GitLab