/**
|
* uview-plus 工具函数封装
|
* 提供统一的提示和模态框工具
|
*/
|
|
/**
|
* 使用 Toast 提示工具
|
* @returns {{showToast: function}}
|
*/
|
export const useToast = () => {
|
/**
|
* 显示提示信息
|
* @param {string} message 提示消息
|
* @param {string} [type='info'] 提示类型:'success' | 'error' | 'warning' | 'info'
|
* @param {number} [duration=2000] 显示时长(毫秒)
|
*/
|
const showToast = (message, type = 'info', duration = 2000) => {
|
// 映射 uview-plus 的图标类型
|
const iconMap = {
|
success: 'success',
|
error: 'error',
|
warning: 'warning',
|
info: 'info'
|
};
|
|
uni.showToast({
|
title: message,
|
icon: iconMap[type] || 'none',
|
duration: duration
|
});
|
};
|
|
return {
|
showToast
|
};
|
};
|
|
/**
|
* 使用 Modal 模态框工具
|
* @returns {{showModal: function}}
|
*/
|
export const useModal = () => {
|
/**
|
* 显示确认模态框
|
* @param {Object} options 配置选项
|
* @param {string} options.title 标题
|
* @param {string} options.content 内容
|
* @param {boolean} [options.showCancel=true] 是否显示取消按钮
|
* @param {string} [options.confirmText='确认'] 确认按钮文本
|
* @param {string} [options.cancelText='取消'] 取消按钮文本
|
* @returns {Promise<boolean>} 用户确认结果
|
*/
|
const showModal = (options) => {
|
return new Promise((resolve) => {
|
uni.showModal({
|
title: options.title || '提示',
|
content: options.content || '',
|
showCancel: options.showCancel !== false,
|
confirmText: options.confirmText || '确认',
|
cancelText: options.cancelText || '取消',
|
success: (res) => {
|
resolve(res.confirm);
|
},
|
fail: () => {
|
resolve(false);
|
}
|
});
|
});
|
};
|
|
return {
|
showModal
|
};
|
};
|
|
/**
|
* 默认导出
|
*/
|
export default {
|
useToast,
|
useModal
|
};
|