消息
This commit is contained in:
102
Web/src/extends/MessageExtend.ts
Normal file
102
Web/src/extends/MessageExtend.ts
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* 消息提示工具
|
||||
*/
|
||||
export class MessageExtend {
|
||||
// 消息通知
|
||||
static notify(type: 'primary' | 'success' | 'danger' | 'warning', message: any) {
|
||||
showNotify({ type, message })
|
||||
}
|
||||
|
||||
// 提示弹窗
|
||||
static dialog(
|
||||
title: string,
|
||||
message: string,
|
||||
theme: 'round-button' | 'default',
|
||||
onConfirm?: () => void,
|
||||
onCancel?: () => void,
|
||||
confirmButtonText: string = '确认',
|
||||
) {
|
||||
showConfirmDialog({
|
||||
title: title,
|
||||
message: message,
|
||||
theme: theme || 'default',
|
||||
confirmButtonText: confirmButtonText || '确认',
|
||||
})
|
||||
.then(() => {
|
||||
onConfirm?.()
|
||||
})
|
||||
.catch(() => {
|
||||
onCancel?.()
|
||||
})
|
||||
}
|
||||
|
||||
// 成功失败默认提示
|
||||
static showToast(type: 'success' | 'fail' | 'default', text: any) {
|
||||
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(type: 'primary' | 'success' | 'danger' | 'warning', messages: string[], 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()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user