index.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * 系统内置方法集,正常情况下您不应该修改或移除此文件
  3. * */
  4. import { cloneDeep } from 'lodash';
  5. /**
  6. * @description 根据当前路由,找打顶部菜单名称
  7. * @param {String} currentPath 当前路径
  8. * @param {Array} menuList 所有路径
  9. * */
  10. function getHeaderName(to, menuList) {
  11. const allMenus = [];
  12. menuList.forEach((menu) => {
  13. const headerName = menu.path || '';
  14. const menus = transferMenu(menu, headerName);
  15. allMenus.push({
  16. path: menu.path,
  17. header: headerName,
  18. });
  19. menus.forEach((item) => allMenus.push(item));
  20. });
  21. const currentMenu = allMenus.find((item) => {
  22. if (item.path === to.path) {
  23. return true;
  24. } else {
  25. return to.path === getPath(to, item.path);
  26. }
  27. });
  28. return currentMenu ? currentMenu.header : null;
  29. }
  30. function getPath(to, path) {
  31. let params = [];
  32. let query = [];
  33. Object.keys(to.params).forEach((item) => {
  34. params.push(to.params[item]);
  35. });
  36. Object.keys(to.query).forEach((item) => {
  37. query.push(item + '=' + to.query[item]);
  38. });
  39. return path + (params.length ? '/' + params.join('/') : '') + (query.length ? '?' + query.join('&') : '');
  40. }
  41. function transferMenu(menu, headerName) {
  42. if (menu.children && menu.children.length) {
  43. return menu.children.reduce((all, item) => {
  44. all.push({
  45. path: item.path,
  46. header: headerName,
  47. });
  48. const foundChildren = transferMenu(item, headerName);
  49. return all.concat(foundChildren);
  50. }, []);
  51. } else {
  52. return [menu];
  53. }
  54. }
  55. export { getHeaderName };
  56. /**
  57. * @description 根据当前路由,找打顶部菜单名称
  58. * @param {String} currentPath 当前路径
  59. * @param {Array} menuList 所有路径
  60. * */
  61. function getHeaderSider(menuList) {
  62. return menuList.filter((item) => item.pid === 0);
  63. }
  64. export { getHeaderSider };
  65. /**
  66. * @description 根据当前路由,找以及菜单名称
  67. * @param {String} currentPath 当前路径
  68. * @param {Array} menuList 所有路径
  69. * */
  70. function getOneHeaderName(menuList, path) {
  71. return menuList.filter((item) => item.path === path);
  72. }
  73. export { getOneHeaderName };
  74. /**
  75. * @description 根据当前顶栏菜单 name,找到对应的二级菜单
  76. * @param {Array} menuList 所有的二级菜单
  77. * @param {String} headerName 当前顶栏菜单的 name
  78. * */
  79. function getMenuSider(menuList, headerName = '') {
  80. if (headerName) {
  81. return menuList.filter((item) => item.path === headerName);
  82. } else {
  83. return menuList;
  84. }
  85. }
  86. export { getMenuSider };
  87. /**
  88. * @description 根据当前路由,找到其所有父菜单 path,作为展开侧边栏 open-names 依据
  89. * @param {String} currentPath 当前路径
  90. * @param {Array} menuList 所有路径
  91. * */
  92. // function getSiderSubmenu (currentPath, menuList) {
  93. // const allMenus = [];
  94. // menuList.forEach(menu => {
  95. // const menus = transferSubMenu(menu, []);
  96. // allMenus.push({
  97. // path: menu.path,
  98. // openNames: []
  99. // });
  100. // menus.forEach(item => allMenus.push(item));
  101. // });
  102. // const currentMenu = allMenus.find(item => item.path === currentPath);
  103. // return currentMenu ? currentMenu.openNames : [];
  104. // }
  105. function getSiderSubmenu(to, menuList) {
  106. const allMenus = [];
  107. menuList.forEach((menu) => {
  108. const menus = transferSubMenu(menu, []);
  109. allMenus.push({
  110. path: menu.path,
  111. openNames: [],
  112. });
  113. menus.forEach((item) => allMenus.push(item));
  114. });
  115. const currentMenu = allMenus.find((item) => {
  116. if (item.openNames.length) {
  117. return item.path === to.path || to.path === getPath(to, item.path);
  118. }
  119. });
  120. return currentMenu ? currentMenu.openNames : [];
  121. }
  122. function transferSubMenu(menu, openNames) {
  123. if (menu.children && menu.children.length) {
  124. const itemOpenNames = openNames.concat([menu.path]);
  125. return menu.children.reduce((all, item) => {
  126. all.push({
  127. path: item.path,
  128. openNames: itemOpenNames,
  129. });
  130. const foundChildren = transferSubMenu(item, itemOpenNames);
  131. return all.concat(foundChildren);
  132. }, []);
  133. } else {
  134. return [menu].map((item) => {
  135. return {
  136. path: item.path,
  137. openNames: openNames,
  138. };
  139. });
  140. }
  141. }
  142. export { getSiderSubmenu };
  143. /**
  144. * @description 递归获取所有子菜单
  145. * */
  146. function getAllSiderMenu(menuList) {
  147. let allMenus = [];
  148. menuList.forEach((menu) => {
  149. if (menu.children && menu.children.length) {
  150. const menus = getMenuChildren(menu);
  151. menus.forEach((item) => allMenus.push(item));
  152. } else {
  153. allMenus.push(menu);
  154. }
  155. });
  156. return allMenus;
  157. }
  158. function getMenuChildren(menu) {
  159. if (menu.children && menu.children.length) {
  160. return menu.children.reduce((all, item) => {
  161. const foundChildren = getMenuChildren(item);
  162. return all.concat(foundChildren);
  163. }, []);
  164. } else {
  165. return [menu];
  166. }
  167. }
  168. export { getAllSiderMenu };
  169. /**
  170. * @description 将菜单转为平级
  171. * */
  172. function flattenSiderMenu(menuList, newList) {
  173. menuList.forEach((menu) => {
  174. let newMenu = {};
  175. for (let i in menu) {
  176. if (i !== 'children') newMenu[i] = cloneDeep(menu[i]);
  177. }
  178. newList.push(newMenu);
  179. menu.children && flattenSiderMenu(menu.children, newList);
  180. });
  181. return newList;
  182. }
  183. export { flattenSiderMenu };
  184. export const findFirstNonNullChildren = (arr) => {
  185. // 如果数组为空,返回null
  186. if (!arr || arr.length === 0) {
  187. return null;
  188. }
  189. // 找到第一个对象
  190. const firstObj = arr[0];
  191. // 如果第一个对象没有children属性,返回该对象
  192. if (!firstObj.children) {
  193. return firstObj;
  194. }
  195. // 如果第一个对象的children属性是数组,
  196. // 递归查找children属性中的第一个非null children属性
  197. if (Array.isArray(firstObj.children)) {
  198. return findFirstNonNullChildren(firstObj.children);
  199. }
  200. // 如果数组中没有非null children属性,返回null
  201. return null;
  202. };
  203. export const findFirstNonNullChildrenKeys = (obj, lastArr) => {
  204. let ids = lastArr;
  205. // 如果第一个对象没有children属性,返回该对象
  206. if (!obj.children) {
  207. ids.push(obj.id);
  208. return ids;
  209. }
  210. // 如果第一个对象的children属性是数组,
  211. // 递归查找children属性中的第一个非null children属性
  212. if (Array.isArray(obj.children)) {
  213. ids.push(obj.id);
  214. return findFirstNonNullChildrenKeys(obj.children[0], ids);
  215. }
  216. return ids;
  217. };
  218. // 多级嵌套数组处理成一维数组
  219. export const formatFlatteningRoutes = (arr) => {
  220. if (arr.length <= 0) return false;
  221. for (let i = 0; i < arr.length; i++) {
  222. if (arr[i].children) {
  223. arr = arr.slice(0, i + 1).concat(arr[i].children, arr.slice(i + 1));
  224. }
  225. }
  226. return arr;
  227. };
  228. /**
  229. * @description 判断列表1中是否包含了列表2中的某一项
  230. * 因为用户权限 access 为数组,includes 方法无法直接得出结论
  231. * */
  232. function includeArray(list1, list2) {
  233. let status = false;
  234. if (list1 === true) {
  235. return true;
  236. } else {
  237. if (typeof list2 !== 'object') {
  238. return false;
  239. }
  240. list2.forEach((item) => {
  241. if (list1.includes(item)) status = true;
  242. });
  243. return status;
  244. }
  245. }
  246. export { includeArray };