import { defalutModalForm } from '@/components/TaskModal/api' import { ElMessageBox } from 'element-plus'; /** * 判断值是否为空 * @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 dataFile 接口返回的数据 * @param fileName 文件名称 */ export function downloadFile(dataFile: any, fileName: string) { const data = dataFile; const blob = new Blob([data]); const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = fileName; document.body.appendChild(a); a.click(); window.URL.revokeObjectURL(url); document.body.removeChild(a); }; /** * 消息弹窗框 * @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); }