123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import router from './router';
- // import systemSetting from './router/modules/systemSetting';
- import store from '@/vendors/vuex'
- import NProgress from 'nprogress';
- import 'nprogress/nprogress.css';
- import {
- getToken
- } from './utils/auth';
- NProgress.configure({
- showSpinner: false
- });
- const whiteList = ['/login', '/ship_login', '/userAgreement', '/privacyAgreement', '/about', 'lab', '/contractManagement'];
- router.beforeEach(async (to, _, next) => {
- NProgress.start();
- // 用户刷新页面,localStorage还在,但vuex.store里的东西会reset
- const hasToken = getToken();
- console.log(hasToken)
- if (hasToken) {
- if (whiteList.includes(to.path)) {
- next({
- path: '/'
- });
- NProgress.done();
- } else {
- console.log(store.getters.token)
- if (!store.getters.token) {
- console.log(to)
- // 首次以及用户手动刷新,token都会还原回空字符串
- const accessRoutes = await store.dispatch('permission/generateRoutes');
- router.addRoutes(accessRoutes);
- // router.addRoutes([systemSetting]);
- // router.addRoutes([{
- // path: '*',
- // redirect: '/404',
- // meta: {},
- // hidden: true
- // }]);
- store.commit('user/SET_TOKEN', 'admin'); // 之所以放到这儿是因为想让store.getters.toke成为上面的标识
- next({
- ...to,
- replace: true
- });
- const value = to.query.src || to.fullPath;
- const label = to.query.name || to.name;
- const meta = to.meta || {};
- const i18n = to.query.i18n;
- if (
- meta.isTab !== false
- ) {
- store.commit('ADD_TAG', {
- label: label,
- value: value,
- params: to.params,
- query: to.query,
- meta: (() => {
- if (!i18n) {
- return meta;
- }
- return {
- i18n: i18n
- };
- })(),
- group: to.group || []
- });
- }
- next();
- } else {
- if (!whiteList.includes(to.path)) {
- checkLoginUser();
- }
- next();
- }
- }
- } else {
- if (whiteList.indexOf(to.path) !== -1) {
- if (!whiteList.includes(to.path)) {
- checkLoginUser();
- }
- next();
- } else {
- next(getLoginOutUrl() + `?redirect=${to.path}`);
- NProgress.done();
- }
- }
- });
- router.afterEach(() => {
- NProgress.done();
- });
- function checkLoginUser() {
- if (location.port) {
- return;
- }
- }
- function getLoginOutUrl() {
- if (process.env.VUE_APP_PACKAGE_ENV === 'ship') {
- return '/ship_login';
- }
- return '/login';
- }
|