util.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { getAction } from '@/api/flowable/manage';
  2. import store from '@/store';
  3. export function formatDictText(dicts, values) {
  4. if (!(Array.isArray(dicts) && dicts.length > 0)) {
  5. return values;
  6. }
  7. if (!values) {
  8. return '';
  9. }
  10. let valueArr = values.split(',');
  11. let contentArr = [];
  12. dicts.forEach((dict) => {
  13. for (let i = 0; i < valueArr.length; i++) {
  14. if (valueArr[i] === dict.value) {
  15. contentArr.push(dict.content);
  16. break;
  17. }
  18. }
  19. });
  20. return contentArr.toString();
  21. }
  22. export function randomString(len) {
  23. len = len || 32;
  24. const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  25. /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  26. const maxPos = $chars.length;
  27. let pwd = '';
  28. for (let i = 0; i < len; i++) {
  29. pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  30. }
  31. return pwd;
  32. }
  33. // 深度拷贝
  34. export function copyObj(obj) {
  35. //变量先置空
  36. let newobj = null;
  37. //判断是否需要继续进行递归
  38. if (typeof obj == 'object' && obj !== null) {
  39. newobj = obj instanceof Array ? [] : {}; //进行下一层递归克隆
  40. for (const i in obj) {
  41. newobj[i] = copyObj(obj[i]);
  42. } //如果不是对象直接赋值
  43. } else {
  44. newobj = obj;
  45. }
  46. return newobj;
  47. }
  48. export function getRecords(records) {
  49. const result = {};
  50. records.forEach((record) => {
  51. const res = record.deliveryTime.split('-');
  52. const yearMonth = res.slice(0, 2).join('-');
  53. const day = 'date' + res.slice(-1);
  54. const quantity = record.quantity;
  55. if (!result[day]) {
  56. result[day] = [];
  57. }
  58. const existingEntry = result[day].find((entry) => entry.day === yearMonth);
  59. if (existingEntry) {
  60. existingEntry.num += quantity;
  61. } else {
  62. result[day].push({ day: yearMonth, num: quantity });
  63. }
  64. });
  65. return result;
  66. }
  67. export function groupByProperty(arr, property) {
  68. return arr.reduce((acc, item) => {
  69. const key = item[property];
  70. // 若对象中不存在该键,则初始化一个空数组
  71. if (!acc[key]) {
  72. acc[key] = [];
  73. }
  74. // 将当前元素添加到对应的数组中
  75. acc[key].push(item);
  76. return acc;
  77. }, {});
  78. }
  79. export function getMaxArrayLength(obj) {
  80. let maxLength = 0;
  81. // 遍历对象的所有属性
  82. for (const key in obj) {
  83. if (Array.isArray(obj[key])) {
  84. // 比较当前数组长度与最大长度
  85. maxLength = Math.max(maxLength, obj[key].length);
  86. }
  87. }
  88. return maxLength;
  89. }
  90. export function getMaxSameProcessIdCount(arr) {
  91. const countMap = {};
  92. // 遍历数组,统计每个 processId 出现的次数
  93. arr.forEach((item) => {
  94. const processId = item.processId;
  95. if (countMap[processId]) {
  96. countMap[processId]++;
  97. } else {
  98. countMap[processId] = 1;
  99. }
  100. });
  101. let maxCount = 0;
  102. // 遍历计数对象,找出最大的计数
  103. for (const key in countMap) {
  104. maxCount = Math.max(maxCount, countMap[key]);
  105. }
  106. return maxCount;
  107. }
  108. //合计费用
  109. export function getSummaries(param, key, unit) {
  110. const { columns, data } = param;
  111. const sums = [];
  112. columns.forEach((column, index) => {
  113. if (index === 0) {
  114. sums[index] = '合计';
  115. return;
  116. }
  117. if (key.includes(column.property)) {
  118. const values = data.map((item) => Number(item[column.property]));
  119. console.log(values);
  120. if (!values.every((value) => isNaN(value))) {
  121. sums[index] = values.reduce((prev, curr) => {
  122. const value = Number(curr);
  123. if (!isNaN(value)) {
  124. return prev + curr;
  125. } else {
  126. return prev;
  127. }
  128. }, 0);
  129. sums[index] = parseFloat(sums[index])
  130. .toFixed(3)
  131. .replace(/\.?0+$/, '');
  132. sums[index] += ' ' + unit + '';
  133. } else {
  134. sums[index] = '';
  135. }
  136. } else {
  137. sums[index] = '';
  138. }
  139. });
  140. return sums;
  141. }