Files
Kg.SeaTime/Web/src/extends/MessageExtend.ts

124 lines
3.3 KiB
TypeScript

/**
* 消息提示工具
*/
export class MessageExtend {
// 消息通知
static Notify(message: any, type?: 'primary' | 'success' | 'danger' | 'warning') {
showNotify({ type, message })
}
// 提示弹窗
static ShowDialog(title: string, message: string) {
showDialog({ title: title, message: message })
}
// 确认提示弹窗
static ShowConfirmDialog(title: string, message: string, onConfirm?: () => void, onCancel?: () => void, confirmButtonText?: string) {
showConfirmDialog({
title: title,
message: message,
confirmButtonText: confirmButtonText || '确认',
})
.then(() => {
onConfirm?.()
})
.catch(() => {
onCancel?.()
})
}
// 异步提示弹窗
static ShowDialogAsyc(title: string, message: string, onConfirm?: () => Promise<boolean>): Promise<boolean> {
return new Promise((resolve) => {
showConfirmDialog({
title,
message,
beforeClose: async (action) => {
if (action === 'confirm' && onConfirm) {
const result = await onConfirm()
if (result) {
resolve(true)
return true
}
return false
}
resolve(action === 'confirm')
return true
},
}).catch(() => {
resolve(false) // 捕获取消操作,返回 false
})
})
}
// 成功失败默认提示
static ShowToast(text: any, type?: 'success' | 'fail' | 'default') {
if (type == 'success') {
showSuccessToast(text)
} else if (type == 'fail') {
showFailToast(text)
} else {
showToast(text)
}
}
// 自定义图标提示
static ShowIconToast(icon: any, text: any) {
showToast({
message: text,
icon: icon,
})
}
// 多条消息通知(堆叠展示)
static NotifyList(messages: string[], type: 'primary' | 'success' | 'danger' | 'warning', duration: number = 3000) {
if (typeof window === 'undefined') return
if (!messages || messages.length === 0) return
const colorMap = {
primary: '#1989fa',
success: '#07c160',
danger: '#ee0a24',
warning: '#ff976a',
}
let container = document.getElementById('custom-notify-container')
if (!container) {
container = document.createElement('div')
container.id = 'custom-notify-container'
document.body.appendChild(container)
}
messages.forEach((message, index) => {
const item = document.createElement('div')
item.className = 'custom-notify-item'
item.style.backgroundColor = colorMap[type]
item.textContent = String(message)
item.style.animationDelay = `${index * 100}ms`
item.addEventListener('click', () => {
this.closeNotifyItem(item)
})
container!.appendChild(item)
setTimeout(() => {
this.closeNotifyItem(item)
}, duration)
})
}
// 关闭单条通知
private static closeNotifyItem(item: HTMLElement) {
if (!item || item.classList.contains('leaving')) return
item.classList.add('leaving')
item.addEventListener('animationend', () => {
item.remove()
const container = document.getElementById('custom-notify-container')
if (container && container.children.length === 0) {
container.remove()
}
})
}
}