// 加载状态,提示语 import { ref } from 'vue' // 全局 loading 状态(可用于页面绑定 v-if) const isLoading = ref(false) /** * 显示 loading * @param {string} title - 提示文字(H5 支持,App 小程序部分支持) */ const showLoading = (title = '加载中...') => { isLoading.value = true // uni.showLoading 在 H5 和 App 中行为略有不同,但基本可用 uni.showLoading({ title, mask: true // 防止穿透点击 }) } /** * 隐藏 loading */ const hideLoading = () => { isLoading.value = false uni.hideLoading() } /** * 统一 Toast 提示 * @param {string} message - 提示内容 * @param {string} type - 'success' | 'error' | 'warning' | 'none' * @param {number} duration - 持续时间(毫秒) */ const showToast = (message, type = 'none', duration = 1800) => { let icon = 'none' if (type === 'success') icon = 'success' if (type === 'error') icon = 'error' if (type === 'warning') icon = 'none' return new Promise(resolve => { uni.showToast({ title: message, icon, duration, mask: true }) setTimeout(() => resolve(), duration) }) } /** 对话框带确认取消按钮 */ const showDialog = (title, content, showCancel = true) => { return new Promise(resolve => { uni.showModal({ title, content, showCancel, confirmText: '确定', cancelText: '取消', success: res => resolve(res.confirm) }) }) } /** 导出响应式状态和方法 */ export const useUI = () => { return { isLoading: isLoading, // 可用于模板中 v-if="isLoading" showLoading, hideLoading, showToast, showDialog } }