routerBefore.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // 路由前置操作
  2. import store from '@/store/index.js';
  3. import {
  4. Message
  5. } from 'element-ui';
  6. import {
  7. getCookie,
  8. setCookie,
  9. dgTree
  10. } from '@/utils/util.js';
  11. // 免登录白名单
  12. const whiteList = ['/', '/login'];
  13. /**
  14. * 当前路由取标题
  15. * @param {Object} to 当前路由
  16. * @param {Array} constantRouterMap 路由map
  17. * @param {Array} menu 菜单树
  18. * @returns {String} 标题
  19. */
  20. const titleFn = function (to, menu) {
  21. let title = '';
  22. if (to.meta && to.meta.title) {
  23. return to.meta.title;
  24. }
  25. //
  26. if (to.meta && to.meta.code) {
  27. // 查找标题
  28. let code = to.meta.code;
  29. dgTree(menu, '', item => {
  30. if (item.menuCode === code) {
  31. title = item.menuLabel;
  32. }
  33. });
  34. }
  35. return title;
  36. }
  37. /**
  38. * matched 添加标题名称
  39. * @param {Array} matched
  40. * @param {Array} menu 菜单树
  41. * @returns {Array} matched
  42. */
  43. const breadcrumbFn = function (to, menu) {
  44. let code = to.meta.code;
  45. let list = [];
  46. let item = null;
  47. let data = [];
  48. let getPid = function (pId) {
  49. for (let ii of list) {
  50. if (ii.menuId === pId) {
  51. return ii;
  52. }
  53. }
  54. return null;
  55. }
  56. dgTree(menu, '', leaf => {
  57. list.push(leaf);
  58. if (leaf.menuCode === code) {
  59. item = leaf;
  60. }
  61. });
  62. let init = function (pId) {
  63. let itemPid = getPid(pId);
  64. if (itemPid) {
  65. data.push(itemPid);
  66. init(itemPid.pId);
  67. }
  68. }
  69. //
  70. if (item) {
  71. data.push(item);
  72. init(item.pId);
  73. }
  74. if (data.length > 1) {
  75. return data.reverse();
  76. }
  77. //
  78. to.matched.forEach(item => {
  79. dgTree(menu, '', leaf => {
  80. if (item.meta && item.meta.code === leaf.menuCode) {
  81. data.push(leaf);
  82. }
  83. });
  84. });
  85. return data;
  86. }
  87. // 路由前置操作
  88. const routerBefore = function (router, constantRouterMap) {
  89. let flag = false
  90. router.beforeEach((to, from, next) => {
  91. // 面包屑
  92. console.log('进入路由', to.name)
  93. store.commit('breadcrumb', breadcrumbFn(to, store.state.routes));
  94. // 标题
  95. if (to.meta && !to.meta.title) {
  96. document.title = titleFn(to, store.state.routes) + ' ' + document.title.substr(document.title.indexOf('-'));
  97. } else {
  98. document.title = to.meta.title + ' ' + document.title.substr(document.title.indexOf('-'));
  99. }
  100. //
  101. if (getCookie('accessToken')) { // 判断是否有token
  102. if (window.top.document.URL === window.document.URL && !window.top.localStorage.getItem('ownPrivilege')) {
  103. store.dispatch('getOwnMenuUrl');
  104. }
  105. if (to.path === '/login') {
  106. next({
  107. path: '/'
  108. });
  109. } else {
  110. if (window.top.document.URL === window.document.URL && store.state.userInfo === null) { // 判断当前用户是否已拉取完路由信息
  111. // 前期无后台测试用
  112. // next();
  113. // /*
  114. store.dispatch('getUserInfo').then(res => { // 拉取info
  115. if (res.code === '0') {
  116. const userInfo = res.data;
  117. store.dispatch('generateRoutes', {
  118. userInfo
  119. }).then(() => { // 生成可访问的路由表
  120. // router.addRoutes(store.state.routes) // 动态添加可访问路由表
  121. if (flag) {
  122. next()
  123. } else {
  124. next({
  125. ...to,
  126. replace: true
  127. })
  128. flag = true
  129. }
  130. // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
  131. console.log('这是个什么东西', {
  132. ...to,
  133. replace: true
  134. })
  135. })
  136. } else {
  137. Message('登录已过期,请重新登录');
  138. // next('/login'); // 否则全部重定向到登录页
  139. // window.location.href = '/views/index.html';
  140. setCookie('accessToken', '', -1, '/');
  141. setCookie('refreshToken', '', -1, '/');
  142. setCookie('workDate', '', -1);
  143. window.top.location.href = store.getters.ctx;
  144. }
  145. }).catch(err => {
  146. console.log(err);
  147. Message('登录已过期,请重新登录');
  148. // next('/login'); // 否则全部重定向到登录页
  149. // window.location.href = '/views/index.html';
  150. setCookie('accessToken', '', -1, '/');
  151. setCookie('refreshToken', '', -1, '/');
  152. setCookie('workDate', '', -1);
  153. window.top.location.href = store.getters.ctx;
  154. });
  155. // */
  156. } else {
  157. next() // 当有用户权限的时候,说明所有可访问路由已生成 如访问没权限的全面会自动进入404页面
  158. }
  159. }
  160. } else {
  161. if (whiteList.indexOf(to.path) !== -1) { // 在免登录白名单,直接进入
  162. next();
  163. } else {
  164. Message('未登录');
  165. // next('/login'); // 否则全部重定向到登录页
  166. // window.location.href = '/views/index.html';
  167. setCookie('accessToken', '', -1, '/');
  168. setCookie('refreshToken', '', -1, '/');
  169. setCookie('workDate', '', -1);
  170. window.top.location.href = store.getters.ctx;
  171. }
  172. }
  173. });
  174. }
  175. export default routerBefore;