github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/static_source/admin/src/permission.ts (about)

     1  import router from './router'
     2  import { useAppStoreWithOut } from '@/store/modules/app'
     3  import { useCache } from '@/hooks/web/useCache'
     4  import type { RouteRecordRaw } from 'vue-router'
     5  import { useTitle } from '@/hooks/web/useTitle'
     6  import { useNProgress } from '@/hooks/web/useNProgress'
     7  import { usePermissionStoreWithOut } from '@/store/modules/permission'
     8  import { useDictStoreWithOut } from '@/store/modules/dict'
     9  import { usePageLoading } from '@/hooks/web/usePageLoading'
    10  
    11  const permissionStore = usePermissionStoreWithOut()
    12  
    13  const appStore = useAppStoreWithOut()
    14  
    15  const dictStore = useDictStoreWithOut()
    16  
    17  const { wsCache } = useCache()
    18  
    19  const { start, done } = useNProgress()
    20  
    21  const { loadStart, loadDone } = usePageLoading()
    22  
    23  const whiteList = ['/login', '/password_reset'] // 不重定向白名单
    24  
    25  router.beforeEach(async (to, from, next) => {
    26    start()
    27    loadStart()
    28    if (appStore.getUser && appStore.getToken) {
    29      if (to.path === '/login') {
    30        next({ path: '/' })
    31      } else {
    32        // if (!dictStore.getIsSetDict) {
    33        //   // 获取所有字典
    34        //   const res = await getDictApi()
    35        //   if (res) {
    36        //     dictStore.setDictObj(res.data)
    37        //     dictStore.setIsSetDict(true)
    38        //   }
    39        // }
    40        if (permissionStore.getIsAddRouters) {
    41          next()
    42          return
    43        }
    44  
    45        // 开发者可根据实际情况进行修改
    46        const roleRouters = wsCache.get('roleRouters') || []
    47        const userInfo = wsCache.get('user')
    48  
    49        // 是否使用动态路由
    50        if (appStore.getDynamicRouter) {
    51          userInfo.role === 'admin'
    52            ? await permissionStore.generateRoutes('admin', roleRouters as AppCustomRouteRecordRaw[])
    53            : await permissionStore.generateRoutes('test', roleRouters as string[])
    54        } else {
    55          await permissionStore.generateRoutes('none')
    56        }
    57  
    58        permissionStore.getAddRouters.forEach((route) => {
    59          router.addRoute(route as unknown as RouteRecordRaw) // 动态添加可访问路由表
    60        })
    61        const redirectPath = from.query.redirect || to.path
    62        const redirect = decodeURIComponent(redirectPath as string)
    63        const nextData = to.path === redirect ? { ...to, replace: true } : { path: redirect }
    64        permissionStore.setIsAddRouters(true)
    65        next(nextData)
    66      }
    67    } else {
    68      if (whiteList.indexOf(to.path) !== -1 || to.path.includes('/landing')) {
    69        next()
    70      } else {
    71        next(`/login?redirect=${to.path}`) // 否则全部重定向到登录页
    72      }
    73    }
    74  })
    75  
    76  router.afterEach((to) => {
    77    useTitle(to?.meta?.title as string)
    78    done() // 结束Progress
    79    loadDone()
    80  })