utils.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import moment from 'moment';
  2. import cloneDeep from 'lodash/cloneDeep';
  3. import navData from '../common/nav';
  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 [moment(`${year}-${fixedZero(month + 1)}-01 00:00:00`), moment(new Date(`${nextYear}-${fixedZero(nextMonth + 1)}-01 00:00:00`).getTime() - 1000)];
  36. }
  37. if (type === 'year') {
  38. const year = now.getFullYear();
  39. return [moment(`${year}-01-01 00:00:00`), moment(`${year}-12-31 23:59:59`)];
  40. }
  41. }
  42. function getPlainNode(nodeList, parentPath = '') {
  43. const arr = [];
  44. nodeList.forEach((node) => {
  45. const item = node;
  46. item.path = `${parentPath}/${item.path || ''}`.replace(/\/+/g, '/');
  47. item.exact = true;
  48. if (item.children && !item.component) {
  49. arr.push(...getPlainNode(item.children, item.path));
  50. } else {
  51. if (item.children && item.component) {
  52. item.exact = false;
  53. }
  54. arr.push(item);
  55. }
  56. });
  57. return arr;
  58. }
  59. export function getRouteData(path) {
  60. if (!navData.some(item => item.layout === path) ||
  61. !(navData.filter(item => item.layout === path)[0].children)) {
  62. return null;
  63. }
  64. const dataList = cloneDeep(navData.filter(item => item.layout === path)[0]);
  65. const nodeList = getPlainNode(dataList.children);
  66. return nodeList;
  67. }
  68. export function digitUppercase(n) {
  69. const fraction = ['角', '分'];
  70. const digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
  71. const unit = [
  72. ['元', '万', '亿'],
  73. ['', '拾', '佰', '仟'],
  74. ];
  75. let num = Math.abs(n);
  76. let s = '';
  77. fraction.forEach((item, index) => {
  78. s += (digit[Math.floor(num * 10 * (10 ** index)) % 10] + item).replace(/零./, '');
  79. });
  80. s = s || '整';
  81. num = Math.floor(num);
  82. for (let i = 0; i < unit[0].length && num > 0; i += 1) {
  83. let p = '';
  84. for (let j = 0; j < unit[1].length && num > 0; j += 1) {
  85. p = digit[num % 10] + unit[1][j] + p;
  86. num = Math.floor(num / 10);
  87. }
  88. s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
  89. }
  90. return s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
  91. }