utils.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import React from 'react';
  2. import moment from 'moment';
  3. import { Badge } from 'antd';
  4. import {
  5. STATUS_NORMAL,
  6. DOMAIN_CP,
  7. DOMAIN_LJ,
  8. DOMAIN_PJ,
  9. } from './config';
  10. export function fixedZero(val) {
  11. return val * 1 < 10 ? `0${val}` : val;
  12. }
  13. export function getTimeDistance(type) {
  14. const now = new Date();
  15. const oneDay = 1000 * 60 * 60 * 24;
  16. if (type === 'today') {
  17. now.setHours(0);
  18. now.setMinutes(0);
  19. now.setSeconds(0);
  20. return [moment(now), moment(now.getTime() + (oneDay - 1000))];
  21. }
  22. if (type === 'week') {
  23. let day = now.getDay();
  24. now.setHours(0);
  25. now.setMinutes(0);
  26. now.setSeconds(0);
  27. if (day === 0) {
  28. day = 6;
  29. } else {
  30. day -= 1;
  31. }
  32. const beginTime = now.getTime() - (day * oneDay);
  33. return [moment(beginTime), moment(beginTime + ((7 * oneDay) - 1000))];
  34. }
  35. if (type === 'month') {
  36. const year = now.getFullYear();
  37. const month = now.getMonth();
  38. const nextDate = moment(now).add(1, 'months');
  39. const nextYear = nextDate.year();
  40. const nextMonth = nextDate.month();
  41. return [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(moment(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).valueOf() - 1000)];
  42. }
  43. if (type === 'year') {
  44. const year = now.getFullYear();
  45. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  46. }
  47. }
  48. export function getPlainNode(nodeList, parentPath = '') {
  49. const arr = [];
  50. nodeList.forEach((node) => {
  51. const item = node;
  52. item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
  53. item.exact = true;
  54. if (item.children && !item.component) {
  55. arr.push(...getPlainNode(item.children, item.path));
  56. } else {
  57. if (item.children && item.component) {
  58. item.exact = false;
  59. }
  60. arr.push(item);
  61. }
  62. });
  63. return arr;
  64. }
  65. export function digitUppercase(n) {
  66. const fraction = ['角', '分'];
  67. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  68. const unit = [
  69. ['元', '万', '亿'],
  70. ['', '拾', '佰', '仟'],
  71. ];
  72. let num = Math.abs(n);
  73. let s = '';
  74. fraction.forEach((item, index) => {
  75. s += (digit[Math.floor(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.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
  88. }
  89. function getRelation(str1, str2) {
  90. if (str1 === str2) {
  91. console.warn('Two path are equal!'); // eslint-disable-line
  92. }
  93. const arr1 = str1.split('/');
  94. const arr2 = str2.split('/');
  95. if (arr2.every((item, index) => item === arr1[index])) {
  96. return 1;
  97. } else if (arr1.every((item, index) => item === arr2[index])) {
  98. return 2;
  99. }
  100. return 3;
  101. }
  102. function getRenderArr(routes) {
  103. let renderArr = [];
  104. renderArr.push(routes[0]);
  105. for (let i = 1; i < routes.length; i += 1) {
  106. let isAdd = false;
  107. // 是否包含
  108. isAdd = renderArr.every(item => getRelation(item, routes[i]) === 3);
  109. // 去重
  110. renderArr = renderArr.filter(item => getRelation(item, routes[i]) !== 1);
  111. if (isAdd) {
  112. renderArr.push(routes[i]);
  113. }
  114. }
  115. return renderArr;
  116. }
  117. /**
  118. * Get router routing configuration
  119. * { path:{name,...param}}=>Array<{name,path ...param}>
  120. * @param {string} path
  121. * @param {routerData} routerData
  122. */
  123. export function getRoutes(path, routerData) {
  124. let routes = Object.keys(routerData).filter(routePath =>
  125. routePath.indexOf(path) === 0 && routePath !== path);
  126. // Replace path to '' eg. path='user' /user/name => name
  127. routes = routes.map(item => item.replace(path, ''));
  128. // Get the route to be rendered to remove the deep rendering
  129. const renderArr = getRenderArr(routes);
  130. // Conversion and stitching parameters
  131. const renderRoutes = renderArr.map((item) => {
  132. const exact = !routes.some(route => route !== item && getRelation(route, item) === 1);
  133. return {
  134. ...routerData[`${path}${item}`],
  135. key: `${path}${item}`,
  136. path: `${path}${item}`,
  137. exact,
  138. };
  139. });
  140. return renderRoutes;
  141. }
  142. /* eslint no-useless-escape:0 */
  143. const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g;
  144. export function isUrl(path) {
  145. return reg.test(path);
  146. }
  147. export function addRowKey(data) {
  148. return data.map(item => {
  149. item.key = item.id;
  150. return item;
  151. });
  152. }
  153. export function renderCategory(domain) {
  154. switch (domain) {
  155. case DOMAIN_CP:
  156. return '供应商';
  157. case DOMAIN_LJ:
  158. return '平台方';
  159. case DOMAIN_PJ:
  160. return '渠道商';
  161. default:
  162. return '未知';
  163. }
  164. }
  165. export function renderStatus(status) {
  166. if (status === STATUS_NORMAL) {
  167. return (
  168. <Badge status="success" text="正常" />
  169. );
  170. } else {
  171. return (
  172. <Badge status="error" text="删除" />
  173. );
  174. }
  175. };
  176. export function toDecimal2(x) {
  177. let f = parseFloat(x);
  178. if (isNaN(f)) {
  179. return false;
  180. }
  181. f = Math.round(x*100)/100;
  182. let s = f.toString();
  183. let rs = s.indexOf('.');
  184. if (rs < 0) {
  185. rs = s.length;
  186. s += '.';
  187. }
  188. while (s.length <= rs + 2) {
  189. s += '0';
  190. }
  191. return s;
  192. }