import { listFileByBasisIdMulType, getToolDetailById, queryToolDict, toolDelete, } from "@/api/bussiness"; import { useRouter } from "vue-router"; import { groupByKey } from "@/utils"; // 数据表格的 hooks const useTable = ( open, title, form, formLoading, submitFormLevelTwoDict, getTableData, updateTypeOneDict, reset ) => { const { proxy } = getCurrentInstance(); const router = useRouter(); // 表格数据 const toolList = ref([]); // 表格的 loading 状态 const loading = ref(true); // 表格勾选框选中的 id 列表 const ids = ref([]); // 多个勾选时为 true 其他为 false const single = ref(true); // 只要有勾选项就为 true const multiple = ref(true); // 多选框选中数据 const handleSelectionChange = (selection) => { ids.value = selection.map((item) => item.id); single.value = selection.length != 1; multiple.value = !selection.length; }; // 新增按钮操作 const handleAdd = () => { reset(); // 重新更新一下大类下拉列表 updateTypeOneDict(); open.value = true; title.value = "添加工具"; }; // 获取类型id const getTypeId = (ancestors, level) => { return ancestors.split(",")[level]; }; // 获取二级分类字典 const updateTypeTwoDict = (parentId) => { queryToolDict("2", parentId).then((data) => { submitFormLevelTwoDict.value = data || []; }); }; // 点击表格上的修改按钮 const handleSelecterUpdate = () => { const targetId = ids.value[0]; formLoading.value = true; getToolDetailById(targetId).then((res) => { handleUpdate(res.data); }); }; // 修改按钮操作 const handleUpdate = (row) => { reset(); updateTypeOneDict(); open.value = true; formLoading.value = true; // 获取工具对应的附件 listFileByBasisIdMulType({ basisId: row.id, delFlag: "0", informationType: 'sc,zl,syt' }) .then((res) => { // 重新修改一下字段 const allFiles = (res.data || []).map((item) => ({ id: item.id, name: item.fileName, url: item.filePath, informationType: item.informationType, })); // 附件分组 const fileMap = groupByKey(allFiles, "informationType"); // 根据祖级列表获取二类的下拉列表 const typeOneId = +getTypeId(row.ancestors, 1); updateTypeTwoDict(typeOneId); // 表单赋值 form.value = { id: row.id, toolName: row.toolName, aliasName: row.aliasName, levelOneId: typeOneId, levelTwoId: row.parentId, describe: row.describe, toolNum: row.toolNum || 0, specifications: row.specifications, example: row.example, // 手册附件 manualFileList: fileMap["sc"] || [], // 资料附件 resourceFileList: fileMap["zl"] || [], // 示意图 imageList: fileMap["syt"] || [], }; title.value = "修改工具"; }) .finally(() => { formLoading.value = false; }); }; // 删除按钮操作 const handleDelete = (row) => { const toolIds = row.id || ids.value; proxy.$modal .confirm("是否确认删除该工具?") .then(function () { return toolDelete(toolIds); }) .then(() => { getTableData(); proxy.$modal.msgSuccess("删除成功"); }) .catch(() => {}); }; // 跳转到物料管理页面 const handlePageToToolsMaterial = (row) => { router.push({ path: "/toolsMaterial", query: { toolId: row.id, }, }); }; return { toolList, loading, single, multiple, handleSelectionChange, handleAdd, handleUpdate, handleDelete, handleSelecterUpdate, handlePageToToolsMaterial, }; }; export default useTable;