tools.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * 判断值是否为空
  3. * @param value 值
  4. * @returns {boolean}
  5. */
  6. export function isValueEmpty(value: any): boolean {
  7. if (value === null || value === undefined) {
  8. return true;
  9. }
  10. if (typeof value === "string" && value.trim() === "") {
  11. return true;
  12. }
  13. if (Array.isArray(value) && value.length === 0) {
  14. return true;
  15. }
  16. if (
  17. typeof value === "object" &&
  18. !Array.isArray(value) &&
  19. Object.keys(value).length === 0
  20. ) {
  21. return true;
  22. }
  23. if (typeof value === "symbol" && value.toString() === "Symbol()") {
  24. return true;
  25. }
  26. return false;
  27. }
  28. /**
  29. * 获取表单数据中有值的数据
  30. * @param formData 表单数据
  31. * @returns {T}
  32. */
  33. export function getFromValue<T>(formData: T): T {
  34. const result: any = {};
  35. for (const key in formData) {
  36. if (!isValueEmpty(formData[key])) {
  37. result[key] = formData[key];
  38. }
  39. }
  40. return result;
  41. }
  42. export function resetFromValue<T>(formData: T, resetForm: any = {}) {
  43. const result: any = {};
  44. for (const key in formData) {
  45. result[key] = "";
  46. }
  47. // return result;
  48. return { ...result, ...resetForm }
  49. }
  50. /**
  51. * 更新部门数据
  52. * @param arr 部门数据源
  53. * @param flag 是否需要添加全部人员和未分配
  54. * @returns
  55. */
  56. export function updateDepTreeData(arr: any, flag: boolean = false) {
  57. const result = []; // 创建一个新数组来存储结果
  58. for (let i = 0; i < arr.length; i++) {
  59. if (arr[i].id !== -1 && arr[i].id !== 0) {
  60. if (Array.isArray(arr[i].children) && arr[i].children.length > 0) {
  61. arr[i].children = updateDepTreeData(arr[i].children); // 递归更新子节点
  62. }
  63. arr[i].value = arr[i].id; // 更新value字段
  64. result.push(arr[i]); // 将更新后的节点添加到结果数组
  65. }
  66. }
  67. if (flag) {
  68. result.splice(0, 0, {
  69. id: -1,
  70. label: "全部人员",
  71. });
  72. result.push({
  73. id: 0,
  74. label: "未分配",
  75. });
  76. return result;
  77. }
  78. return result; // 返回更新后的数组,不包含id为-1或0的节点
  79. }
  80. const listByCode = [
  81. { name: "线索来源", id: "ClueSources" },
  82. { name: "客户级别", id: "CustomLevel" },
  83. { name: "客户行业", id: "CustomIndustry" },
  84. { name: "客户来源", id: "CustomSources" },
  85. { name: "商机阶段", id: "BusinessStage" },
  86. { name: "产品类型", id: "ProductType" },
  87. { name: "产品单位", id: "ProductUnit" },
  88. { name: "订单类型", id: "OrderType" },
  89. ];
  90. /**
  91. * 获取系统字段的数据
  92. * @param arr 需要获取字典的中文名称
  93. * @returns 接口所需要的id
  94. */
  95. export function getAllListByCode(arr: ListByCodeType) {
  96. const result = arr
  97. .map((item) => {
  98. const found = listByCode.find((obj) => obj.name === item);
  99. return found ? found.id : null;
  100. })
  101. .filter(Boolean);
  102. return result;
  103. }
  104. /**
  105. * 获取当月第一天
  106. * @param date 日期 new Date()
  107. * @returns
  108. */
  109. export function getFirstDayOfMonth(date: Date) {
  110. const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  111. return formatDate(firstDay);
  112. }
  113. /**
  114. * 获取当月最后一天
  115. * @param date 日期 new Date()
  116. * @returns
  117. */
  118. export function getLastDayOfMonth(date: Date) {
  119. const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  120. return formatDate(nextMonth);
  121. }
  122. /**
  123. * 将 Date 对象格式化为 "YYYY-MM-DD" 的形式
  124. * @param date 日期 new Date()
  125. * @returns
  126. */
  127. export function formatDate(date: Date) {
  128. const year = date.getFullYear();
  129. const month = (1 + date.getMonth()).toString().padStart(2, '0');
  130. const day = date.getDate().toString().padStart(2, '0');
  131. return `${year}-${month}-${day}`;
  132. }
  133. /**
  134. * 返回上一级
  135. */
  136. export function backPath() {
  137. window.history.go(-1);
  138. }