diff --git a/src/components/Authorized/CheckPermissions.js b/src/components/Authorized/CheckPermissions.js index a62deed48738ba915a4732af995eceb99c066d0d..0a7dbaedf7d9c99b8f0d17cfd517f1b42325cdd2 100644 --- a/src/components/Authorized/CheckPermissions.js +++ b/src/components/Authorized/CheckPermissions.js @@ -46,7 +46,7 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => { // Function 处理 if (typeof authority === 'function') { try { - const bool = authority(); + const bool = authority(currentAuthority); if (bool) { return target; } diff --git a/src/components/Authorized/index.d.ts b/src/components/Authorized/index.d.ts index 45df4e8b035272699fc2e94a238179dc5aaa804e..4a68fa18138545b62d563c2d36f5126d490e375a 100644 --- a/src/components/Authorized/index.d.ts +++ b/src/components/Authorized/index.d.ts @@ -1,7 +1,7 @@ import * as React from 'react'; import { RouteProps } from 'react-router'; -type authorityFN = () => string; +type authorityFN = (currentAuthority?: string) => boolean; type authority = string | Array | authorityFN | Promise; diff --git a/src/components/Authorized/index.md b/src/components/Authorized/index.md index 1fe99615526048702c190cdaafc05b7bfe141a4b..51588bb5a5e22ff49bb048f128773bf67146277f 100644 --- a/src/components/Authorized/index.md +++ b/src/components/Authorized/index.md @@ -17,6 +17,7 @@ order: 15 权限组件默认 export RenderAuthorized 函数,它接收当前权限作为参数,返回一个权限对象,该对象提供以下几种使用方式。 + ### Authorized 最基础的权限控制。 @@ -24,14 +25,14 @@ order: 15 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| | children | 正常渲染的元素,权限判断通过时展示 | ReactNode | - | -| authority | 准入权限/权限判断 | `string | array | Promise | () => boolean` | - | +| authority | 准入权限/权限判断 | `string | array | Promise | (currentAuthority) => boolean` | - | | noMatch | 权限异常渲染元素,权限判断不通过时展示 | ReactNode | - | ### Authorized.AuthorizedRoute | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| authority | 准入权限/权限判断 | `string | array | Promise | () => boolean` | - | +| authority | 准入权限/权限判断 | `string | array | Promise | (currentAuthority) => boolean` | - | | redirectPath | 权限异常时重定向的页面路由 | string | - | 其余参数与 `Route` 相同。 @@ -42,7 +43,7 @@ order: 15 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| authority | 准入权限/权限判断 | `string | Promise | () => boolean` | - | +| authority | 准入权限/权限判断 | `string | Promise | (currentAuthority) => boolean` | - | | error | 权限异常时渲染元素 | ReactNode | | ### Authorized.check @@ -52,6 +53,6 @@ order: 15 | 参数 | 说明 | 类型 | 默认值 | |----------|------------------------------------------|-------------|-------| -| authority | 准入权限/权限判断 | `string | Promise | () => boolean` | - | -| target | 权限判断通过时渲染的元素 | `string | array | Promise | () => boolean` | - | +| authority | 准入权限/权限判断 | `string | Promise | (currentAuthority) => boolean` | - | +| target | 权限判断通过时渲染的元素 | ReactNode | - | | Exception | 权限异常时渲染元素 | ReactNode | - | diff --git a/src/models/login.js b/src/models/login.js index 5efd5f19fdb18a714b031de73b9f4a6706e50d87..ea9fd30af079595d69a14199d9310d184466e2b6 100644 --- a/src/models/login.js +++ b/src/models/login.js @@ -1,5 +1,7 @@ +import { routerRedux } from 'dva/router'; import { fakeAccountLogin } from '../services/api'; import { setAuthority } from '../utils/authority'; +import { reloadAuthorized } from '../utils/Authorized'; export default { namespace: 'login', @@ -17,11 +19,8 @@ export default { }); // Login successfully if (response.status === 'ok') { - // 非常粗暴的跳转,登陆成功之后权限会变成user或admin,会自动重定向到主页 - // Login success after permission changes to admin or user - // The refresh will automatically redirect to the home page - // yield put(routerRedux.push('/')); - window.location.reload(); + reloadAuthorized(); + yield put(routerRedux.push('/')); } }, *logout(_, { put, select }) { @@ -33,9 +32,6 @@ export default { urlParams.searchParams.set('redirect', pathname); window.history.replaceState(null, 'login', urlParams.href); } finally { - // yield put(routerRedux.push('/user/login')); - // Login out after permission changes to admin or user - // The refresh will automatically redirect to the login page yield put({ type: 'changeLoginStatus', payload: { @@ -43,7 +39,8 @@ export default { currentAuthority: 'guest', }, }); - window.location.reload(); + reloadAuthorized(); + yield put(routerRedux.push('/user/login')); } }, }, diff --git a/src/router.js b/src/router.js index 4ba7297663eb9832edc736d41da6b32ddfd2726b..54eefd1702fc5b99c336fb5a82c9c7d1005f0215 100644 --- a/src/router.js +++ b/src/router.js @@ -24,7 +24,6 @@ function RouterConfig({ history, app }) { } - authority="guest" redirectPath="/" /> { + Authorized = RenderAuthorized(getAuthority()); +}; + +export { reloadAuthorized }; export default Authorized;