utils.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. function accMul(arg1, arg2) {
  61. let m = 0;
  62. const s1 = arg1.toString();
  63. const s2 = arg2.toString();
  64. m += s1.split('.').length > 1 ? s1.split('.')[1].length : 0;
  65. m += s2.split('.').length > 1 ? s2.split('.')[1].length : 0;
  66. return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / 10 ** m;
  67. }
  68. export function digitUppercase(n) {
  69. const fraction = ['角', '分'];
  70. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  71. const unit = [['元', '万', '亿'], ['', '拾', '佰', '仟', '万']];
  72. let num = Math.abs(n);
  73. let s = '';
  74. fraction.forEach((item, index) => {
  75. s += (digit[Math.floor(accMul(num, 10 * 10 ** index)) % 10] + item).replace(/零./, '');
  76. });
  77. s = s || '整';
  78. num = Math.floor(num);
  79. for (let i = 0; i < unit[0].length && num > 0; i += 1) {
  80. let p = '';
  81. for (let j = 0; j < unit[1].length && num > 0; j += 1) {
  82. p = digit[num % 10] + unit[1][j] + p;
  83. num = Math.floor(num / 10);
  84. }
  85. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
  86. }
  87. return s
  88. .replace(/(零.)*零元/, '元')
  89. .replace(/(零.)+/g, '零')
  90. .replace(/^整$/, '零元整');
  91. }
  92. function getRelation(str1, str2) {
  93. if (str1 === str2) {
  94. console.warn('Two path are equal!'); // eslint-disable-line
  95. }
  96. const arr1 = str1.split('/');
  97. const arr2 = str2.split('/');
  98. if (arr2.every((item, index) => item === arr1[index])) {
  99. return 1;
  100. } else if (arr1.every((item, index) => item === arr2[index])) {
  101. return 2;
  102. }
  103. return 3;
  104. }
  105. function getRenderArr(routes) {
  106. let renderArr = [];
  107. renderArr.push(routes[0]);
  108. for (let i = 1; i < routes.length; i += 1) {
  109. // 去重
  110. renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
  111. // 是否包含
  112. const isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
  113. if (isAdd) {
  114. renderArr.push(routes[i]);
  115. }
  116. }
  117. return renderArr;
  118. }
  119. /**
  120. * Get router routing configuration
  121. * { path:{name,...param}}=>Array<{name,path ...param}>
  122. * @param {string} path
  123. * @param {routerData} routerData
  124. */
  125. export function getRoutes(path, routerData) {
  126. let routes = Object.keys(routerData).filter(
  127. routePath => routePath.indexOf(path) === 0 && routePath !== path
  128. );
  129. // Replace path to '' eg. path='user' /user/name => name
  130. routes = routes.map(item => item.replace(path, ''));
  131. // Get the route to be rendered to remove the deep rendering
  132. const renderArr = getRenderArr(routes);
  133. // Conversion and stitching parameters
  134. const renderRoutes = renderArr.map(item => {
  135. const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
  136. return {
  137. exact,
  138. ...routerData[`${path}${item}`],
  139. key: `${path}${item}`,
  140. path: `${path}${item}`,
  141. };
  142. });
  143. return renderRoutes;
  144. }
  145. /* eslint no-useless-escape:0 */
  146. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  147. export function isUrl(path) {
  148. return reg.test(path);
  149. }