95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
// 定义对话框选项接口
|
|
interface BaseDialogOptions {
|
|
title: string;
|
|
message: string;
|
|
confirmText?: string;
|
|
cancelText?: string;
|
|
confirmColor?: string;
|
|
}
|
|
|
|
interface ConfirmDialogOptions extends BaseDialogOptions { }
|
|
|
|
interface MessageDialogOptions extends BaseDialogOptions {
|
|
dialogColor?: string;
|
|
}
|
|
|
|
// 全局对话框管理器接口
|
|
interface GlobalDialogManager {
|
|
showConfirm: (options: ConfirmDialogOptions) => Promise<boolean>;
|
|
showMessage: (options: MessageDialogOptions) => Promise<void>;
|
|
}
|
|
|
|
// 确认对话框服务
|
|
export const ConfirmDialogService = {
|
|
// 显示确认对话框
|
|
async confirm(options: ConfirmDialogOptions): Promise<boolean> {
|
|
if (window.ConfirmDialogManager) {
|
|
return (window.ConfirmDialogManager as GlobalDialogManager).showConfirm(options);
|
|
} else {
|
|
console.error('GlobalDialog component not properly initialized');
|
|
// 降级到原生 confirm
|
|
return confirm(`${options.title}: ${options.message}`);
|
|
}
|
|
},
|
|
|
|
// 显示信息对话框
|
|
async info(options: MessageDialogOptions): Promise<void> {
|
|
if (window.ConfirmDialogManager) {
|
|
return (window.ConfirmDialogManager as GlobalDialogManager).showMessage({
|
|
...options,
|
|
dialogColor: options.dialogColor || 'info',
|
|
confirmColor: options.confirmColor || 'white',
|
|
});
|
|
} else {
|
|
console.error('GlobalDialog component not properly initialized');
|
|
// 降级到原生 alert
|
|
alert(`${options.title}: ${options.message}`);
|
|
}
|
|
},
|
|
|
|
// 显示成功对话框
|
|
async success(options: MessageDialogOptions): Promise<void> {
|
|
if (window.ConfirmDialogManager) {
|
|
return (window.ConfirmDialogManager as GlobalDialogManager).showMessage({
|
|
...options,
|
|
dialogColor: options.dialogColor || 'success',
|
|
confirmColor: options.confirmColor || 'white',
|
|
});
|
|
} else {
|
|
console.error('GlobalDialog component not properly initialized');
|
|
// 降级到原生 alert
|
|
alert(`${options.title}: ${options.message}`);
|
|
}
|
|
},
|
|
|
|
// 显示警告对话框
|
|
async warning(options: MessageDialogOptions): Promise<void> {
|
|
if (window.ConfirmDialogManager) {
|
|
return (window.ConfirmDialogManager as GlobalDialogManager).showMessage({
|
|
...options,
|
|
dialogColor: options.dialogColor || 'warning',
|
|
confirmColor: options.confirmColor || 'white',
|
|
});
|
|
} else {
|
|
console.error('GlobalDialog component not properly initialized');
|
|
// 降级到原生 alert
|
|
alert(`${options.title}: ${options.message}`);
|
|
}
|
|
},
|
|
|
|
// 显示错误对话框
|
|
async error(options: MessageDialogOptions): Promise<void> {
|
|
if (window.ConfirmDialogManager) {
|
|
return (window.ConfirmDialogManager as GlobalDialogManager).showMessage({
|
|
...options,
|
|
dialogColor: options.dialogColor || 'error',
|
|
confirmColor: options.confirmColor || 'white',
|
|
});
|
|
} else {
|
|
console.error('GlobalDialog component not properly initialized');
|
|
// 降级到原生 alert
|
|
alert(`${options.title}: ${options.message}`);
|
|
}
|
|
},
|
|
};
|