useTable.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {
  2. listFileByBasisIdMulType,
  3. getToolDetailById,
  4. queryToolDict,
  5. toolDelete,
  6. } from "@/api/bussiness";
  7. import { useRouter } from "vue-router";
  8. import { groupByKey } from "@/utils";
  9. // 数据表格的 hooks
  10. const useTable = (
  11. open,
  12. title,
  13. form,
  14. formLoading,
  15. submitFormLevelTwoDict,
  16. getTableData,
  17. updateTypeOneDict,
  18. reset
  19. ) => {
  20. const { proxy } = getCurrentInstance();
  21. const router = useRouter();
  22. // 表格数据
  23. const toolList = ref([]);
  24. // 表格的 loading 状态
  25. const loading = ref(true);
  26. // 表格勾选框选中的 id 列表
  27. const ids = ref([]);
  28. // 多个勾选时为 true 其他为 false
  29. const single = ref(true);
  30. // 只要有勾选项就为 true
  31. const multiple = ref(true);
  32. // 多选框选中数据
  33. const handleSelectionChange = (selection) => {
  34. ids.value = selection.map((item) => item.id);
  35. single.value = selection.length != 1;
  36. multiple.value = !selection.length;
  37. };
  38. // 新增按钮操作
  39. const handleAdd = () => {
  40. reset();
  41. // 重新更新一下大类下拉列表
  42. updateTypeOneDict();
  43. open.value = true;
  44. title.value = "添加工具";
  45. };
  46. // 获取类型id
  47. const getTypeId = (ancestors, level) => {
  48. return ancestors.split(",")[level];
  49. };
  50. // 获取二级分类字典
  51. const updateTypeTwoDict = (parentId) => {
  52. queryToolDict("2", parentId).then((data) => {
  53. submitFormLevelTwoDict.value = data || [];
  54. });
  55. };
  56. // 点击表格上的修改按钮
  57. const handleSelecterUpdate = () => {
  58. const targetId = ids.value[0];
  59. formLoading.value = true;
  60. getToolDetailById(targetId).then((res) => {
  61. handleUpdate(res.data);
  62. });
  63. };
  64. // 修改按钮操作
  65. const handleUpdate = (row) => {
  66. reset();
  67. updateTypeOneDict();
  68. open.value = true;
  69. formLoading.value = true;
  70. // 获取工具对应的附件
  71. listFileByBasisIdMulType({
  72. basisId: row.id,
  73. delFlag: "0",
  74. informationType: 'sc,zl,syt'
  75. })
  76. .then((res) => {
  77. // 重新修改一下字段
  78. const allFiles = (res.data || []).map((item) => ({
  79. id: item.id,
  80. name: item.fileName,
  81. url: item.filePath,
  82. informationType: item.informationType,
  83. }));
  84. // 附件分组
  85. const fileMap = groupByKey(allFiles, "informationType");
  86. // 根据祖级列表获取二类的下拉列表
  87. const typeOneId = +getTypeId(row.ancestors, 1);
  88. updateTypeTwoDict(typeOneId);
  89. // 表单赋值
  90. form.value = {
  91. id: row.id,
  92. toolName: row.toolName,
  93. aliasName: row.aliasName,
  94. levelOneId: typeOneId,
  95. levelTwoId: row.parentId,
  96. describe: row.describe,
  97. toolNum: row.toolNum || 0,
  98. specifications: row.specifications,
  99. example: row.example,
  100. // 手册附件
  101. manualFileList: fileMap["sc"] || [],
  102. // 资料附件
  103. resourceFileList: fileMap["zl"] || [],
  104. // 示意图
  105. imageList: fileMap["syt"] || [],
  106. };
  107. title.value = "修改工具";
  108. })
  109. .finally(() => {
  110. formLoading.value = false;
  111. });
  112. };
  113. // 删除按钮操作
  114. const handleDelete = (row) => {
  115. const toolIds = row.id || ids.value;
  116. proxy.$modal
  117. .confirm("是否确认删除该工具?")
  118. .then(function () {
  119. return toolDelete(toolIds);
  120. })
  121. .then(() => {
  122. getTableData();
  123. proxy.$modal.msgSuccess("删除成功");
  124. })
  125. .catch(() => {});
  126. };
  127. // 跳转到物料管理页面
  128. const handlePageToToolsMaterial = (row) => {
  129. router.push({
  130. path: "/toolsMaterial",
  131. query: {
  132. toolId: row.id,
  133. },
  134. });
  135. };
  136. return {
  137. toolList,
  138. loading,
  139. single,
  140. multiple,
  141. handleSelectionChange,
  142. handleAdd,
  143. handleUpdate,
  144. handleDelete,
  145. handleSelecterUpdate,
  146. handlePageToToolsMaterial,
  147. };
  148. };
  149. export default useTable;