猫妈狗爸伴宠师小程序后端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.8 KiB

3 months ago
  1. import router from './router'
  2. import store from './store'
  3. import { Message } from 'element-ui'
  4. import NProgress from 'nprogress'
  5. import 'nprogress/nprogress.css'
  6. import { getToken } from '@/utils/auth'
  7. import { isRelogin } from '@/utils/request'
  8. NProgress.configure({ showSpinner: false })
  9. const whiteList = ['/login', '/auth-redirect', '/bind', '/register']
  10. router.beforeEach((to, from, next) => {
  11. NProgress.start()
  12. if (window._hmt) {
  13. if (to.path) {
  14. window._hmt.push(['_trackPageview', '/#' + to.fullPath]);
  15. }
  16. }
  17. if (getToken()) {
  18. to.meta.title && store.dispatch('settings/setTitle', to.meta.title)
  19. /* has token*/
  20. if (to.path === '/login') {
  21. next({ path: '/' })
  22. NProgress.done()
  23. } else {
  24. //当页面刷新,vuex里面没值,请求重新获取,数据会重新初始化
  25. if (store.getters.roles.length === 0) {
  26. isRelogin.show = true
  27. // 判断当前用户是否已拉取完user_info信息
  28. store.dispatch('GetInfo').then(() => {
  29. isRelogin.show = false
  30. store.dispatch('GenerateRoutes').then(accessRoutes => {
  31. // 根据roles权限生成可访问的路由表
  32. router.addRoutes(accessRoutes) // 动态添加可访问路由表
  33. next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
  34. })
  35. }).catch(err => {
  36. store.dispatch('LogOut').then(() => {
  37. Message.error(err)
  38. next({ path: '/' })
  39. })
  40. })
  41. } else {
  42. next()
  43. }
  44. }
  45. } else {
  46. // 没有token
  47. if (whiteList.indexOf(to.path) !== -1) {
  48. // 在免登录白名单,直接进入
  49. next()
  50. } else {
  51. next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  52. NProgress.done()
  53. }
  54. }
  55. })
  56. router.afterEach(() => {
  57. NProgress.done()
  58. })