index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. import Vue from "vue";
  11. import VueRouter from "vue-router";
  12. import iView from "view-design";
  13. import util from "@/libs/util";
  14. import Setting from "@/setting";
  15. import store from "@/store/index";
  16. // 路由数据
  17. import routes from "./routes";
  18. import { includeArray } from "@/libs/system";
  19. Vue.use(VueRouter);
  20. /**
  21. * 重写路由的push方法
  22. */
  23. const routerPush = VueRouter.prototype.push;
  24. VueRouter.prototype.push = function push(location) {
  25. return routerPush.call(this, location).catch((error) => error);
  26. };
  27. // 导出路由 在 main.js 里使用
  28. const router = new VueRouter({
  29. routes,
  30. mode: Setting.routerMode,
  31. });
  32. /**
  33. * 路由拦截
  34. * 权限验证
  35. */
  36. router.beforeEach(async (to, from, next) => {
  37. if (to.fullPath.indexOf(`${Setting.routePreKF}`) != -1) {
  38. return next();
  39. }
  40. // if (Setting.showProgressBar) iView.LoadingBar.start();
  41. // // 判断是否需要登录才可以进入
  42. if (to.matched.some((_) => _.meta.auth)) {
  43. // 这里依据 token 判断是否登录,可视情况修改
  44. const db = await store.dispatch("admin/db/database", {
  45. user: true,
  46. });
  47. const token = util.cookies.get("token");
  48. if (token && token !== "undefined") {
  49. const access = db.get("unique_auth").value();
  50. const isPermission = includeArray(to.meta.auth, access);
  51. if (isPermission) {
  52. next();
  53. } else {
  54. next({
  55. name: "403",
  56. });
  57. }
  58. // next();
  59. } else {
  60. store.dispatch("admin/db/databaseClear", {
  61. user: true,
  62. });
  63. // 没有登录的时候跳转到登录界面
  64. // 携带上登陆成功之后需要跳转的页面完整路径
  65. next({
  66. name: "login",
  67. query: {
  68. redirect: to.fullPath,
  69. },
  70. });
  71. }
  72. } else {
  73. // 不需要身份校验 直接通过
  74. if (store.state.admin.menus.menusName.length || to.path === `${Setting.roterPre}/login` || to.path === '/app/upload') {
  75. next();
  76. } else {
  77. store.dispatch("admin/db/databaseClear", {
  78. user: true
  79. });
  80. next({
  81. name: "login",
  82. query: {
  83. redirect: to.fullPath.replace(localStorage.getItem('roterPre'), Setting.roterPre)
  84. }
  85. });
  86. }
  87. }
  88. // next();
  89. });
  90. router.afterEach((to) => {
  91. // if (Setting.showProgressBar) iView.LoadingBar.finish();
  92. // 多页控制 打开新的页面
  93. store.dispatch("admin/page/open", to);
  94. // 更改标题
  95. util.title({
  96. title: to.meta.title,
  97. });
  98. // 返回页面顶端
  99. window.scrollTo(0, 0);
  100. });
  101. export default router;