// composables/useFlash.js export function useFlash() { // 全局可复用状态(跨页面不丢) const state = useState('flash:state', () => ({ show: false, text: '', type: 'info', // info | success | error | warning })) const hideTimer = useState('flash:timer', () => null) function open(text, type = 'info', ms = 3000) { // 允许字符串或对象 if (typeof text === 'object' && text) { state.value = { show: true, text: text.text ?? '', type: text.type ?? 'info', } ms = text.ms ?? ms } else { state.value = { show: true, text: String(text ?? ''), type } } if (hideTimer.value) clearTimeout(hideTimer.value) hideTimer.value = setTimeout(() => { state.value.show = false hideTimer.value = null }, Math.max(0, ms)) } function close() { if (hideTimer.value) { clearTimeout(hideTimer.value) hideTimer.value = null } state.value.show = false } return { state, open, close, } }