import { readonly, ref } from 'vue'; export type MessageLevel = 'success' | 'info' | 'warning' | 'error'; // 全局单例状态:整个应用共享一套消息弹框。 const visible = ref(false); const level = ref('info'); const title = ref(''); const message = ref(''); const MAX_DIALOG_MESSAGE_LENGTH = 240; const normalizeMessage = (raw: string) => { if (!raw) return ''; return raw .replace(/\s+/g, ' ') .replace(/<[^>]*>/g, '') .trim(); }; const formatMessageForDialog = (level: MessageLevel, raw: string) => { const normalized = normalizeMessage(raw); if (!normalized) return ''; // Error messages often include long backend stack/driver details. if (level === 'error' && normalized.length > MAX_DIALOG_MESSAGE_LENGTH) { return `${normalized.slice(0, MAX_DIALOG_MESSAGE_LENGTH)}...`; } return normalized; }; export const useGlobalMessageDialog = () => { const showMessageDialog = ( nextLevel: MessageLevel, nextMessage: string, nextTitle = '' ) => { level.value = nextLevel; title.value = nextTitle; message.value = formatMessageForDialog(nextLevel, nextMessage); visible.value = true; }; const closeMessageDialog = () => { visible.value = false; title.value = ''; message.value = ''; }; return { messageDialogVisible: visible, messageDialogLevel: readonly(level), messageDialogTitle: readonly(title), messageDialogContent: readonly(message), showMessageDialog, closeMessageDialog, }; };