tools.ts 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. import { defalutModalForm } from "@/components/TaskModal/api";
  2. import { ElNotification } from "element-plus";
  3. import { ElMessageBox } from "element-plus";
  4. import { IMPORTTIMELIST } from '../pages/api'
  5. import { get, post } from "./request";
  6. /**
  7. * 判断值是否为空
  8. * @param value 值
  9. * @returns {boolean}
  10. */
  11. export function isValueEmpty(value: any): boolean {
  12. if (value === null || value === undefined) {
  13. return true;
  14. }
  15. if (typeof value === "string" && value.trim() === "") {
  16. return true;
  17. }
  18. if (Array.isArray(value) && value.length === 0) {
  19. return true;
  20. }
  21. if (
  22. typeof value === "object" &&
  23. !Array.isArray(value) &&
  24. Object.keys(value).length === 0
  25. ) {
  26. return true;
  27. }
  28. if (typeof value === "symbol" && value.toString() === "Symbol()") {
  29. return true;
  30. }
  31. return false;
  32. }
  33. /**
  34. * 获取表单数据中有值的数据
  35. * @param formData 表单数据
  36. * @returns {T}
  37. */
  38. export function getFromValue<T>(formData: T): T {
  39. const result: any = {};
  40. for (const key in formData) {
  41. if (!isValueEmpty(formData[key])) {
  42. result[key] = formData[key];
  43. }
  44. }
  45. return result;
  46. }
  47. export function resetFromValue<T>(formData: T, resetForm: any = {}) {
  48. const result: any = {};
  49. for (const key in formData) {
  50. result[key] = "";
  51. }
  52. // return result;
  53. return { ...result, ...resetForm };
  54. }
  55. /**
  56. * 更新部门数据
  57. * @param arr 部门数据源
  58. * @param flag 是否需要添加全部人员和未分配
  59. * @returns
  60. */
  61. export function updateDepTreeData(arr: any, flag: boolean = false) {
  62. const result = []; // 创建一个新数组来存储结果
  63. for (let i = 0; i < arr.length; i++) {
  64. if (arr[i].id !== -1 && arr[i].id !== 0) {
  65. if (Array.isArray(arr[i].children) && arr[i].children.length > 0) {
  66. arr[i].children = updateDepTreeData(arr[i].children); // 递归更新子节点
  67. }
  68. arr[i].value = arr[i].id; // 更新value字段
  69. result.push(arr[i]); // 将更新后的节点添加到结果数组
  70. }
  71. }
  72. if (flag) {
  73. result.splice(0, 0, {
  74. id: -1,
  75. label: "全部人员",
  76. });
  77. result.push({
  78. id: 0,
  79. label: "未分配",
  80. });
  81. return result;
  82. }
  83. return result; // 返回更新后的数组,不包含id为-1或0的节点
  84. }
  85. const listByCode = [
  86. { name: "线索来源", id: "ClueSources" },
  87. { name: "客户级别", id: "CustomLevel" },
  88. { name: "客户行业", id: "CustomIndustry" },
  89. { name: "客户来源", id: "CustomSources" },
  90. { name: "商机阶段", id: "BusinessStage" },
  91. { name: "产品类型", id: "ProductType" },
  92. { name: "产品单位", id: "ProductUnit" },
  93. { name: "订单类型", id: "OrderType" },
  94. ];
  95. /**
  96. * 获取系统字段的数据
  97. * @param arr 需要获取字典的中文名称
  98. * @returns 接口所需要的id
  99. */
  100. export function getAllListByCode(arr: ListByCodeType) {
  101. const result = arr
  102. .map((item) => {
  103. const found = listByCode.find((obj) => obj.name === item);
  104. return found ? found.id : null;
  105. })
  106. .filter(Boolean);
  107. return result;
  108. }
  109. /**
  110. * 获取当月第一天
  111. * @param date 日期 new Date()
  112. * @returns
  113. */
  114. export function getFirstDayOfMonth(date: Date) {
  115. const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  116. return formatDate(firstDay);
  117. }
  118. /**
  119. * 获取当月最后一天
  120. * @param date 日期 new Date()
  121. * @returns
  122. */
  123. export function getLastDayOfMonth(date: Date) {
  124. const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  125. return formatDate(nextMonth);
  126. }
  127. /**
  128. * 将 Date 对象格式化为 "YYYY-MM-DD" 的形式
  129. * @param date 日期 new Date()
  130. * @returns
  131. */
  132. export function formatDate(date: Date) {
  133. const year = date.getFullYear();
  134. const month = (1 + date.getMonth()).toString().padStart(2, "0");
  135. const day = date.getDate().toString().padStart(2, "0");
  136. return `${year}-${month}-${day}`;
  137. }
  138. /**
  139. * 获取创建任务的 form
  140. * @param taskType 任务类型
  141. * @returns form
  142. */
  143. export function createTaskFromType(taskType: TASK_VALUE_TYPE) {
  144. return {
  145. ...defalutModalForm,
  146. taskType,
  147. };
  148. }
  149. /**
  150. * 下载文件
  151. * @param fileData 接口返回的数据 或 文件地址
  152. * @param fileName 文件名称
  153. */
  154. export async function downloadFile(fileData: any, fileName: string) {
  155. const url = fileData;
  156. const link = document.createElement("a");
  157. link.href = url;
  158. link.setAttribute("download", fileName);
  159. document.body.appendChild(link);
  160. link.click();
  161. document.body.removeChild(link);
  162. }
  163. /**
  164. * 消息弹窗框
  165. * @param message 消息弹窗框提示
  166. * @param title 消息弹窗框标题
  167. * @param type type 类型
  168. * @param options 消息弹窗框其他配置
  169. * @returns promise
  170. */
  171. export function confirmAction(
  172. message: string,
  173. title = "",
  174. type: componentType = "warning",
  175. options = {}
  176. ) {
  177. return new Promise<void>((resolve, reject) => {
  178. ElMessageBox.confirm(message, title, {
  179. ...{
  180. confirmButtonText: "确定",
  181. cancelButtonText: "取消",
  182. type: type,
  183. },
  184. ...options,
  185. })
  186. .then(() => resolve())
  187. .catch(() => reject());
  188. });
  189. }
  190. /**
  191. * 返回上一级
  192. */
  193. export function backPath() {
  194. window.history.go(-1);
  195. }
  196. /**
  197. * 下载模板
  198. * @param mod 模块名称(模板链接)
  199. * @param fileName 下载的文件名称
  200. */
  201. export function downloadTemplate(mod: any, fileName: string) {
  202. post(`${IMPORTTIMELIST}`, { code: mod }).then((res: any) => {
  203. downloadFile(res.data, fileName)
  204. });
  205. }
  206. /**
  207. * 取出自定义模板需要的 key
  208. * @param list 自定义模板数据(list)
  209. * @returns
  210. */
  211. export function getTemplateKey(list: Array<any>) {
  212. const parsedList = list;
  213. const models: Array<string> = [];
  214. parsedList.forEach((item: any) => {
  215. if (item.type === 'grid') {
  216. item.columns.forEach((column: any) => {
  217. column.list.forEach((subItem: any) => {
  218. models.push(subItem.model);
  219. });
  220. });
  221. } else {
  222. models.push(item.model);
  223. }
  224. });
  225. return models;
  226. }
  227. /**
  228. * 设置模板数据禁用
  229. * @param list 模板数据
  230. * @param fieldList 需要禁用的字段
  231. * @returns
  232. */
  233. export function setTemplateDataDisable(list: Array<any>, fieldList: Array<any>) {
  234. let result = list;
  235. result.forEach((item: any) => {
  236. if (item.type === 'grid') {
  237. item.columns.forEach((column: any) => {
  238. column.list.forEach((subItem: any) => {
  239. if (fieldList.includes(subItem.model)) {
  240. subItem.options.disabled = true
  241. }
  242. });
  243. });
  244. } else {
  245. if (fieldList.includes(item.model)) {
  246. item.options.disabled = true;
  247. }
  248. }
  249. })
  250. return result;
  251. }
  252. /**
  253. * 新建商机指定方法,用来判断商机金额需与产品总金额是否相等
  254. * @param mob 商机表单
  255. * @param arr 相关产品
  256. * @returns Boolean
  257. */
  258. export function judgmentaAmounteEqual(mob: any, arr: any[] = []) {
  259. let flag = true;
  260. const amounte = mob.amountOfMoney || 0;
  261. const totalAmounte = arr.reduce((pre: number, cur: any) => pre + (cur.totalPrice || 0), 0);
  262. if (amounte !== totalAmounte) {
  263. ElNotification.closeAll();
  264. ElNotification({
  265. title: '提示',
  266. message: `商机金额${amounte > totalAmounte ? '大于' : '小于'}产品总金额,请修改`,
  267. type: 'warning',
  268. });
  269. flag = false;
  270. }
  271. return flag;
  272. }