From c22c95ad32584dbc16a3628cbc4b6ae41de2f61e Mon Sep 17 00:00:00 2001 From: zhang_12370 <z2864490065@outlook.com> Date: 星期四, 26 六月 2025 10:08:55 +0800 Subject: [PATCH] 采购优化删除 --- src/views/procureMent/index.vue | 33 +++------- src/hooks/useDelete.js | 119 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 22 deletions(-) diff --git a/src/hooks/useDelete.js b/src/hooks/useDelete.js new file mode 100644 index 0000000..7c6df82 --- /dev/null +++ b/src/hooks/useDelete.js @@ -0,0 +1,119 @@ +/** + * 閫氱敤鍒犻櫎鍔熻兘缁勫悎寮忓嚱鏁� + * 鎻愪緵缁熶竴鐨勫垹闄ょ‘璁ゃ�丄PI璋冪敤銆佹暟鎹洿鏂伴�昏緫 + */ +import { ElMessage, ElMessageBox } from "element-plus"; + +/** + * 鍒涘缓鍒犻櫎鍔熻兘 + * @param {Object} options 閰嶇疆閫夐」 + * @param {Function} options.deleteApi 鍒犻櫎API鍑芥暟 + * @param {Function} options.getList 閲嶆柊鑾峰彇鍒楄〃鏁版嵁鐨勫嚱鏁� + * @param {Ref} options.selectedRows 閫変腑琛岀殑鍝嶅簲寮忓紩鐢� + * @param {Ref} options.tableData 琛ㄦ牸鏁版嵁鐨勫搷搴斿紡寮曠敤 + * @param {Ref} options.total 鎬绘暟鐨勫搷搴斿紡寮曠敤 + * @param {String} options.confirmText 纭鍒犻櫎鐨勬彁绀烘枃鏈� + * @param {String} options.successText 鍒犻櫎鎴愬姛鐨勬彁绀烘枃鏈� + * @param {Boolean} options.useLocalUpdate 鏄惁浣跨敤鏈湴鏇存柊锛堜笉閲嶆柊璇锋眰鎺ュ彛锛� + * @returns {Object} 杩斿洖鍒犻櫎鐩稿叧鐨勬柟娉� + */ +export function useDelete(options = {}) { + const { + deleteApi, + getList, + selectedRows, + tableData, + total, + confirmText = "纭畾鍒犻櫎閫変腑鐨勬暟鎹悧锛�", + successText = "鍒犻櫎鎴愬姛", + useLocalUpdate = false + } = options; + + /** + * 鎵归噺鍒犻櫎鏂规硶 + * @param {Array} customIds 鑷畾涔夎鍒犻櫎鐨処D鏁扮粍锛屽鏋滀笉浼犲垯浣跨敤selectedRows + */ + const handleDelete = async (customIds = null) => { + // 纭畾瑕佸垹闄ょ殑琛� + const rowsToDelete = customIds ? + tableData.value.filter(item => customIds.includes(item.id)) : + selectedRows.value; + + // 妫�鏌ユ槸鍚︽湁閫変腑鏁版嵁 + if (rowsToDelete.length === 0) { + ElMessage.warning("璇烽�夋嫨瑕佸垹闄ょ殑鏁版嵁"); + return false; + } + + try { + // 纭鍒犻櫎 + await ElMessageBox.confirm(confirmText, "鎻愮ず", { + confirmButtonText: "纭畾", + cancelButtonText: "鍙栨秷", + type: "warning", + }); + + // 鎻愬彇ID + const ids = rowsToDelete.map(item => item.id); + + // 璋冪敤鍒犻櫎API + const res = await deleteApi(ids); + + if (res.code === 200) { + // 鏍规嵁閰嶇疆閫夋嫨鏇存柊鏂瑰紡 + if (useLocalUpdate) { + // 鏈湴鏇存柊锛氫粠琛ㄦ牸鏁版嵁涓Щ闄ゅ凡鍒犻櫎鐨勯」 + tableData.value = tableData.value.filter(item => !ids.includes(item.id)); + if (total && total.value !== undefined) { + total.value = tableData.value.length; + } + } else { + // 閲嶆柊鑾峰彇鏁版嵁 + if (getList) { + await getList(); + } + } + + // 娓呯┖閫変腑鐘舵�� + if (selectedRows && selectedRows.value) { + selectedRows.value = []; + } + + ElMessage.success(successText); + return true; + } else { + ElMessage.error("鍒犻櫎澶辫触锛�" + (res.msg || "鏈煡閿欒")); + return false; + } + } catch (error) { + if (error !== "cancel") { + console.error("鍒犻櫎鎿嶄綔澶辫触:", error); + ElMessage.error("鍒犻櫎澶辫触锛�" + (error.message || "璇风◢鍚庨噸璇�")); + } else { + ElMessage.info("宸插彇娑堝垹闄�"); + } + return false; + } + }; + + /** + * 鍒犻櫎鍗曚釜椤圭洰 + * @param {Object} row 瑕佸垹闄ょ殑琛屾暟鎹� + */ + const handleDeleteSingle = async (row) => { + return await handleDelete([row.id]); + }; + + /** + * 鍒犻櫎澶氫釜椤圭洰锛堟壒閲忓垹闄わ級 + */ + const handleDeleteBatch = async () => { + return await handleDelete(); + }; + + return { + handleDelete, + handleDeleteSingle, + handleDeleteBatch + }; +} diff --git a/src/views/procureMent/index.vue b/src/views/procureMent/index.vue index f692780..5427783 100644 --- a/src/views/procureMent/index.vue +++ b/src/views/procureMent/index.vue @@ -73,7 +73,9 @@ purchaseRegistration, getSupplyList, getCoalInfoList, + delPR } from "@/api/procureMent"; +import { useDelete } from "@/hooks/useDelete"; import useUserStore from "@/store/modules/user"; // 寮曞叆瀛楀吀鏁版嵁 @@ -238,28 +240,15 @@ addOrEdit.value = "viewRow"; handleAddEdit(); }; -const handleDelete = () => { - if (selectedRows.value.length === 0) { - ElMessage.warning("璇烽�夋嫨瑕佸垹闄ょ殑鏁版嵁"); - return; - } - ElMessageBox.confirm(`纭畾鍒犻櫎閫変腑鐨勬暟鎹悧锛焋, "鎻愮ず", { - confirmButtonText: "纭畾", - cancelButtonText: "鍙栨秷", - type: "warning", - }) - .then(() => { - // 妯℃嫙鍒犻櫎鎿嶄綔 - tableData.value = tableData.value.filter( - (item) => !selectedRows.value.includes(item) - ); - total.value = tableData.value.length; - ElMessage.success("鍒犻櫎鎴愬姛"); - }) - .catch(() => { - ElMessage.info("宸插彇娑堝垹闄�"); - }); -}; +// 浣跨敤鍒犻櫎缁勫悎寮忓嚱鏁� - 绠�鍖栫増鏈� +const { handleDeleteBatch: handleDelete } = useDelete({ + deleteApi: delPR, + selectedRows, + tableData, + total, + confirmText: "纭畾鍒犻櫎閫変腑鐨勯噰璐褰曞悧锛�", + useLocalUpdate: true +}); const handleDeleteSuccess = (row) => { ElMessage.success("鍒犻櫎鎴愬姛锛�" + row.supplierName); }; -- Gitblit v1.9.3