utils.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import moment from 'moment';
  2. import React from 'react';
  3. import nzh from 'nzh/cn';
  4. import { parse, stringify } from 'qs';
  5. export function fixedZero(val) {
  6. return val * 1 < 10 ? `0${val}` : val;
  7. }
  8. export function getTimeDistance(type) {
  9. const now = new Date();
  10. const oneDay = 1000 * 60 * 60 * 24;
  11. if (type === 'today') {
  12. now.setHours(0);
  13. now.setMinutes(0);
  14. now.setSeconds(0);
  15. return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  16. }
  17. if (type === 'week') {
  18. let day = now.getDay();
  19. now.setHours(0);
  20. now.setMinutes(0);
  21. now.setSeconds(0);
  22. if (day === 0) {
  23. day = 6;
  24. } else {
  25. day -= 1;
  26. }
  27. const beginTime = now.getTime() - day * oneDay;
  28. return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  29. }
  30. if (type === 'month') {
  31. const year = now.getFullYear();
  32. const month = now.getMonth();
  33. const nextDate = moment(now).add(1, 'months');
  34. const nextYear = nextDate.year();
  35. const nextMonth = nextDate.month();
  36. return [
  37. moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
  38. moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
  39. ];
  40. }
  41. const year = now.getFullYear();
  42. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  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. export function digitUppercase(n) {
  62. return nzh.toMoney(n);
  63. }
  64. function getRelation(str1, str2) {
  65. if (str1 === str2) {
  66. console.warn('Two path are equal!'); // eslint-disable-line
  67. }
  68. const arr1 = str1.split('/');
  69. const arr2 = str2.split('/');
  70. if (arr2.every((item, index) => item === arr1[index])) {
  71. return 1;
  72. }
  73. if (arr1.every((item, index) => item === arr2[index])) {
  74. return 2;
  75. }
  76. return 3;
  77. }
  78. function getRenderArr(routes) {
  79. let renderArr = [];
  80. renderArr.push(routes[0]);
  81. for (let i = 1; i < routes.length; i += 1) {
  82. // 去重
  83. renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
  84. // 是否包含
  85. const isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
  86. if (isAdd) {
  87. renderArr.push(routes[i]);
  88. }
  89. }
  90. return renderArr;
  91. }
  92. /**
  93. * Get router routing configuration
  94. * { path:{name,...param}}=>Array<{name,path ...param}>
  95. * @param {string} path
  96. * @param {routerData} routerData
  97. */
  98. export function getRoutes(path, routerData) {
  99. let routes = Object.keys(routerData).filter(
  100. routePath => routePath.indexOf(path) === 0 && routePath !== path
  101. );
  102. // Replace path to '' eg. path='user' /user/name => name
  103. routes = routes.map(item => item.replace(path, ''));
  104. // Get the route to be rendered to remove the deep rendering
  105. const renderArr = getRenderArr(routes);
  106. // Conversion and stitching parameters
  107. const renderRoutes = renderArr.map(item => {
  108. const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
  109. return {
  110. exact,
  111. ...routerData[`${path}${item}`],
  112. key: `${path}${item}`,
  113. path: `${path}${item}`,
  114. };
  115. });
  116. return renderRoutes;
  117. }
  118. export function getPageQuery() {
  119. return parse(window.location.href.split('?')[1]);
  120. }
  121. export function getQueryPath(path = '', query = {}) {
  122. const search = stringify(query);
  123. if (search.length) {
  124. return `${path}?${search}`;
  125. }
  126. return path;
  127. }
  128. /* eslint no-useless-escape:0 */
  129. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  130. export function isUrl(path) {
  131. return reg.test(path);
  132. }
  133. export function formatWan(val) {
  134. const v = val * 1;
  135. if (!v || Number.isNaN(v)) return '';
  136. let result = val;
  137. if (val > 10000) {
  138. result = Math.floor(val / 10000);
  139. result = (
  140. <span>
  141. {result}
  142. <span
  143. styles={{
  144. position: 'relative',
  145. top: -2,
  146. fontSize: 14,
  147. fontStyle: 'normal',
  148. lineHeight: 20,
  149. marginLeft: 2,
  150. }}
  151. >
  152. </span>
  153. </span>
  154. );
  155. }
  156. return result;
  157. }
  158. export function isAntdPro() {
  159. return window.location.hostname === 'preview.pro.ant.design';
  160. }