tools.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { defalutModalForm } from '@/components/TaskModal/api'
  2. import { ElMessageBox } from 'element-plus';
  3. /**
  4. * 判断值是否为空
  5. * @param value 值
  6. * @returns {boolean}
  7. */
  8. export function isValueEmpty(value: any): boolean {
  9. if (value === null || value === undefined) {
  10. return true;
  11. }
  12. if (typeof value === "string" && value.trim() === "") {
  13. return true;
  14. }
  15. if (Array.isArray(value) && value.length === 0) {
  16. return true;
  17. }
  18. if (
  19. typeof value === "object" &&
  20. !Array.isArray(value) &&
  21. Object.keys(value).length === 0
  22. ) {
  23. return true;
  24. }
  25. if (typeof value === "symbol" && value.toString() === "Symbol()") {
  26. return true;
  27. }
  28. return false;
  29. }
  30. /**
  31. * 获取表单数据中有值的数据
  32. * @param formData 表单数据
  33. * @returns {T}
  34. */
  35. export function getFromValue<T>(formData: T): T {
  36. const result: any = {};
  37. for (const key in formData) {
  38. if (!isValueEmpty(formData[key])) {
  39. result[key] = formData[key];
  40. }
  41. }
  42. return result;
  43. }
  44. export function resetFromValue<T>(formData: T, resetForm: any = {}) {
  45. const result: any = {};
  46. for (const key in formData) {
  47. result[key] = "";
  48. }
  49. // return result;
  50. return { ...result, ...resetForm }
  51. }
  52. /**
  53. * 更新部门数据
  54. * @param arr 部门数据源
  55. * @param flag 是否需要添加全部人员和未分配
  56. * @returns
  57. */
  58. export function updateDepTreeData(arr: any, flag: boolean = false) {
  59. const result = []; // 创建一个新数组来存储结果
  60. for (let i = 0; i < arr.length; i++) {
  61. if (arr[i].id !== -1 && arr[i].id !== 0) {
  62. if (Array.isArray(arr[i].children) && arr[i].children.length > 0) {
  63. arr[i].children = updateDepTreeData(arr[i].children); // 递归更新子节点
  64. }
  65. arr[i].value = arr[i].id; // 更新value字段
  66. result.push(arr[i]); // 将更新后的节点添加到结果数组
  67. }
  68. }
  69. if (flag) {
  70. result.splice(0, 0, {
  71. id: -1,
  72. label: "全部人员",
  73. });
  74. result.push({
  75. id: 0,
  76. label: "未分配",
  77. });
  78. return result;
  79. }
  80. return result; // 返回更新后的数组,不包含id为-1或0的节点
  81. }
  82. const listByCode = [
  83. { name: "线索来源", id: "ClueSources" },
  84. { name: "客户级别", id: "CustomLevel" },
  85. { name: "客户行业", id: "CustomIndustry" },
  86. { name: "客户来源", id: "CustomSources" },
  87. { name: "商机阶段", id: "BusinessStage" },
  88. { name: "产品类型", id: "ProductType" },
  89. { name: "产品单位", id: "ProductUnit" },
  90. { name: "订单类型", id: "OrderType" },
  91. ];
  92. /**
  93. * 获取系统字段的数据
  94. * @param arr 需要获取字典的中文名称
  95. * @returns 接口所需要的id
  96. */
  97. export function getAllListByCode(arr: ListByCodeType) {
  98. const result = arr
  99. .map((item) => {
  100. const found = listByCode.find((obj) => obj.name === item);
  101. return found ? found.id : null;
  102. })
  103. .filter(Boolean);
  104. return result;
  105. }
  106. /**
  107. * 获取当月第一天
  108. * @param date 日期 new Date()
  109. * @returns
  110. */
  111. export function getFirstDayOfMonth(date: Date) {
  112. const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  113. return formatDate(firstDay);
  114. }
  115. /**
  116. * 获取当月最后一天
  117. * @param date 日期 new Date()
  118. * @returns
  119. */
  120. export function getLastDayOfMonth(date: Date) {
  121. const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  122. return formatDate(nextMonth);
  123. }
  124. /**
  125. * 将 Date 对象格式化为 "YYYY-MM-DD" 的形式
  126. * @param date 日期 new Date()
  127. * @returns
  128. */
  129. export function formatDate(date: Date) {
  130. const year = date.getFullYear();
  131. const month = (1 + date.getMonth()).toString().padStart(2, '0');
  132. const day = date.getDate().toString().padStart(2, '0');
  133. return `${year}-${month}-${day}`;
  134. }
  135. /**
  136. * 获取创建任务的 form
  137. * @param taskType 任务类型
  138. * @returns form
  139. */
  140. export function createTaskFromType(taskType: TASK_VALUE_TYPE) {
  141. return {
  142. ...defalutModalForm,
  143. taskType
  144. }
  145. }
  146. /**
  147. * 下载文件
  148. * @param dataFile 接口返回的数据
  149. * @param fileName 文件名称
  150. */
  151. export function downloadFile(dataFile: any, fileName: string) {
  152. const data = dataFile;
  153. const blob = new Blob([data]);
  154. const url = window.URL.createObjectURL(blob);
  155. const a = document.createElement('a');
  156. a.href = url;
  157. a.download = fileName;
  158. document.body.appendChild(a);
  159. a.click();
  160. window.URL.revokeObjectURL(url);
  161. document.body.removeChild(a);
  162. };
  163. /**
  164. * 消息弹窗框
  165. * @param message 消息弹窗框提示
  166. * @param title 消息弹窗框标题
  167. * @param type type 类型
  168. * @param options 消息弹窗框其他配置
  169. * @returns promise
  170. */
  171. export function confirmAction(message: string, title = '', type: componentType = 'warning', options = {}) {
  172. return new Promise<void>((resolve, reject) => {
  173. ElMessageBox.confirm(message, title, {
  174. ...{
  175. confirmButtonText: '确定',
  176. cancelButtonText: '取消',
  177. type: type,
  178. },
  179. ...options,
  180. })
  181. .then(() => resolve())
  182. .catch(() => reject());
  183. });
  184. }
  185. /**
  186. * 返回上一级
  187. */
  188. export function backPath() {
  189. window.history.go(-1);
  190. }