家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import Vue from 'vue';
  2. import iView from 'iview';
  3. import Util from '../libs/util';
  4. import VueRouter from 'vue-router';
  5. import Cookies from 'js-cookie';
  6. import {routers, otherRouter, appRouter} from './router';
  7. Vue.use(VueRouter);
  8. // 路由配置
  9. const RouterConfig = {
  10. // mode: 'history',
  11. routes: routers
  12. };
  13. export const router = new VueRouter(RouterConfig);
  14. router.beforeEach((to, from, next) => {
  15. iView.LoadingBar.start();
  16. Util.title(to.meta.title);
  17. if (Cookies.get('locking') === '1' && to.name !== 'locking') { // 判断当前是否是锁定状态
  18. next({
  19. replace: true,
  20. name: 'locking'
  21. });
  22. } else if (Cookies.get('locking') === '0' && to.name === 'locking') {
  23. next(false);
  24. } else {
  25. if (!Cookies.get('user') && to.name !== 'login') { // 判断是否已经登录且前往的页面不是登录页
  26. next({
  27. name: 'login'
  28. });
  29. } else if (Cookies.get('user') && to.name === 'login') { // 判断是否已经登录且前往的是登录页
  30. Util.title();
  31. next({
  32. name: 'home_index'
  33. });
  34. } else {
  35. const curRouterObj = Util.getRouterObjByName([otherRouter, ...appRouter], to.name);
  36. if (curRouterObj && curRouterObj.access !== undefined) { // 需要判断权限的路由
  37. if (Util.oneOf(parseInt(Cookies.get('access')), curRouterObj.access)) {
  38. Util.toDefaultPage([otherRouter, ...appRouter], to.name, router, next); // 如果在地址栏输入的是一级菜单则默认打开其第一个二级菜单的页面
  39. } else {
  40. next({
  41. replace: true,
  42. name: 'error-403'
  43. });
  44. }
  45. } else { // 没有配置权限的路由, 直接通过
  46. Util.toDefaultPage([...routers], to.name, router, next);
  47. }
  48. }
  49. }
  50. });
  51. router.afterEach((to) => {
  52. Util.openNewPage(router.app, to.name, to.params, to.query);
  53. iView.LoadingBar.finish();
  54. window.scrollTo(0, 0);
  55. });