permission.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import router, { adminRouter, normalRouter, agentRouter } from '@/router'
  2. import store from '@/store'
  3. import { checklogin } from '@/api/login'
  4. import { message } from 'ant-design-vue'
  5. const appType = import.meta.env.VITE_APP_TYPE
  6. console.log(appType)
  7. // adminRouter.forEach(r => { router.addRoute(r) }) // 添加静态路由
  8. // normalRouter.forEach(r => { router.addRoute(r) })
  9. // agentRouter.forEach(r => { router.addRoute(r) })
  10. store.commit('SET_ROUTES', normalRouter)
  11. router.beforeEach(async (to, from, next) => {
  12. // const isLogin = await checklogin()
  13. // console.log(isLogin)
  14. const isLogin = false
  15. if (to.path === '/login' || to.path === '/' || to.path === '/init') {
  16. if (isLogin) {
  17. const pathMap = {
  18. admin: '/audit/index',
  19. merchant: '/order/index',
  20. agent: '/scenicOrder/index'
  21. }
  22. next({ path: pathMap[appType], replace: true })
  23. } else {
  24. next()
  25. }
  26. } else {
  27. next()
  28. // if (isLogin) {
  29. // if (store.state.addRoutes) {
  30. // next()
  31. // } else {
  32. // const routerMap = {
  33. // admin: adminRouter,
  34. // merchant: normalRouter,
  35. // agent: agentRouter
  36. // }
  37. // const asyncRouter = routerMap[appType]
  38. // await store.dispatch('getUserPermission').then(permissions => {
  39. // if (permissions) {
  40. // asyncRouter.forEach(r => {
  41. // r.children = r.children.filter(item => {
  42. // return permissions.indexOf(item.meta.permission) > -1 || !item.meta.permission
  43. // })
  44. // })
  45. // }
  46. // store.commit('SET_ROUTES', asyncRouter) // 筛选权限列表
  47. // })
  48. // asyncRouter.forEach(r => {
  49. // router.addRoute(r) // 添加动态路由
  50. // })
  51. // let availableRouters = []
  52. // asyncRouter.forEach(v => {
  53. // v.children.length && (availableRouters = [...availableRouters, ...v.children]) // 将所有可进入的路由 降级为一维数组
  54. // })
  55. // if (availableRouters.length === 0) return message.error('对不起,您暂时没有权限访问该系统,请联系管理员修改权限')
  56. // const path = router.getRoutes().find(i => i.path.indexOf(to.path) > -1) ? to.path : availableRouters[0].path // 取所有可进入路由的第一个
  57. // next({ path, replace: true })
  58. // }
  59. // } else {
  60. // next({ path: '/login', replace: true })
  61. // }
  62. }
  63. })