mirror of https://github.com/openspug/spug
fix issues
parent
de2bfe4e2c
commit
26a38665b8
|
@ -10,21 +10,19 @@ import Sider from './Sider';
|
|||
import Header from './Header';
|
||||
import Footer from './Footer'
|
||||
import routes from '../routes';
|
||||
import { updatePermissions, hasPermission } from 'libs';
|
||||
import { hasPermission } from 'libs';
|
||||
import styles from './layout.module.less';
|
||||
|
||||
function initRoutes(routes) {
|
||||
const Routes = [];
|
||||
function initRoutes(Routes, routes) {
|
||||
for (let route of routes) {
|
||||
if (route.component) {
|
||||
if (!route.auth || hasPermission(route.auth)) {
|
||||
Routes.push(<Route exact key={route.path} path={route.path} component={route.component}/>)
|
||||
}
|
||||
} else if (route.child) {
|
||||
initRoutes(route.child)
|
||||
initRoutes(Routes, route.child)
|
||||
}
|
||||
}
|
||||
return Routes
|
||||
}
|
||||
|
||||
// 404
|
||||
|
@ -47,8 +45,9 @@ export default function () {
|
|||
const [Routes, setRoutes] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
updatePermissions();
|
||||
setRoutes(initRoutes(routes));
|
||||
const Routes = [];
|
||||
initRoutes(Routes, routes);
|
||||
setRoutes(Routes)
|
||||
}, [])
|
||||
|
||||
return (
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
* Released under the AGPL-3.0 License.
|
||||
*/
|
||||
let Permission = {
|
||||
isReady: false,
|
||||
isSuper: false,
|
||||
hostPerms: [],
|
||||
permissions: []
|
||||
|
@ -13,6 +14,7 @@ export let X_TOKEN;
|
|||
|
||||
export function updatePermissions() {
|
||||
X_TOKEN = localStorage.getItem('token');
|
||||
Permission.isReady = true;
|
||||
Permission.isSuper = localStorage.getItem('is_supper') === 'true';
|
||||
Permission.hostPerms = JSON.parse(localStorage.getItem('host_perms') || '[]');
|
||||
Permission.permissions = JSON.parse(localStorage.getItem('permissions') || '[]');
|
||||
|
@ -20,8 +22,8 @@ export function updatePermissions() {
|
|||
|
||||
// 前端页面的权限判断(仅作为前端功能展示的控制,具体权限控制应在后端实现)
|
||||
export function hasPermission(strCode) {
|
||||
if (!Permission.isReady) updatePermissions();
|
||||
const {isSuper, permissions} = Permission;
|
||||
// console.log(isSuper, strCode, permissions);
|
||||
if (!strCode || isSuper) return true;
|
||||
for (let or_item of strCode.split('|')) {
|
||||
if (isSubArray(permissions, or_item.split('&'))) {
|
||||
|
|
Loading…
Reference in New Issue