import { defalutModalForm } from "@/components/TaskModal/api"; import { ElNotification } from "element-plus"; import { ElMessageBox } from "element-plus"; import { IMPORTTIMELIST } from '../pages/api' import { get, post } from "./request"; /** * 判断值是否为空 * @param value 值 * @returns {boolean} */ export function isValueEmpty(value: any): boolean { if (value === null || value === undefined) { return true; } if (typeof value === "string" && value.trim() === "") { return true; } if (Array.isArray(value) && value.length === 0) { return true; } if ( typeof value === "object" && !Array.isArray(value) && Object.keys(value).length === 0 ) { return true; } if (typeof value === "symbol" && value.toString() === "Symbol()") { return true; } return false; } /** * 获取表单数据中有值的数据 * @param formData 表单数据 * @returns {T} */ export function getFromValue(formData: T): T { const result: any = {}; for (const key in formData) { if (!isValueEmpty(formData[key])) { result[key] = formData[key]; } } return result; } export function resetFromValue(formData: T, resetForm: any = {}) { const result: any = {}; for (const key in formData) { result[key] = ""; } // return result; return { ...result, ...resetForm }; } /** * 更新部门数据 * @param arr 部门数据源 * @param flag 是否需要添加全部人员和未分配 * @returns */ export function updateDepTreeData(arr: any, flag: boolean = false) { const result = []; // 创建一个新数组来存储结果 for (let i = 0; i < arr.length; i++) { if (arr[i].id !== -1 && arr[i].id !== 0) { if (Array.isArray(arr[i].children) && arr[i].children.length > 0) { arr[i].children = updateDepTreeData(arr[i].children); // 递归更新子节点 } arr[i].value = arr[i].id; // 更新value字段 result.push(arr[i]); // 将更新后的节点添加到结果数组 } } if (flag) { result.splice(0, 0, { id: -1, label: "全部人员", }); result.push({ id: 0, label: "未分配", }); return result; } return result; // 返回更新后的数组,不包含id为-1或0的节点 } const listByCode = [ { name: "线索来源", id: "ClueSources" }, { name: "客户级别", id: "CustomLevel" }, { name: "客户行业", id: "CustomIndustry" }, { name: "客户来源", id: "CustomSources" }, { name: "商机阶段", id: "BusinessStage" }, { name: "产品类型", id: "ProductType" }, { name: "产品单位", id: "ProductUnit" }, { name: "订单类型", id: "OrderType" }, ]; /** * 获取系统字段的数据 * @param arr 需要获取字典的中文名称 * @returns 接口所需要的id */ export function getAllListByCode(arr: ListByCodeType) { const result = arr .map((item) => { const found = listByCode.find((obj) => obj.name === item); return found ? found.id : null; }) .filter(Boolean); return result; } /** * 获取当月第一天 * @param date 日期 new Date() * @returns */ export function getFirstDayOfMonth(date: Date) { const firstDay = new Date(date.getFullYear(), date.getMonth(), 1); return formatDate(firstDay); } /** * 获取当月最后一天 * @param date 日期 new Date() * @returns */ export function getLastDayOfMonth(date: Date) { const nextMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0); return formatDate(nextMonth); } /** * 将 Date 对象格式化为 "YYYY-MM-DD" 的形式 * @param date 日期 new Date() * @returns */ export function formatDate(date: Date) { const year = date.getFullYear(); const month = (1 + date.getMonth()).toString().padStart(2, "0"); const day = date.getDate().toString().padStart(2, "0"); return `${year}-${month}-${day}`; } /** * 获取创建任务的 form * @param taskType 任务类型 * @returns form */ export function createTaskFromType(taskType: TASK_VALUE_TYPE) { return { ...defalutModalForm, taskType, }; } /** * 下载文件 * @param fileData 接口返回的数据 或 文件地址 * @param fileName 文件名称 */ export async function downloadFile(fileData: any, fileName: string) { const url = fileData; const link = document.createElement("a"); link.href = url; link.setAttribute("download", fileName); document.body.appendChild(link); link.click(); document.body.removeChild(link); } /** * 消息弹窗框 * @param message 消息弹窗框提示 * @param title 消息弹窗框标题 * @param type type 类型 * @param options 消息弹窗框其他配置 * @returns promise */ export function confirmAction( message: string, title = "", type: componentType = "warning", options = {} ) { return new Promise((resolve, reject) => { ElMessageBox.confirm(message, title, { ...{ confirmButtonText: "确定", cancelButtonText: "取消", type: type, }, ...options, }) .then(() => resolve()) .catch(() => reject()); }); } /** * 返回上一级 */ export function backPath() { window.history.go(-1); } /** * 下载模板 * @param mod 模块名称(模板链接) * @param fileName 下载的文件名称 */ export function downloadTemplate(mod: any, fileName: string) { post(`${IMPORTTIMELIST}`, { code: mod }).then((res: any) => { downloadFile(res.data, fileName) }); } /** * 取出自定义模板需要的 key * @param list 自定义模板数据(list) * @returns */ export function getTemplateKey(list: Array) { const parsedList = list; const models: Array = []; parsedList.forEach((item: any) => { if (item.type === 'grid') { item.columns.forEach((column: any) => { column.list.forEach((subItem: any) => { models.push(subItem.model); }); }); } else { models.push(item.model); } }); return models; } /** * 设置模板数据禁用 * @param list 模板数据 * @param fieldList 需要禁用的字段 * @returns */ export function setTemplateDataDisable(list: Array, fieldList: Array) { let result = list; result.forEach((item: any) => { if (item.type === 'grid') { item.columns.forEach((column: any) => { column.list.forEach((subItem: any) => { if (fieldList.includes(subItem.model)) { subItem.options.disabled = true } }); }); } else { if (fieldList.includes(item.model)) { item.options.disabled = true; } } }) return result; } /** * 新建商机指定方法,用来判断商机金额需与产品总金额是否相等 * @param mob 商机表单 * @param arr 相关产品 * @returns Boolean */ export function judgmentaAmounteEqual(mob: any, arr: any[] = []) { let flag = true; const amounte = mob.amountOfMoney || 0; const totalAmounte = arr.reduce((pre: number, cur: any) => pre + (cur.totalPrice || 0), 0); if (amounte !== totalAmounte) { ElNotification.closeAll(); ElNotification({ title: '提示', message: `商机金额${amounte > totalAmounte ? '大于' : '小于'}产品总金额,请修改`, type: 'warning', }); flag = false; } return flag; }