pda-uni-app-perkins/utils/dialog.js

143 lines
3.2 KiB
JavaScript
Raw Normal View History

2025-10-31 22:49:51 +08:00
/**
* 弹窗工具类 - 基于 Uniapp 原生 API 的简洁实现
*
* 使用系统原生弹窗 API无需自定义组件
* 提供统一的接口用于显示成功警告错误消息和 Toast
*
* @example
* // 显示成功消息
* DialogUtils.showSuccessMessage('成功', '数据已保存');
*
* // 显示确认对话框
* uni.showModal({
* title: '确认',
* content: '确定删除吗?',
* success: (res) => {
* if (res.confirm) {
* // 执行删除
* }
* }
* });
*
* // 显示 Toast 提示
* DialogUtils.toast('保存中...', 'loading');
*/
export const DialogUtils = {
/**
* 显示成功消息弹窗
* @param {string} title - 弹窗标题
* @param {string} message - 消息内容
* @param {string} confirmText - 确认按钮文本
*/
showSuccessMessage(title = '成功', message = '', confirmText = '我知道了') {
uni.showModal({
title,
content: message,
showCancel: false,
confirmText
});
},
/**
* 显示警告消息弹窗
* @param {string} title - 弹窗标题
* @param {string} message - 消息内容
* @param {string} confirmText - 确认按钮文本
*/
showWarningMessage(title = '提示', message = '', confirmText = '我知道了') {
uni.showModal({
title,
content: message,
showCancel: false,
confirmText
});
},
/**
* 显示错误消息弹窗
* @param {string} title - 弹窗标题
* @param {string} message - 消息内容
* @param {string} confirmText - 确认按钮文本
*/
showErrorMessage(title = '错误', message = '', confirmText = '我知道了') {
uni.showModal({
title,
content: message,
showCancel: false,
confirmText
});
},
/**
* 显示确认对话框带取消按钮
* @param {string} title - 弹窗标题
* @param {string} message - 消息内容
* @param {string} confirmText - 确认按钮文本
* @param {string} cancelText - 取消按钮文本
* @returns {Promise<boolean>} 用户是否点击了确认
*
* @example
* const confirmed = await DialogUtils.showConfirm('确认删除', '确定要删除这条记录吗?');
* if (confirmed) {
* // 执行删除操作
* }
*/
async showConfirm(
title = '确认',
message = '',
confirmText = '确定',
cancelText = '取消'
) {
return new Promise((resolve) => {
uni.showModal({
title,
content: message,
showCancel: true,
confirmText,
cancelText,
success: (res) => {
resolve(res.confirm);
},
fail: () => {
resolve(false);
}
});
});
},
/**
* 显示 Toast 提示
* @param {string} message - 提示消息
* @param {string} icon - 图标类型 (success/error/loading/none)
* @param {number} duration - 显示时长毫秒默认 2000
*
* @example
* // 显示加载中提示
* DialogUtils.toast('加载中...', 'loading');
*
* // 显示成功提示2秒后自动隐藏
* DialogUtils.toast('保存成功', 'success');
*/
toast(message, icon = 'none', duration = 2000) {
uni.showToast({
title: message,
icon,
duration,
mask: false
});
},
/**
* 隐藏 Toast 提示
*
* @example
* DialogUtils.toast('加载中...', 'loading', 0);
* // ... 执行某些操作
* DialogUtils.hideToast();
*/
hideToast() {
uni.hideToast();
}
};