|
@@ -1,6 +1,7 @@
|
|
|
import {LoadingText} from "../components/loading";
|
|
|
-import {useEffect} from "react";
|
|
|
-import {useNavigate} from "react-router";
|
|
|
+import {useEffect, useMemo} from "react";
|
|
|
+import {useMatches, useNavigate} from "react-router";
|
|
|
+import {useAppSelector} from "../store";
|
|
|
|
|
|
/**
|
|
|
* 顶部的菜单
|
|
@@ -23,6 +24,36 @@ export const NavList = [
|
|
|
}
|
|
|
]
|
|
|
|
|
|
+/**
|
|
|
+ * 是否有菜单全新
|
|
|
+ */
|
|
|
+export const useMatchNav = (checkPermission = false) => {
|
|
|
+ const matches = useMatches()
|
|
|
+ const user = useAppSelector(state => state.user.user)
|
|
|
+ const navigate = useNavigate()
|
|
|
+
|
|
|
+ return useMemo(()=>{
|
|
|
+ let nav = null
|
|
|
+ for (const match of matches) {
|
|
|
+ nav = NavList.find(item=>item.key == match.pathname)
|
|
|
+ if (nav){
|
|
|
+ break
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!nav || nav.roles.length == 0 || !checkPermission){
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ const roles = user?.roles?.split(",") || []
|
|
|
+ for (const r of nav.roles){
|
|
|
+ if (roles.includes(r)){
|
|
|
+ return nav
|
|
|
+ }
|
|
|
+ }
|
|
|
+ navigate('/')
|
|
|
+ return false;
|
|
|
+ },[matches, user, checkPermission])
|
|
|
+}
|
|
|
+
|
|
|
type IProps = {
|
|
|
to: string
|
|
|
}
|