permission.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import router from '@/router'
  2. import store from '@/store'
  3. import { getToken, getUserInfo, initGoPage } from '@/utils/auth'
  4. // import getPageTitle from './utils/get-page-title'
  5. // 获取用户信息
  6. const userInfo = getUserInfo()
  7. if (userInfo) {
  8. store.commit('user/SAVE_USER_INOF', userInfo)
  9. }
  10. // 路由白名单
  11. const whiteList = ['/login']
  12. const shareList = ['Share', 'SharePayVip', 'SharePaySuccess', 'SharePayFaild']
  13. // 路由拦截器
  14. router.beforeEach(async(to, from, next) => {
  15. const hasToken = getToken()
  16. // 设置页面标题
  17. // document.title = getPageTitle(to.meta.title)
  18. if (shareList.indexOf(to.name) > -1) {
  19. next()
  20. return
  21. }
  22. if (hasToken) {
  23. if (to.path === '/login') {
  24. next({ path: initGoPage() })
  25. } else {
  26. store.dispatch('app/setCountry')
  27. const hasRoles = store.getters.roles && store.getters.roles.length > 0
  28. if (hasRoles) {
  29. next()
  30. } else {
  31. try {
  32. // 代理状态更换时清空全部页面标签
  33. console.log('代理状态切换 ', store.getters.isChangeProxy)
  34. if (store.getters.isChangeProxy) {
  35. store.commit('tagsView/CLEAR_ALL_VIEW')
  36. store.commit('proxy/CHANGE_PROXY_STATE', false)
  37. }
  38. await store.dispatch('user/fetchUserInfo')
  39. await store.dispatch('user/fetchUserVipInfo')
  40. // 根据用户角色获取路由配置
  41. const accessRoutes = await store.dispatch('permission/generateRoutes')
  42. console.log(accessRoutes)
  43. // 添加路由配置
  44. router.addRoutes(accessRoutes)
  45. // websocket
  46. store.dispatch('notice/start')
  47. // 放行
  48. next({ ...to, path: '/', replace: true })
  49. } catch (err) {
  50. store.dispatch('user/logout')
  51. }
  52. }
  53. }
  54. } else {
  55. if (whiteList.indexOf(to.path) !== -1) {
  56. next()
  57. } else {
  58. next('/login')
  59. }
  60. }
  61. })