export const showToast = (message: string, type: 'success' | 'error' | 'loading' = 'success', showIcon: boolean = false) => {
// 创建提示元素
const toast = document.createElement('div');
toast.className = `fixed inset-0 flex items-center justify-center z-50`;
// 创建遮罩层
const overlay = document.createElement('div');
overlay.className = 'fixed inset-0';
// 创建内容框
const content = document.createElement('div');
content.className = `relative bg-black/50 rounded-lg shadow-lg text-white z-50 min-w-[160px] max-w-[80%] flex flex-col items-center ${showIcon ? 'p-4 w-[160px] h-[160px]' : 'py-4 px-6'} ${showIcon ? 'gap-2' : ''}`;
if (showIcon) {
// 创建图标
const icon = document.createElement('div');
icon.className = 'w-10 h-10 flex items-center justify-center mt-4';
if (type === 'loading') {
icon.innerHTML = ``;
} else if (type === 'success') {
icon.innerHTML = ``;
} else {
icon.innerHTML = ``;
}
content.appendChild(icon);
}
// 创建消息
const messageElement = document.createElement('p');
messageElement.className = `text-sm text-center ${showIcon ? 'mt-2' : ''}`;
messageElement.textContent = message;
// 组装元素
content.appendChild(messageElement);
toast.appendChild(overlay);
toast.appendChild(content);
// 添加到页面
document.body.appendChild(toast);
// 如果不是loading状态,1.5秒后自动移除
if (type !== 'loading') {
setTimeout(() => {
toast.remove();
}, 1500);
}
// 返回toast元素,方便手动移除
return toast;
};