permission.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import router from './router';
  2. // import systemSetting from './router/modules/systemSetting';
  3. import store from '@/vendors/vuex'
  4. import NProgress from 'nprogress';
  5. import 'nprogress/nprogress.css';
  6. import {
  7. getToken
  8. } from './utils/auth';
  9. NProgress.configure({
  10. showSpinner: false
  11. });
  12. const whiteList = ['/login', '/ship_login', '/userAgreement', '/privacyAgreement', '/about', 'lab', '/contractManagement'];
  13. router.beforeEach(async (to, _, next) => {
  14. NProgress.start();
  15. // 用户刷新页面,sessionStorage还在,但vuex.store里的东西会reset
  16. const hasToken = getToken();
  17. if (hasToken) {
  18. if (whiteList.includes(to.path)) {
  19. next({
  20. path: '/'
  21. });
  22. NProgress.done();
  23. } else {
  24. if (!store.getters.token) {
  25. // 首次以及用户手动刷新,token都会还原回空字符串
  26. const accessRoutes = await store.dispatch('permission/generateRoutes');
  27. router.addRoutes(accessRoutes);
  28. // router.addRoutes([systemSetting]);
  29. // router.addRoutes([{
  30. // path: '*',
  31. // redirect: '/404',
  32. // meta: {},
  33. // hidden: true
  34. // }]);
  35. store.commit('user/SET_TOKEN', 'admin'); // 之所以放到这儿是因为想让store.getters.toke成为上面的标识
  36. next({
  37. ...to,
  38. replace: true
  39. });
  40. const value = to.query.src || to.fullPath;
  41. const label = to.query.name || to.name;
  42. const meta = to.meta || {};
  43. const i18n = to.query.i18n;
  44. if (
  45. meta.isTab !== false
  46. ) {
  47. store.commit('ADD_TAG', {
  48. label: label,
  49. value: value,
  50. params: to.params,
  51. query: to.query,
  52. meta: (() => {
  53. if (!i18n) {
  54. return meta;
  55. }
  56. return {
  57. i18n: i18n
  58. };
  59. })(),
  60. group: to.group || []
  61. });
  62. }
  63. next();
  64. } else {
  65. if (!whiteList.includes(to.path)) {
  66. checkLoginUser();
  67. }
  68. next();
  69. }
  70. }
  71. } else {
  72. if (whiteList.indexOf(to.path) !== -1) {
  73. if (!whiteList.includes(to.path)) {
  74. checkLoginUser();
  75. }
  76. next();
  77. } else {
  78. next(getLoginOutUrl() + `?redirect=${to.path}`);
  79. NProgress.done();
  80. }
  81. }
  82. });
  83. router.afterEach(() => {
  84. NProgress.done();
  85. });
  86. function checkLoginUser() {
  87. if (location.port) {
  88. return;
  89. }
  90. }
  91. function getLoginOutUrl() {
  92. if (process.env.VUE_APP_PACKAGE_ENV === 'ship') {
  93. return '/ship_login';
  94. }
  95. return '/login';
  96. }