tools.ts 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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: "BusinessStage" },
  92. { name: "产品类型", id: "ProductType" },
  93. { name: "产品单位", id: "ProductUnit" },
  94. { name: "订单类型", id: "OrderType" },
  95. ];
  96. /**
  97. * 获取系统字段的数据
  98. * @param arr 需要获取字典的中文名称
  99. * @returns 接口所需要的id
  100. */
  101. export function getAllListByCode(arr: ListByCodeType) {
  102. const result = arr
  103. .map((item) => {
  104. const found = listByCode.find((obj) => obj.name === item);
  105. return found ? found.id : null;
  106. })
  107. .filter(Boolean);
  108. return result;
  109. }
  110. /**
  111. * 获取当月第一天
  112. * @param date 日期 new Date()
  113. * @returns
  114. */
  115. export function getFirstDayOfMonth(date: Date) {
  116. const firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  117. return formatDate(firstDay);
  118. }
  119. /**
  120. * 获取当月最后一天
  121. * @param date 日期 new Date()
  122. * @returns
  123. */
  124. export function getLastDayOfMonth(date: Date) {
  125. const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  126. return formatDate(nextMonth);
  127. }
  128. /**
  129. * 将 Date 对象格式化为 "YYYY-MM-DD" 的形式
  130. * @param date 日期 new Date()
  131. * @returns
  132. */
  133. export function formatDate(date: Date) {
  134. const year = date.getFullYear();
  135. const month = (1 + date.getMonth()).toString().padStart(2, "0");
  136. const day = date.getDate().toString().padStart(2, "0");
  137. return `${year}-${month}-${day}`;
  138. }
  139. /**
  140. * 获取创建任务的 form
  141. * @param taskType 任务类型
  142. * @returns form
  143. */
  144. export function createTaskFromType(taskType: TASK_VALUE_TYPE) {
  145. return {
  146. ...defalutModalForm,
  147. taskType,
  148. };
  149. }
  150. /**
  151. * 下载文件
  152. * @param fileData 接口返回的数据 或 文件地址
  153. * @param fileName 文件名称
  154. */
  155. export async function downloadFile(fileData: any, fileName: string) {
  156. const url = fileData;
  157. const link = document.createElement("a");
  158. link.href = url;
  159. link.setAttribute("download", fileName);
  160. document.body.appendChild(link);
  161. link.click();
  162. document.body.removeChild(link);
  163. }
  164. /**
  165. * 消息弹窗框
  166. * @param message 消息弹窗框提示
  167. * @param title 消息弹窗框标题
  168. * @param type type 类型
  169. * @param options 消息弹窗框其他配置
  170. * @returns promise
  171. */
  172. export function confirmAction(
  173. message: string,
  174. title = "",
  175. type: componentType = "warning",
  176. options = {}
  177. ) {
  178. return new Promise<void>((resolve, reject) => {
  179. ElMessageBox.confirm(message, title, {
  180. ...{
  181. confirmButtonText: "确定",
  182. cancelButtonText: "取消",
  183. type: type,
  184. },
  185. ...options,
  186. })
  187. .then(() => resolve())
  188. .catch(() => reject());
  189. });
  190. }
  191. /**
  192. * 返回上一级
  193. */
  194. export function backPath() {
  195. window.history.go(-1);
  196. }
  197. /**
  198. * 下载模板
  199. * @param mod 模块名称(模板链接)
  200. * @param fileName 下载的文件名称
  201. */
  202. export function downloadTemplate(mod: any, fileName: string) {
  203. post(`${IMPORTTIMELIST}`, { code: mod }).then((res: any) => {
  204. downloadFile(res.data, fileName)
  205. });
  206. }
  207. /**
  208. * 取出自定义模板需要的 key
  209. * @param list 自定义模板数据(list)
  210. * @returns
  211. */
  212. export function getTemplateKey(list: Array<any>) {
  213. const parsedList = list;
  214. const models: Array<string> = [];
  215. parsedList.forEach((item: any) => {
  216. if (item.type === 'grid') {
  217. item.columns.forEach((column: any) => {
  218. column.list.forEach((subItem: any) => {
  219. models.push(subItem.model);
  220. });
  221. });
  222. } else {
  223. models.push(item.model);
  224. }
  225. });
  226. return models;
  227. }
  228. /**
  229. * 设置模板数据禁用和启用
  230. * @param list 模板数据
  231. * @param fieldList 需要操作的字段
  232. * @param filedFlag Boolean
  233. * @returns
  234. */
  235. export function setTemplateDataDisable(list: Array<any>, fieldList: Array<any>, filedFlag: boolean = true) {
  236. let result = list;
  237. result.forEach((item: any) => {
  238. if (item.type === 'grid') {
  239. item.columns.forEach((column: any) => {
  240. column.list.forEach((subItem: any) => {
  241. if (fieldList.includes(subItem.model)) {
  242. subItem.options.disabled = filedFlag
  243. }
  244. });
  245. });
  246. } else {
  247. if (fieldList.includes(item.model)) {
  248. item.options.disabled = filedFlag;
  249. }
  250. }
  251. })
  252. return result;
  253. }
  254. /**
  255. * 新建商机指定方法,用来判断商机金额需与产品总金额是否相等
  256. * @param mob 商机表单
  257. * @param arr 相关产品
  258. * @returns Boolean
  259. */
  260. export function judgmentaAmounteEqual(mob: any, arr: any) {
  261. if(!arr || arr.length <= 0) {
  262. return false;
  263. }
  264. let flag = false;
  265. const amounte = mob.amountOfMoney || 0;
  266. const totalAmounte = arr.reduce((pre: number, cur: any) => pre + (cur.totalPrice || 0), 0);
  267. const isExistBusiness = sessionStorage.getItem("isExistBusiness");
  268. const businessLabel = isExistBusiness === "1" ? "商机" : "项目";
  269. if (amounte != totalAmounte) {
  270. ElNotification.closeAll();
  271. ElNotification({
  272. title: '提示',
  273. message: `${businessLabel}金额${amounte > totalAmounte ? '大于' : '小于'}产品总金额,${amounte > totalAmounte ? '' : '请修改'}`,
  274. type: 'warning',
  275. });
  276. flag = true;
  277. }
  278. return (amounte > totalAmounte) ? false : flag;
  279. }
  280. /**
  281. * 生成唯一id
  282. * @param minLength 最小长度
  283. * @param maxLength 最大长度
  284. * @returns
  285. */
  286. export function generateUniqueId(minLength = 3, maxLength = 8) {
  287. const timestamp = Date.now().toString(36); // 转换成36进制表示时间戳
  288. const randomPart = Math.random().toString(36).substring(2, 8); // 获取6位随机字符
  289. let uniqueId = timestamp + randomPart;
  290. if (uniqueId.length < minLength) {
  291. uniqueId = uniqueId.padStart(minLength, '0');
  292. } else if (uniqueId.length > maxLength) {
  293. uniqueId = uniqueId.substring(0, maxLength);
  294. }
  295. return uniqueId;
  296. }