/** * 全局弹窗调用工具 * @param {Object} options - 配置项 (title, content, type, success, fail, confirmColor...) */ export const $confirm = (options = {}) => { return new Promise((resolve) => { // 1. 稍微延迟发送,确保注入的页面组件已完成 uni.$on 监听挂载 // 如果是用户点击触发,延迟可以设为 0;如果是拦截器自动触发,建议保留 200ms setTimeout(() => { uni.$emit('show-global-popup', { title: options.title || '提示', content: options.content || '', type: options.type || 'info', // 核心:重新包装 resolve 逻辑 resolve: (isConfirm) => { if (isConfirm) { // 执行成功回调 if (typeof options.success === 'function') { options.success({ confirm: true, cancel: false }); } resolve(true); } else { // 执行失败/取消回调 if (typeof options.fail === 'function') { options.fail({ confirm: false, cancel: true }); } resolve(false); } }, }); }, options.delay || 200); }); }; // // 页面中使用 // import { // $confirm // } from '@/utils/modal.js'; // const ok = await $confirm({ // title: '测试' // });