permission.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import router from '@/router'
  2. import store from '@/store'
  3. // import {
  4. // Notify
  5. // } from 'vant'
  6. import {
  7. getToken
  8. } from '@/utils/auth' // get token from cookie
  9. import getPageTitle from '@/utils/get-page-title'
  10. const whiteList = ['/', '/validation','/hisEnter'] // 白名单列表 ,
  11. router.beforeEach((to, from, next) => {
  12. // 设置页面标题
  13. document.title = getPageTitle(to.meta.title)
  14. const hasToken = getToken()
  15. if (hasToken) {
  16. if (from.path === '/') {
  17. // 已经登录,跳转到首页
  18. next({
  19. path: '/index/todaypatients'
  20. })
  21. } else {
  22. // 获取用户信息
  23. const hasGetUserInfo = localStorage.getItem('TOKEN') // 判断是否存在token
  24. if (hasGetUserInfo) {
  25. next()
  26. } else {
  27. try {
  28. next()
  29. } catch (error) {
  30. // 清除用户信息,退出登录,跳转登录页
  31. store.commit('user/LOGOUT')
  32. // Notify.error(error || 'Has Error')
  33. this.$notify({
  34. title: '标题名称',
  35. message: error || 'Has Error'
  36. });
  37. next(`/login?redirect=${to.path}`)
  38. }
  39. }
  40. }
  41. } else {
  42. /* has no token */
  43. if (whiteList.indexOf(to.path) !== -1) {
  44. // 白名单中,无需验证
  45. next()
  46. } else {
  47. next(`/?redirect=${to.path}`)
  48. }
  49. }
  50. })