permission.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. document.title = "发电量管控平台";
  20. NProgress.start()
  21. let token = getToken()
  22. if (token) {
  23. if(to.path==='/login'){
  24. return next()
  25. }else{
  26. if(!token){
  27. ElMessage.error('登录已失效,请重新登录');
  28. return next('/login')
  29. } else if (to.path==='/') {
  30. return next('/login')
  31. } else{
  32. next()
  33. }
  34. }
  35. } else {
  36. // 没有token
  37. // if (whiteList.indexOf(to.path) !== -1) {
  38. // // 在免登录白名单,直接进入
  39. // next()
  40. // } else {
  41. // next(`/login?redirect=${to.fullPath}`) // 否则全部重定向到登录页
  42. // NProgress.done()
  43. // }
  44. next()
  45. }
  46. })
  47. router.afterEach(() => {
  48. NProgress.done()
  49. })