diff --git a/UserLogin/package.json b/UserLogin/package.json index b958db3012764bce3231c0a10ac64722c25895f6..ba65d83ee401ac3c3e1ede080121a587aa264cbb 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 9c327d3bc0583143dc5f8767aaa1dbc3f6c2fc94..417380c1cd8f900446857305b7b104e847b71573 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 2a206ecc4e13d52cb6776bd09cfc6da9dace59d4..a63d0f8b89b912ca22542cccca3612c5fec47768 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)); +}