95 lines
2.9 KiB
JavaScript
95 lines
2.9 KiB
JavaScript
import {
|
||
CheckCircleOutlined,
|
||
CloseCircleOutlined,
|
||
ExclamationCircleOutlined,
|
||
InfoCircleOutlined,
|
||
} from '@ant-design/icons'
|
||
import { getFeedbackApi } from '@/components/Feedback/feedbackApi'
|
||
|
||
/**
|
||
* 标准反馈组件(全站唯一入口)
|
||
*
|
||
* - 通知统一从右上角滑出,默认 3 秒后消失(配置见 App.jsx 的 <App notification>)
|
||
* - 内部走 AntD App 上下文实例,因此自动继承明暗主题与圆角等设计规范
|
||
* - 二次确认统一用 Toast.confirm(),不要再用 Modal.confirm 静态方法
|
||
*/
|
||
|
||
let muteErrorsUntil = 0
|
||
|
||
function shouldMuteError() {
|
||
return Date.now() < muteErrorsUntil
|
||
}
|
||
|
||
const notificationStyle = {
|
||
borderRadius: 'var(--radius-md)',
|
||
boxShadow: 'var(--shadow-3)',
|
||
}
|
||
|
||
const ICONS = {
|
||
success: <CheckCircleOutlined style={{ color: 'var(--color-success)' }} />,
|
||
error: <CloseCircleOutlined style={{ color: 'var(--color-danger)' }} />,
|
||
warning: <ExclamationCircleOutlined style={{ color: 'var(--color-warning)' }} />,
|
||
info: <InfoCircleOutlined style={{ color: 'var(--color-primary)' }} />,
|
||
}
|
||
|
||
const open = (type, message, description, duration) => {
|
||
getFeedbackApi().notification[type]({
|
||
message,
|
||
description,
|
||
duration,
|
||
icon: ICONS[type],
|
||
style: notificationStyle,
|
||
})
|
||
}
|
||
|
||
const Toast = {
|
||
/** 短时间内抑制错误/警告提示,用于批量操作失败刷屏的场景 */
|
||
muteErrors: (durationMs = 2000) => {
|
||
muteErrorsUntil = Date.now() + durationMs
|
||
},
|
||
|
||
/** 成功通知 @param {string} message @param {string} [description] @param {number} [duration] 秒 */
|
||
success: (message, description = '', duration = 3) => open('success', message, description, duration),
|
||
|
||
/** 错误通知(默认受 muteErrors 抑制) */
|
||
error: (message, description = '', duration = 3, options = {}) => {
|
||
if (!options.force && shouldMuteError()) return
|
||
open('error', message, description, duration)
|
||
},
|
||
|
||
/** 警告通知(默认受 muteErrors 抑制) */
|
||
warning: (message, description = '', duration = 3, options = {}) => {
|
||
if (!options.force && shouldMuteError()) return
|
||
open('warning', message, description, duration)
|
||
},
|
||
|
||
/** 信息通知 */
|
||
info: (message, description = '', duration = 3) => open('info', message, description, duration),
|
||
|
||
/** 自定义通知(透传 AntD notification 配置) */
|
||
custom: (config) => {
|
||
getFeedbackApi().notification.open({
|
||
...config,
|
||
style: { ...notificationStyle, ...config.style },
|
||
})
|
||
},
|
||
|
||
/** 轻提示(顶部居中的 message),用于无需用户处理的即时反馈 */
|
||
toast: (content, type = 'info', duration = 2) => {
|
||
getFeedbackApi().message[type](content, duration)
|
||
},
|
||
|
||
/**
|
||
* 二次确认对话框(危险操作请传 okButtonProps={{ danger: true }})
|
||
* @returns AntD ModalConfirmHandler
|
||
*/
|
||
confirm: (config) =>
|
||
getFeedbackApi().modal.confirm({
|
||
okText: '确定',
|
||
cancelText: '取消',
|
||
...config,
|
||
}),
|
||
}
|
||
|
||
export default Toast
|