|
|
|
@ -2,21 +2,13 @@ import React from 'react'; |
|
|
|
import PromiseRender from './PromiseRender'; |
|
|
|
import { CURRENT } from './renderAuthorize'; |
|
|
|
|
|
|
|
function isPromise(obj) { |
|
|
|
return ( |
|
|
|
!!obj && |
|
|
|
(typeof obj === 'object' || typeof obj === 'function') && |
|
|
|
typeof obj.then === 'function' |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 通用权限检查方法 |
|
|
|
* Common check permissions method |
|
|
|
* @param { 权限判定 Permission judgment type string |array | Promise | Function } authority |
|
|
|
* @param { 你的权限 Your permission description type:string} currentAuthority |
|
|
|
* @param { 通过的组件 Passing components } target |
|
|
|
* @param { 未通过的组件 no pass components } Exception |
|
|
|
* @param { 权限判定 | Permission judgment } authority |
|
|
|
* @param { 你的权限 | Your permission description } currentAuthority |
|
|
|
* @param { 通过的组件 | Passing components } target |
|
|
|
* @param { 未通过的组件 | no pass components } Exception |
|
|
|
*/ |
|
|
|
const checkPermissions = (authority, currentAuthority, target, Exception) => { |
|
|
|
// 没有判定权限.默认查看所有
|
|
|
|
@ -26,47 +18,36 @@ const checkPermissions = (authority, currentAuthority, target, Exception) => { |
|
|
|
} |
|
|
|
// 数组处理
|
|
|
|
if (Array.isArray(authority)) { |
|
|
|
if (authority.indexOf(currentAuthority) >= 0) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
if (Array.isArray(currentAuthority)) { |
|
|
|
for (let i = 0; i < currentAuthority.length; i += 1) { |
|
|
|
const element = currentAuthority[i]; |
|
|
|
if (authority.indexOf(element) >= 0) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
if (currentAuthority.some(item => authority.includes(item))) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
} else if (authority.includes(currentAuthority)) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
return Exception; |
|
|
|
} |
|
|
|
|
|
|
|
// string 处理
|
|
|
|
if (typeof authority === 'string') { |
|
|
|
if (authority === currentAuthority) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
if (Array.isArray(currentAuthority)) { |
|
|
|
for (let i = 0; i < currentAuthority.length; i += 1) { |
|
|
|
const element = currentAuthority[i]; |
|
|
|
if (authority === element) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
if (currentAuthority.some(item => authority === item)) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
} else if (authority === currentAuthority) { |
|
|
|
return target; |
|
|
|
} |
|
|
|
return Exception; |
|
|
|
} |
|
|
|
|
|
|
|
// Promise 处理
|
|
|
|
if (isPromise(authority)) { |
|
|
|
if (authority instanceof Promise) { |
|
|
|
return <PromiseRender ok={target} error={Exception} promise={authority} />; |
|
|
|
} |
|
|
|
|
|
|
|
// Function 处理
|
|
|
|
if (typeof authority === 'function') { |
|
|
|
try { |
|
|
|
const bool = authority(currentAuthority); |
|
|
|
// 函数执行后返回值是 Promise
|
|
|
|
if (isPromise(bool)) { |
|
|
|
if (bool instanceof Promise) { |
|
|
|
return <PromiseRender ok={target} error={Exception} promise={bool} />; |
|
|
|
} |
|
|
|
if (bool) { |
|
|
|
|