permission.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import router from './router' // vue官方的路由
  2. import store from './store' // vue状态管理
  3. import { Message, MessageBox } from 'element-ui' // 引入Message、MessageBox
  4. import NProgress from 'nprogress' // 进度条
  5. import 'nprogress/nprogress.css' // 进度条样式
  6. import getPageTitle from '@/utils/get-page-title' // 获取页面标题
  7. import { Caegw_LogUrl } from '@/settings' // 引入settings.js
  8. NProgress.configure({ showSpinner: false }) // NProgress配置
  9. const whiteList = ['/fail404', '/home','/sso','/login/index','/register/index', '/forget/index', '/protocol/index','/indexLayout/index','/index',]// 免登录的'白名单
  10. // 前置路由守卫
  11. router.beforeEach(async (to, from, next) => {
  12. NProgress.start() // 开启进度条
  13. document.title = getPageTitle(to.meta.title) // 设置页面标题
  14. const token = store.getters.token // 确定用户是否已登录
  15. if (token) {
  16. if (to.path === '/login/index') {
  17. next({ path: '/index' }) // 重定向到主页
  18. NProgress.done() // 关闭进度条
  19. } else {
  20. next() // 直接进入
  21. NProgress.done() // 关闭进度
  22. }
  23. } else {
  24. if (whiteList.indexOf(to.path) !== -1) {
  25. next() // 在免登录白名单中,直接进入
  26. NProgress.done() // 关闭进度条
  27. } else {
  28. // next(`/login?redirect=${to.path}`) // 在没有访问权限的其他页面将重定向到登录页
  29. // NProgress.done() // 关闭进度条
  30. MessageBox.confirm('你还未登录,请登录后再操作, 是否登录?', '提示', {
  31. confirmButtonText: '登录',
  32. cancelButtonText: '取消',
  33. type: 'warning'
  34. })
  35. .then(() => {
  36. next(`/indexLayout/index'?type=reLogin`)
  37. // next(`/login/index?redirect=${to.path}`) // 在没有访问权限的其他页面将重定向到登录页
  38. // const errUril = window.location.protocol+"//"+window.location.host+"/"+this.$store.getters.logUrl
  39. // window.location.href=errUril
  40. NProgress.done() // 关闭进度条
  41. })
  42. .catch(() => {
  43. NProgress.done() // 关闭进度条
  44. Message({
  45. type: 'info',
  46. message: '已取消'
  47. })
  48. })
  49. }
  50. }
  51. })
  52. // 后置路由守卫
  53. router.afterEach(() => {
  54. NProgress.done() // 关闭进度条
  55. })