动态菜单和动态路由的 antd pro

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import moment from 'moment';
  2. export function fixedZero(val) {
  3. return val * 1 < 10 ? `0${val}` : val;
  4. }
  5. export function getTimeDistance(type) {
  6. const now = new Date();
  7. const oneDay = 1000 * 60 * 60 * 24;
  8. if (type === 'today') {
  9. now.setHours(0);
  10. now.setMinutes(0);
  11. now.setSeconds(0);
  12. return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  13. }
  14. if (type === 'week') {
  15. let day = now.getDay();
  16. now.setHours(0);
  17. now.setMinutes(0);
  18. now.setSeconds(0);
  19. if (day === 0) {
  20. day = 6;
  21. } else {
  22. day -= 1;
  23. }
  24. const beginTime = now.getTime() - day * oneDay;
  25. return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  26. }
  27. if (type === 'month') {
  28. const year = now.getFullYear();
  29. const month = now.getMonth();
  30. const nextDate = moment(now).add(1, 'months');
  31. const nextYear = nextDate.year();
  32. const nextMonth = nextDate.month();
  33. return [
  34. moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
  35. moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
  36. ];
  37. }
  38. if (type === 'year') {
  39. const year = now.getFullYear();
  40. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  41. }
  42. }
  43. export function getPlainNode(nodeList, parentPath = '') {
  44. const arr = [];
  45. nodeList.forEach(node => {
  46. const item = node;
  47. item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
  48. item.exact = true;
  49. if (item.children && !item.component) {
  50. arr.push(...getPlainNode(item.children, item.path));
  51. } else {
  52. if (item.children && item.component) {
  53. item.exact = false;
  54. }
  55. arr.push(item);
  56. }
  57. });
  58. return arr;
  59. }
  60. export function digitUppercase(n) {
  61. const fraction = ['角', '分'];
  62. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  63. const unit = [['元', '万', '亿'], ['', '拾', '佰', '仟']];
  64. let num = Math.abs(n);
  65. let s = '';
  66. fraction.forEach((item, index) => {
  67. s += (digit[Math.floor(num * 10 * 10 ** index) % 10] + item).replace(/零./, '');
  68. });
  69. s = s || '整';
  70. num = Math.floor(num);
  71. for (let i = 0; i < unit[0].length && num > 0; i += 1) {
  72. let p = '';
  73. for (let j = 0; j < unit[1].length && num > 0; j += 1) {
  74. p = digit[num % 10] + unit[1][j] + p;
  75. num = Math.floor(num / 10);
  76. }
  77. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
  78. }
  79. return s
  80. .replace(/(零.)*零元/, '元')
  81. .replace(/(零.)+/g, '零')
  82. .replace(/^整$/, '零元整');
  83. }
  84. function getRelation(str1, str2) {
  85. if (str1 === str2) {
  86. console.warn('Two path are equal!'); // eslint-disable-line
  87. }
  88. const arr1 = str1.split('/');
  89. const arr2 = str2.split('/');
  90. if (arr2.every((item, index) => item === arr1[index])) {
  91. return 1;
  92. } else if (arr1.every((item, index) => item === arr2[index])) {
  93. return 2;
  94. }
  95. return 3;
  96. }
  97. function getRenderArr(routes) {
  98. let renderArr = [];
  99. renderArr.push(routes[0]);
  100. for (let i = 1; i < routes.length; i += 1) {
  101. let isAdd = false;
  102. // 是否包含
  103. isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
  104. // 去重
  105. renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
  106. if (isAdd) {
  107. renderArr.push(routes[i]);
  108. }
  109. }
  110. return renderArr;
  111. }
  112. /**
  113. * Get router routing configuration
  114. * { path:{name,...param}}=>Array<{name,path ...param}>
  115. * @param {string} path
  116. * @param {routerData} routerData
  117. */
  118. export function getRoutes(path, routerData) {
  119. let routes = Object.keys(routerData).filter(
  120. routePath => routePath.indexOf(path) === 0 && routePath !== path
  121. );
  122. // Replace path to '' eg. path='user' /user/name => name
  123. routes = routes.map(item => item.replace(path, ''));
  124. // Get the route to be rendered to remove the deep rendering
  125. const renderArr = getRenderArr(routes);
  126. // Conversion and stitching parameters
  127. const renderRoutes = renderArr.map(item => {
  128. const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
  129. return {
  130. exact,
  131. ...routerData[`${path}${item}`],
  132. key: `${path}${item}`,
  133. path: `${path}${item}`,
  134. };
  135. });
  136. return renderRoutes;
  137. }
  138. /* eslint no-useless-escape:0 */
  139. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g;
  140. export function isUrl(path) {
  141. return reg.test(path);
  142. }