正在加载文档...
文档内容较大,正在处理中,请稍候
正在加载文档...
文档内容较大,正在处理中,请稍候
消息管理系统是一个基于 React Hooks 的全局消息和弹窗通知机制,提供了统一的 Message、Modal 和 Notification 显示功能。系统通过 useNotifier Hook 封装了 Ant Design 的消息通知组件,支持消息队列管理,确保消息按顺序发送。
┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐
│ 消息发送请求 │──────▶│ useNotifier │──────▶│ 消息队列管理 │
└─────────────────┘ │ (消息通知器) │ └─────────────────┘
│ │
│ ▼
│ ┌─────────────────┐
│ │ Ant Design │
│ │ Message/Modal │
│ │ Notification │
│ └─────────────────┘
│
▼
┌─────────────────┐
│ 事件总线集成 │
│ (可选) │
└─────────────────┘export const MESSAGE_TYPES = {
SUCCESS: "success", // 成功消息
ERROR: "error", // 错误消息
WARNING: "warning", // 警告消息
INFO: "info", // 信息消息
LOADING: "loading", // 加载状态
};export const MODAL_TYPES = {
INFO: "info", // 信息弹窗
SUCCESS: "success", // 成功弹窗
ERROR: "error", // 错误弹窗
WARNING: "warning", // 警告弹窗
CONFIRM: "confirm", // 确认弹窗
};export const NOTIFICATION_TYPES = {
SUCCESS: "success", // 成功通知
ERROR: "error", // 错误通知
WARNING: "warning", // 警告通知
INFO: "info", // 信息通知
LOADING: "loading", // 加载通知
};import { message, Modal, notification } from "antd";
import { useNotifier } from "@/hooks/useNotifier";
const MyComponent = () => {
const [messageApi, messageContextHolder] = message.useMessage();
const [modalApi, modalContextHolder] = Modal.useModal();
const [notificationApi, notificationContextHolder] = notification.useNotification();
const {
// 消息相关
sendMessage,
success,
error,
warning,
info,
loading,
// 弹窗相关
sendModal,
modalInfo,
modalSuccess,
modalError,
modalWarning,
modalConfirm,
// 通知相关
sendNotification,
notificationSuccess,
notificationError,
notificationWarning,
notificationInfo,
} = useNotifier(messageApi, modalApi, notificationApi);
return (
<>
{messageContextHolder}
{modalContextHolder}
{notificationContextHolder}
{/* 你的组件内容 */}
</>
);
};| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| sendMessage | 发送全局消息 | type: 消息类型content: 消息内容options: 配置项 | 隐藏消息的函数 |
| success | 发送成功消息 | content: 消息内容options: 配置项 | 隐藏消息的函数 |
| error | 发送错误消息 | content: 消息内容options: 配置项 | 隐藏消息的函数 |
| warning | 发送警告消息 | content: 消息内容options: 配置项 | 隐藏消息的函数 |
| info | 发送信息消息 | content: 消息内容options: 配置项 | 隐藏消息的函数 |
| loading | 发送加载状态 | content: 消息内容options: 配置项 | 隐藏消息的函数 |
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| sendModal | 发送弹窗 | type: 弹窗类型title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| modalInfo | 发送信息弹窗 | title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| modalSuccess | 发送成功弹窗 | title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| modalError | 发送错误弹窗 | title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| modalWarning | 发送警告弹窗 | title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| modalConfirm | 发送确认弹窗 | title: 弹窗标题content: 弹窗内容options: 配置项 | 无 |
| 方法名 | 说明 | 参数 | 返回值 |
|---|---|---|---|
| sendNotification | 发送桌面通知 | type: 通知类型title: 标题description: 描述options: 配置项 | 无 |
| notificationSuccess | 发送成功通知 | title: 标题description: 描述options: 配置项 | 无 |
| notificationError | 发送错误通知 | title: 标题description: 描述options: 配置项 | 无 |
| notificationWarning | 发送警告通知 | title: 标题description: 描述options: 配置项 | 无 |
| notificationInfo | 发送信息通知 | title: 标题description: 描述options: 配置项 | 无 |
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| duration | number | 3 | 消息持续时间(秒) |
| key | string | 无 | 消息唯一标识 |
| onClose | function | 无 | 消息关闭回调 |
| className | string | 无 | 自定义类名 |
| style | object | 无 | 自定义样式 |
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| width | number | 520 | 弹窗宽度 |
| okText | string | "确定" | 确认按钮文本 |
| cancelText | string | "取消" | 取消按钮文本 |
| okType | string | "primary" | 确认按钮类型 |
| onOk | function | 无 | 确认回调 |
| onCancel | function | 无 | 取消回调 |
| className | string | 无 | 自定义类名 |
| ...其他 | - | - | 支持所有 Ant Design Modal 配置项 |
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| duration | number | 3 | 通知持续时间(秒) |
| key | string | 无 | 通知唯一标识 |
| onClose | function | 无 | 通知关闭回调 |
| className | string | 无 | 自定义类名 |
| style | object | 无 | 自定义样式 |
| showProgress | boolean | true | 是否显示进度条 |
| ...其他 | - | - | 支持所有 Ant Design Notification 配置项 |
import React from "react";
import { message, Modal, notification, Button } from "antd";
import { useNotifier } from "@/hooks/useNotifier";
const MessageExample = () => {
const [messageApi, messageContextHolder] = message.useMessage();
const [modalApi, modalContextHolder] = Modal.useModal();
const [notificationApi, notificationContextHolder] = notification.useNotification();
const { success, error, warning, info, loading } = useNotifier(
messageApi,
modalApi,
notificationApi
);
return (
<>
{messageContextHolder}
{modalContextHolder}
{notificationContextHolder}
<div>
<Button onClick={() => success("操作成功!")}>成功消息</Button>
<Button onClick={() => error("操作失败!")}>错误消息</Button>
<Button onClick={() => warning("警告信息")}>警告消息</Button>
<Button onClick={() => info("提示信息")}>信息消息</Button>
</div>
</>
);
};import React from "react";
import { Button } from "antd";
import { useGlobalMessageContext } from "@/contexts/GlobalMessageContext";
const MessageExample = () => {
const { success, error, warning, info } = useGlobalMessageContext();
return (
<div>
<Button onClick={() => success("操作成功!")}>成功消息</Button>
<Button onClick={() => error("操作失败!")}>错误消息</Button>
<Button onClick={() => warning("警告信息")}>警告消息</Button>
<Button onClick={() => info("提示信息")}>信息消息</Button>
</div>
);
};const { success, error, warning, info } = useGlobalMessageContext();
// 成功消息
success("数据保存成功!");
// 错误消息
error("保存失败,请重试");
// 警告消息
warning("请注意数据格式");
// 信息消息
info("操作已完成");const { success, error } = useGlobalMessageContext();
// 自定义持续时间
success("操作成功", { duration: 5 });
// 自定义样式
error("错误信息", {
duration: 0, // 不自动关闭
className: "custom-error-message",
style: { color: "#ff4d4f" },
});
// 添加关闭回调
success("操作成功", {
onClose: () => {
console.log("消息已关闭");
},
});const { loading } = useGlobalMessageContext();
const handleAsyncOperation = async () => {
// 显示加载消息
const hide = loading("正在处理...");
try {
await someAsyncOperation();
hide(); // 手动关闭加载消息
success("处理完成");
} catch (error) {
hide();
error("处理失败");
}
};const { modalInfo, modalSuccess, modalError, modalWarning, modalConfirm } =
useGlobalMessageContext();
// 信息弹窗
modalInfo("提示", "这是一个信息弹窗");
// 成功弹窗
modalSuccess("成功", "操作已成功完成");
// 错误弹窗
modalError("错误", "操作失败,请检查");
// 警告弹窗
modalWarning("警告", "此操作可能会影响系统");const { modalConfirm } = useGlobalMessageContext();
const handleDelete = () => {
modalConfirm("确认删除", "确定要删除这个项目吗?此操作不可恢复。", {
okText: "确认删除",
cancelText: "取消",
okType: "danger",
onOk: async () => {
try {
await deleteItem();
success("删除成功");
} catch (error) {
error("删除失败");
}
},
onCancel: () => {
info("已取消删除");
},
});
};const { modalInfo } = useGlobalMessageContext();
modalInfo("详细信息", "这是弹窗内容", {
width: 800,
className: "custom-modal",
okText: "我知道了",
onOk: () => {
console.log("用户点击了确定");
},
});const { notificationSuccess, notificationError, notificationWarning, notificationInfo } =
useGlobalMessageContext();
// 成功通知
notificationSuccess("操作成功", "数据已成功保存到服务器");
// 错误通知
notificationError("操作失败", "网络连接异常,请检查后重试");
// 警告通知
notificationWarning("注意", "您的账户余额不足");
// 信息通知
notificationInfo("新消息", "您有一条新的系统消息");const { notificationInfo } = useGlobalMessageContext();
notificationInfo("系统通知", "这是一条重要的系统消息", {
duration: 10, // 10秒后自动关闭
showProgress: true, // 显示进度条
className: "custom-notification",
onClose: () => {
console.log("通知已关闭");
},
});系统自动管理消息队列,确保消息按顺序显示:
const { success } = useGlobalMessageContext();
const sendMultipleMessages = () => {
// 连续发送多条消息,系统会自动加入队列并按顺序显示
for (let i = 1; i <= 10; i++) {
success(`这是第 ${i} 条消息`);
}
};系统支持通过事件总线触发消息:
import { globalMessageUtils } from "@/hooks/useGlobalMessage";
// 通过事件发送消息
globalMessageUtils.success("操作成功");
globalMessageUtils.error("操作失败");
// 通过事件发送弹窗
globalMessageUtils.modalConfirm("确认", "确定要执行此操作吗?", {
onOk: () => {
console.log("用户确认");
},
});在应用根部添加 GlobalMessageProvider:
import React from "react";
import { GlobalMessageProvider } from "@/contexts/GlobalMessageContext";
import App from "./App";
const Root = () => {
return (
<GlobalMessageProvider>
<App />
</GlobalMessageProvider>
);
};import { useGlobalMessageContext } from "@/contexts/GlobalMessageContext";
const MyComponent = () => {
const { success, error, modalConfirm } = useGlobalMessageContext();
return (
<div>
<button onClick={() => success("成功")}>成功</button>
<button onClick={() => error("失败")}>失败</button>
</div>
);
};import { globalMessageUtils } from "@/hooks/useGlobalMessage";
// 发送消息
globalMessageUtils.success("操作成功");
globalMessageUtils.error("操作失败");
globalMessageUtils.warning("警告信息");
globalMessageUtils.info("提示信息");
// 发送弹窗
globalMessageUtils.modalInfo("提示", "这是信息弹窗");
globalMessageUtils.modalConfirm("确认", "确定要执行吗?", {
onOk: () => {
console.log("确认");
},
});import { globalMessageUtils } from "@/hooks/useGlobalMessage";
async function handleApiRequest(url, options) {
try {
globalMessageUtils.info("正在发送请求...");
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
globalMessageUtils.success("请求成功");
return data;
} catch (error) {
globalMessageUtils.error(`请求失败: ${error.message}`);
throw error;
}
}可能原因:
messageApi、modalApi 或 notificationApicontextHolder解决方法:
// 确保正确初始化
const [messageApi, messageContextHolder] = message.useMessage();
const [modalApi, modalContextHolder] = Modal.useModal();
const [notificationApi, notificationContextHolder] = notification.useNotification();
// 确保在组件中渲染 contextHolder
return (
<>
{messageContextHolder}
{modalContextHolder}
{notificationContextHolder}
{/* 组件内容 */}
</>
);解决方法:系统已经实现了消息队列,确保消息按顺序显示。如果遇到问题,检查是否有多个消息实例。
可能原因:弹窗配置中缺少关闭逻辑
解决方法:
modalConfirm("确认", "确定吗?", {
onOk: () => {
// 处理确认逻辑
},
onCancel: () => {
// 处理取消逻辑(可选)
},
});| 版本 | 日期 | 变更内容 |
|---|---|---|
| 2.0.0 | 2025-12-05 | 架构重构版本 |
| - 独立的消息通知 Hook | ||
| - 新增桌面通知(Notification)支持 | ||
| - 优化消息队列管理 | ||
| - 与事件总线集成 | ||
| 1.0.0 | 2025-12-02 | 初始版本发布 |
| - 实现基础消息功能 | ||
| - 实现弹窗功能 | ||
| - 支持消息队列 |
文档作者: lyqjob@yeah.net 更新日期: 2025-12-05 版本: 2.0.0