使用Route做登录权限控制
const USER_TYPE ={
0:'EW',
1:'EA',
100:'PA'
}
// 校验身份
/*
* allow_role_list: [G, EW, EA, PA]
* G: guest 未登录的来宾
* EW: enterprise worker 企业员工
* EA: enterprise administrator 企业管理员
* PA: Platform administrator 平台管理员
* */
const authorization = (props, component, allow_role_list = ['EW', 'EA', 'PA']) => {
let role = true;
// 校验参数是否存在
if ( !props || !component)
role = 'G'
{
let signer = localStorage.getItem('signer')
if (!signer)
role = 'G'
else {
signer = JSON.parse(signer)
role = USER_TYPE[signer.userType]
}
}
return allow_role_list.some(item=>item===role) ? component : <Redirect to='/sign/in'/>
}
<Route path='/cms'
exact
render={props =>
authorization(
props,
<ProjectCreate {...props}/>,
['adminstrator']
)
}
></Route>
网友评论