permission.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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','/HCFDLab/chart']// 免登录的'白名单
  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. //console.log(errUril);
  40. // window.location.href=errUril
  41. NProgress.done() // 关闭进度条
  42. })
  43. .catch(() => {
  44. NProgress.done() // 关闭进度条
  45. Message({
  46. type: 'info',
  47. message: '已取消'
  48. })
  49. })
  50. }
  51. }
  52. })
  53. // 后置路由守卫
  54. router.afterEach(() => {
  55. NProgress.done() // 关闭进度条
  56. })