xiangpei
2025-05-13 4c4995771ce83925a2d69dedc11c4404d9b77875
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import Vue from 'vue';
import ViewUI from 'view-design';
import Util from '../libs/util';
import VueRouter from 'vue-router';
import Cookies from 'js-cookie';
import { routers } from './router';
 
Vue.use(VueRouter);
 
// 路由配置
const RouterConfig = {
  mode: 'history',
  routes: routers
};
 
/**
 * 解决重复点击菜单会控制台报错bug
 */
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}
 
export const router = new VueRouter(RouterConfig);
 
router.beforeEach((to, from, next) => {
  ViewUI.LoadingBar.start();
  Util.title(to.meta.title);
 
  next();
 
  const name = to.name;
 
  if (!Cookies.get('userInfoSeller') && name !== 'login') {
    if (name === 'forgetPassword') {
      console.log(name)
      Util.toDefaultPage([...routers], name, router, next);
    } else {
      // 判断是否已经登录且前往的页面不是登录页
      next({
        name: 'login'
      });
    }
  } else if (Cookies.get('userInfoSeller') && name === 'login') {
    // 判断是否已经登录且前往的是登录页
    Util.title();
    next({
      name: 'home_index'
    });
  } else {
    Util.toDefaultPage([...routers], name, router, next);
  }
});
 
router.afterEach((to) => {
  Util.openNewPage(router.app, to.name, to.params, to.query);
  ViewUI.LoadingBar.finish();
  window.scrollTo(0, 0);
});