index.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * @Author: From-wh from-wh@hotmail.com
  3. * @Date: 2023-03-04 11:49:55
  4. * @FilePath: /admin/src/router/index.js
  5. * @Description:
  6. *
  7. */
  8. // +----------------------------------------------------------------------
  9. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  10. // +----------------------------------------------------------------------
  11. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  12. // +----------------------------------------------------------------------
  13. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  14. // +----------------------------------------------------------------------
  15. // | Author: CRMEB Team <admin@crmeb.com>
  16. // +----------------------------------------------------------------------
  17. import Vue from 'vue';
  18. import Router from 'vue-router';
  19. import routes from './routers';
  20. import Setting from '@/setting';
  21. import store from '@/store';
  22. import iView from 'iview';
  23. import { removeCookies, getCookies, setTitle } from '@/libs/util';
  24. import { includeArray } from '@/libs/auth';
  25. import { PrevLoading } from '@/utils/loading.js';
  26. Vue.use(Router);
  27. // 解决 `element ui` 导航栏重复点菜单报错问题
  28. const originalPush = Router.prototype.push;
  29. Router.prototype.push = function push(location) {
  30. return originalPush.call(this, location).catch((err) => err);
  31. };
  32. const router = new Router({
  33. routes,
  34. mode: Setting.routerMode,
  35. });
  36. // 判断路由 meta.roles 中是否包含当前登录用户权限字段
  37. export function hasAuth(roles, route) {
  38. if (route.meta && route.meta.auth) return roles.some((role) => route.meta.auth.includes(role));
  39. else return true;
  40. }
  41. // 递归过滤有权限的路由
  42. export function setFilterMenuFun(routes, role) {
  43. const menu = [];
  44. routes.forEach((route) => {
  45. const item = { ...route };
  46. if (hasAuth(role, item)) {
  47. if (item.children) item.children = setFilterMenuFun(item.children, role);
  48. menu.push(item);
  49. }
  50. });
  51. return menu;
  52. }
  53. // 递归处理多余的 layout : <router-view>,让需要访问的组件保持在第一层 layout 层。
  54. // 因为 `keep-alive` 只能缓存二级路由
  55. // 默认初始化时就执行
  56. export function keepAliveSplice(to) {
  57. if (to.matched && to.matched.length > 2) {
  58. to.matched.map((v, k) => {
  59. if (v.components.default instanceof Function) {
  60. v.components.default().then((components) => {
  61. if (components.default.name === 'parent') {
  62. to.matched.splice(k, 1);
  63. router.push({ path: to.path, query: to.query });
  64. keepAliveSplice(to);
  65. }
  66. });
  67. } else {
  68. if (v.components.default.name === 'parent') {
  69. to.matched.splice(k, 1);
  70. keepAliveSplice(to);
  71. }
  72. }
  73. });
  74. }
  75. }
  76. // 延迟关闭进度条
  77. export function delayNProgressDone(time = 300) {
  78. setTimeout(() => {
  79. NProgress.done();
  80. }, time);
  81. }
  82. /**
  83. * 路由拦截
  84. * 权限验证
  85. */
  86. router.beforeEach(async (to, from, next) => {
  87. // PrevLoading.start();
  88. keepAliveSplice(to);
  89. if (to.fullPath.indexOf('kefu') != -1) {
  90. return next();
  91. }
  92. // if (Setting.showProgressBar) iView.LoadingBar.start()
  93. // 判断是否需要登录才可以进入
  94. if (to.matched.some((_) => _.meta.auth)) {
  95. // 这里依据 token 判断是否登录,可视情况修改
  96. const token = getCookies('token');
  97. if (token && token !== 'undefined') {
  98. const access = store.state.userInfo.uniqueAuth;
  99. const isPermission = includeArray(to.meta.auth, access);
  100. if (isPermission) {
  101. next();
  102. } else {
  103. if (access.length == 0) {
  104. next({
  105. name: 'login',
  106. query: {
  107. redirect: to.fullPath,
  108. },
  109. });
  110. localStorage.clear();
  111. removeCookies('token');
  112. removeCookies('expires_time');
  113. removeCookies('uuid');
  114. } else {
  115. next({
  116. name: '403',
  117. });
  118. }
  119. }
  120. // next();
  121. } else {
  122. // 没有登录的时候跳转到登录界面
  123. // 携带上登陆成功之后需要跳转的页面完整路径
  124. next({
  125. name: 'login',
  126. query: {
  127. redirect: to.fullPath,
  128. },
  129. });
  130. localStorage.clear();
  131. removeCookies('token');
  132. removeCookies('expires_time');
  133. removeCookies('uuid');
  134. }
  135. } else {
  136. // 不需要身份校验 直接通过
  137. next();
  138. }
  139. });
  140. router.afterEach((to) => {
  141. // if (Setting.showProgressBar) iView.LoadingBar.finish()
  142. // 更改标题
  143. setTitle(to, router.app);
  144. // 返回页面顶端
  145. window.scrollTo(0, 0);
  146. PrevLoading.done();
  147. });
  148. export default router;