/** * 判断值是否为空 * @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}`; } /** * 返回上一级 */ export function backPath() { window.history.go(-1); }