|
@@ -3,13 +3,36 @@ import {
|
|
|
createWebHashHistory,
|
|
createWebHashHistory,
|
|
|
type RouteRecordRaw
|
|
type RouteRecordRaw
|
|
|
} from 'vue-router'
|
|
} from 'vue-router'
|
|
|
|
|
+import { usePermissionStoreHook } from '@/stores/modules/permission'
|
|
|
|
|
|
|
|
|
|
|
|
|
const Layouts = () => import('@/layouts/index.vue')
|
|
const Layouts = () => import('@/layouts/index.vue')
|
|
|
|
|
|
|
|
|
|
+/**
|
|
|
|
|
+ * 路由 name 与 FunctionList key 的映射
|
|
|
|
|
+ * 一级菜单: route name → FunctionList 一级 key
|
|
|
|
|
+ * 二级菜单(Settings 子路由): route name → FunctionList Settings 下的 key
|
|
|
|
|
+ */
|
|
|
|
|
+const menuPermissionMap: Record<string, string> = {
|
|
|
|
|
+ // 一级菜单
|
|
|
|
|
+ Settings: 'Settings',
|
|
|
|
|
+ RemoteViewing: 'Remote',
|
|
|
|
|
+ StandardProtocol: 'Protocol',
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+const subMenuPermissionMap: Record<string, string> = {
|
|
|
|
|
+ // Settings 下的二级菜单
|
|
|
|
|
+ SystemSettings: 'System_Settings',
|
|
|
|
|
+ ImageDisplay: 'Image_Display',
|
|
|
|
|
+ AudioVideo: 'Audio_Video',
|
|
|
|
|
+ NetSettings: 'Network_Settings',
|
|
|
|
|
+ AlarmSettings: 'Alarm_Settings',
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export const constantRoutes: RouteRecordRaw[] = [
|
|
export const constantRoutes: RouteRecordRaw[] = [
|
|
|
{
|
|
{
|
|
|
path: '/',
|
|
path: '/',
|
|
|
|
|
+ name: 'PreviewLayout',
|
|
|
component: Layouts,
|
|
component: Layouts,
|
|
|
redirect: '/preview',
|
|
redirect: '/preview',
|
|
|
children: [
|
|
children: [
|
|
@@ -80,6 +103,7 @@ export const constantRoutes: RouteRecordRaw[] = [
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/',
|
|
path: '/',
|
|
|
|
|
+ name: 'RemoteLayout',
|
|
|
component: Layouts,
|
|
component: Layouts,
|
|
|
redirect: '/remoteViewing',
|
|
redirect: '/remoteViewing',
|
|
|
children: [
|
|
children: [
|
|
@@ -97,6 +121,7 @@ export const constantRoutes: RouteRecordRaw[] = [
|
|
|
},
|
|
},
|
|
|
{
|
|
{
|
|
|
path: '/',
|
|
path: '/',
|
|
|
|
|
+ name: 'ProtocolLayout',
|
|
|
component: Layouts,
|
|
component: Layouts,
|
|
|
redirect: '/standardProtocol',
|
|
redirect: '/standardProtocol',
|
|
|
children: [
|
|
children: [
|
|
@@ -129,8 +154,80 @@ const router = createRouter({
|
|
|
|
|
|
|
|
export default router
|
|
export default router
|
|
|
|
|
|
|
|
|
|
+/** 清除所有动态注册的路由 */
|
|
|
|
|
+function resetRouter() {
|
|
|
|
|
+ // 移除所有 constantRoutes 中定义的路由(按 name 移除)
|
|
|
|
|
+ const routeNames: string[] = []
|
|
|
|
|
+ constantRoutes.forEach((route) => {
|
|
|
|
|
+ if (route.name) {
|
|
|
|
|
+ routeNames.push(route.name as string)
|
|
|
|
|
+ }
|
|
|
|
|
+ if (route.children) {
|
|
|
|
|
+ route.children.forEach((child) => {
|
|
|
|
|
+ if (child.name) {
|
|
|
|
|
+ routeNames.push(child.name as string)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ routeNames.forEach((name) => {
|
|
|
|
|
+ if (router.hasRoute(name)) {
|
|
|
|
|
+ router.removeRoute(name)
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/** 根据权限过滤路由并注册 */
|
|
|
export function setRouter() {
|
|
export function setRouter() {
|
|
|
- constantRoutes.forEach((item) => {
|
|
|
|
|
|
|
+ // 先清除旧路由,避免重复注册
|
|
|
|
|
+ resetRouter()
|
|
|
|
|
+
|
|
|
|
|
+ const permissionStore = usePermissionStoreHook()
|
|
|
|
|
+
|
|
|
|
|
+ const filteredRoutes = constantRoutes
|
|
|
|
|
+ .filter((route) => {
|
|
|
|
|
+ // 对于有单个子路由的一级菜单(Preview / Remote / Protocol)
|
|
|
|
|
+ if (route.children && route.children.length === 1) {
|
|
|
|
|
+ const child = route.children[0]
|
|
|
|
|
+ const routeName = child.name as string
|
|
|
|
|
+ const permKey = menuPermissionMap[routeName]
|
|
|
|
|
+ if (permKey && !permissionStore.isMenuVisible(permKey)) {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ // 对于 Settings 这种有多个子路由的一级菜单
|
|
|
|
|
+ if (route.name && menuPermissionMap[route.name as string]) {
|
|
|
|
|
+ const permKey = menuPermissionMap[route.name as string]
|
|
|
|
|
+ if (!permissionStore.isMenuVisible(permKey)) {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+ })
|
|
|
|
|
+ .map((route) => {
|
|
|
|
|
+ // 过滤 Settings 的子路由
|
|
|
|
|
+ if (route.name === 'Settings' && route.children) {
|
|
|
|
|
+ const filteredChildren = route.children.filter((child) => {
|
|
|
|
|
+ const childName = child.name as string
|
|
|
|
|
+ const subPermKey = subMenuPermissionMap[childName]
|
|
|
|
|
+ if (subPermKey) {
|
|
|
|
|
+ return permissionStore.isSubMenuVisible(subPermKey)
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+ })
|
|
|
|
|
+ return { ...route, children: filteredChildren }
|
|
|
|
|
+ }
|
|
|
|
|
+ return route
|
|
|
|
|
+ })
|
|
|
|
|
+ // 如果 Settings 的子路由全被过滤掉了,移除 Settings 本身
|
|
|
|
|
+ .filter((route) => {
|
|
|
|
|
+ if (route.name === 'Settings' && route.children && route.children.length === 0) {
|
|
|
|
|
+ return false
|
|
|
|
|
+ }
|
|
|
|
|
+ return true
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ filteredRoutes.forEach((item) => {
|
|
|
router.addRoute(item)
|
|
router.addRoute(item)
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|