utils.js 5.3 KB

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