From 357abc75cfae4f74da3f9664b40266cd1a8ae95e Mon Sep 17 00:00:00 2001 From: jiangs <373297395@qq.com> Date: Fri, 26 Dec 2025 09:14:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=A2=E9=97=AE=E7=BB=84?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/messageBox.ts | 53 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/hooks/messageBox.ts diff --git a/src/hooks/messageBox.ts b/src/hooks/messageBox.ts new file mode 100644 index 0000000..87c4226 --- /dev/null +++ b/src/hooks/messageBox.ts @@ -0,0 +1,53 @@ +// 兼容verbatimModuleSyntax:拆分/标记类型与值导入 +import { ElMessageBox } from "element-plus"; +import type { ElMessageBoxOptions } from "element-plus"; + +/** + * 极简删除确认框封装(支持返回值判断) + * @param {string} [message] - 自定义删除提示信息(可选) + * @param {Partial} [customOptions] - 自定义配置(可选) + * @returns {Promise} 确认返回true,取消/关闭返回false + */ +export const deleteConfirm = ( + message?: string, + customOptions?: Partial +) => { + // 删除场景默认配置 + const defaultOptions: ElMessageBoxOptions = { + title: "删除确认", + message: message || "此操作将永久删除该数据,无法恢复,是否继续?", + confirmButtonText: "确认删除", + cancelButtonText: "取消", + type: "error", + //center: true, + }; + + // 合并默认配置与自定义配置 + const finalOptions = { ...defaultOptions, ...customOptions }; + + // 核心修改:无论确认/取消,都resolve布尔值,不触发reject + return new Promise((resolve) => { + ElMessageBox.confirm(finalOptions.message, finalOptions.title, finalOptions) + .then(() => resolve(true)) // 确认:返回true + .catch(() => resolve(false)); // 取消/关闭:返回false(不再reject) + }); +}; + +/** + * 使用示例 +const handleDelete = async (id: number) => { + // 直接获取返回值:true=确认,false=取消 + const isConfirmed = await deleteConfirm('是否删除数据?', { title: '自定义标题', type: 'warning' }) + + if (isConfirmed) { + // 确认删除:执行业务逻辑 + // await api.deleteData(id) // 真实接口请求 + ElMessage.success(`ID为${id}的数据删除成功!`) + console.log(`执行删除操作,数据ID:${id}`) + } else { + // 取消删除:给出提示(可选) + ElMessage.info('已取消删除操作') + console.log('用户取消删除') + } +} + */