import React from 'react' ; import { CURRENT } from './renderAuthorize' ; import PromiseRender from './PromiseRender' ; export type IAuthorityType = undefined | string | string [ ] | Promise < boolean > | ( ( currentAuthority : string | string [ ] ) => IAuthorityType ) ; param { 权限判定 | Permission judgment param { 你的权限 | Your permission param { 通过的组件 | Passing components param { 未通过的组件 | no pass const checkPermissions = < T , K > ( authority , currentAuthority , target , Exception ) : T | K | React . ReactNode => { if ( ! authority ) { return target ; } if ( Array . isArray ( authority ) ) { if ( Array . isArray ( currentAuthority ) ) { if ( currentAuthority . some ( ( item ) => authority . includes ( item ) ) ) { return target ; } } else if ( authority . includes ( currentAuthority ) ) { return target ; } return Exception ; } if ( typeof authority === 'string' ) { if ( Array . isArray ( currentAuthority ) ) { if ( currentAuthority . some ( ( item ) => authority === item ) ) { return target ; } } else if ( authority === currentAuthority ) { return target ; } return Exception ; } if ( authority instanceof Promise ) { return < PromiseRender < T , K > ok = { target } error = { Exception } promise = { authority } / > ; } if ( typeof authority === 'function' ) { try { const bool = authority ( currentAuthority ) ; if ( bool instanceof Promise ) { return < PromiseRender < T , K > ok = { target } error = { Exception } promise = { bool } / > ; } if ( bool ) { return target ; } return Exception ; } catch ( error ) { throw error ; } } throw new Error ( 'unsupported parameters' ) ; } ; export { checkPermissions } ; function check < T , K > ( authority , target , Exception ) { return checkPermissions < T , K > ( authority , CURRENT , target , Exception ) ; } export default check ;