permission.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import store from '@/store'
  2. export default (router) => {
  3. // 路由白名单,可以考虑不同环境配置不同白名单
  4. const whiteList = ['/pages/index/index', '/pages/home/home', '/pages/login/login'] // no redirect whitelist
  5. // 全局路由前置守卫
  6. router.beforeEach(async (to, from, next) => {
  7. const token = store.state.vuex_access_token
  8. const userId = store.state.vuex_user_info
  9. if (token) {
  10. if (to.path === '/pages/login/login') {
  11. next()
  12. } else if (!userId) {
  13. try {
  14. // await store.dispatch('user/getUserData')
  15. next()
  16. } catch (error) {
  17. // await store.dispatch('user/logout')
  18. next({
  19. path: '/pages/login/index',
  20. query: {
  21. redirect: JSON.stringify({
  22. path: to.path,
  23. query: to.query
  24. })
  25. }
  26. })
  27. }
  28. } else {
  29. next()
  30. }
  31. } else if (whiteList.indexOf(to.path) !== -1) {
  32. // 在免登录白名单,直接进入
  33. next()
  34. } else {
  35. next({
  36. path: '/pages/account/login/index',
  37. query: {
  38. redirectView: JSON.stringify({
  39. path: to.path,
  40. query: to.query
  41. })
  42. }
  43. })
  44. }
  45. })
  46. // 全局路由后置守卫
  47. // eslint-disable-next-line
  48. router.afterEach((to, from) => {
  49. // console.log('router.afterEach.to', to)
  50. // console.log('router.afterEach.from', from)
  51. })
  52. }