utils.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import moment from 'moment';
  2. import React from 'react';
  3. import { parse, stringify } from 'qs';
  4. export function fixedZero(val) {
  5. return val * 1 < 10 ? `0${val}` : val;
  6. }
  7. export function getTimeDistance(type) {
  8. const now = new Date();
  9. const oneDay = 1000 * 60 * 60 * 24;
  10. if (type === 'today') {
  11. now.setHours(0);
  12. now.setMinutes(0);
  13. now.setSeconds(0);
  14. return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  15. }
  16. if (type === 'week') {
  17. let day = now.getDay();
  18. now.setHours(0);
  19. now.setMinutes(0);
  20. now.setSeconds(0);
  21. if (day === 0) {
  22. day = 6;
  23. } else {
  24. day -= 1;
  25. }
  26. const beginTime = now.getTime() - day * oneDay;
  27. return [moment(beginTime), moment(beginTime + (7 * oneDay - 1000))];
  28. }
  29. if (type === 'month') {
  30. const year = now.getFullYear();
  31. const month = now.getMonth();
  32. const nextDate = moment(now).add(1, 'months');
  33. const nextYear = nextDate.year();
  34. const nextMonth = nextDate.month();
  35. return [
  36. moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`),
  37. moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000),
  38. ];
  39. }
  40. const year = now.getFullYear();
  41. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  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. }
  101. 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. export function getPageQuery() {
  147. return parse(window.location.href.split('?')[1]);
  148. }
  149. export function getQueryPath(path = '', query = {}) {
  150. const search = stringify(query);
  151. if (search.length) {
  152. return `${path}?${search}`;
  153. }
  154. return path;
  155. }
  156. /* eslint no-useless-escape:0 */
  157. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
  158. export function isUrl(path) {
  159. return reg.test(path);
  160. }
  161. export function formatWan(val) {
  162. const v = val * 1;
  163. if (!v || Number.isNaN(v)) return '';
  164. let result = val;
  165. if (val > 10000) {
  166. result = Math.floor(val / 10000);
  167. result = (
  168. <span>
  169. {result}
  170. <span
  171. styles={{
  172. position: 'relative',
  173. top: -2,
  174. fontSize: 14,
  175. fontStyle: 'normal',
  176. lineHeight: 20,
  177. marginLeft: 2,
  178. }}
  179. >
  180. </span>
  181. </span>
  182. );
  183. }
  184. return result;
  185. }
  186. export function isAntdPro() {
  187. return window.location.hostname === 'preview.pro.ant.design';
  188. }