permission.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * @Date: 2023-06-19 13:38:07
  3. * @LastEditors: zhubj
  4. * @LastEditTime: 2023-06-19 17:01:56
  5. * @Description: 头部注释
  6. * @FilePath: \own-vue3-vuecli-template\src\permission.js
  7. */
  8. import NProgress from "nprogress";
  9. import 'nprogress/nprogress.css'
  10. import router from "./router";
  11. import store from "./store";
  12. import { ElMessage } from "element-plus";
  13. import { getToken } from "./utils/auth";
  14. import { isRelogin } from "./utils/request";
  15. NProgress.configure({ showSpinner: false }) // 关闭加载微调器
  16. const whiteList = ['/login','/404','/layout', '/home'] // 设置白名单,用于任何人可访问
  17. // 钩子函数
  18. router.beforeEach((to, from, next) => {
  19. NProgress.start()
  20. let token = getToken()
  21. if (token) {
  22. if(to.path==='/login'){
  23. return next()
  24. }else{
  25. if(!token){
  26. ElMessage.error('登录已失效,请重新登录');
  27. return next('/login')
  28. } else if (to.path==='/') {
  29. return next('/login')
  30. } else{
  31. next()
  32. }
  33. }
  34. } else {
  35. // 没有token
  36. // if (whiteList.indexOf(to.path) !== -1) {
  37. // // 在免登录白名单,直接进入
  38. // next()
  39. // } else {
  40. // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  41. // NProgress.done()
  42. // }
  43. next()
  44. }
  45. })
  46. router.afterEach(() => {
  47. NProgress.done()
  48. })