/** * 全局Toast工具函数 * * 使用方式: * import { Toast } from '@/utils/toast.utils'; * * Toast.success('操作成功!'); * Toast.error('操作失败!'); * Toast.warning('注意!'); */ import { ToastContextType } from '@/contexts/ToastContext'; let toastRef: ToastContextType | null = null; export const setToastRef = (ref: ToastContextType) => { toastRef = ref; }; export const Toast = { success: (message: string, duration?: number) => { if (toastRef) { toastRef.showSuccess(message, duration); } else { console.warn('Toast not initialized. Please wrap your app with ToastProvider'); } }, error: (message: string, duration?: number) => { if (toastRef) { toastRef.showError(message, duration); } else { console.warn('Toast not initialized. Please wrap your app with ToastProvider'); } }, warning: (message: string, duration?: number) => { if (toastRef) { toastRef.showWarning(message, duration); } else { console.warn('Toast not initialized. Please wrap your app with ToastProvider'); } }, show: (config: { message: string; duration?: number; backgroundColor?: string; textColor?: string; icon?: string; }) => { if (toastRef) { toastRef.showToast(config); } else { console.warn('Toast not initialized. Please wrap your app with ToastProvider'); } }, };