// // src/plugins/toastPlugin.js // import { createApp, reactive, h } from 'vue' // import ToastHost from './errorshow.vue' // 路径按项目调整 // const defaultOpt = { // title: '', // duration: 1500, // type: 'text', // 'text' | 'dialog' // mask: true, // maskClosable: true, // success: null, // complete: null // } // function createToastService() { // const state = reactive({ // show: false, // title: '', // duration: defaultOpt.duration, // type: defaultOpt.type, // mask: defaultOpt.mask, // maskClosable: defaultOpt.maskClosable, // success: null, // complete: null, // _timer: null, // // dialog 专用(可扩展) // confirmText: '确定', // cancelText: '取消' // }) // function clearTimer() { // if (state._timer) { // clearTimeout(state._timer) // state._timer = null // } // } // function showToast(options = {}) { // const opt = Object.assign({}, defaultOpt, options) // clearTimer() // state.title = opt.title == null ? '' : String(opt.title) // state.duration = Number(opt.duration) || defaultOpt.duration // state.type = opt.type // state.mask = !!opt.mask // state.maskClosable = opt.maskClosable !== undefined ? !!opt.maskClosable : true // state.success = typeof opt.success === 'function' ? opt.success : null // state.complete = typeof opt.complete === 'function' ? opt.complete : null // state.confirmText = opt.confirmText || '确定' // state.cancelText = opt.cancelText || '取消' // state.show = true // return new Promise((resolve) => { // if (state.type === 'text') { // state._timer = setTimeout(() => { // hideToast() // if (state.complete) try { state.complete() } catch(e){/*ignore*/} // resolve() // }, state.duration) // } else { // // dialog 不自动关闭,等待按键或 maskClosable // // 但为了安全可以选择不自动超时 // // resolve 将在按钮或 mask 关闭时调用 // } // }) // } // function hideToast(by = 'complete') { // clearTimer() // state.show = false // // 执行回调 // if (by === 'success' && state.success) { // try { state.success() } catch(e) {} // } // if (state.complete) { // try { state.complete() } catch(e) {} // } // // 清理 // state.success = null // state.complete = null // state.title = '' // state.type = defaultOpt.type // } // // dialog 按钮触发 // function onConfirm() { // hideToast('success') // } // function onCancel() { // hideToast('complete') // } // return { // state, // showToast, // hideToast, // onConfirm, // onCancel // } // } // // 插件安装函数 // export default { // install: (app, options = {}) => { // const service = createToastService() // // programmatically mount the host component once // const container = document.createElement('div') // document.body.appendChild(container) // const toastApp = createApp({ // render() { // return h(ToastHost, { service }) // } // }) // toastApp.mount(container) // // 全局方法 // app.config.globalProperties.$errorshow = service.showToast // // 兼容直接使用 window.uni.showToast // if (typeof window !== 'undefined') { // window.uni = window.uni || {} // window.uni.$errorshow = service.showToast // } // // 可选:返回 service 以便调试 // app.provide('toastService', service) // } // }