--- title: 通知模块 | 用户通知的发送与管理 - MengStack官方文档 --- # 通知模块 通知模块提供用户级别的消息推送和管理能力,支持多种通知类型和已读状态管理。 ## 数据模型 ```go type Notification struct { ID uint // 自增主键 TenantID string // 租户 ID UserID uint // 接收用户 ID Title string // 通知标题 Content string // 通知内容 Type string // 通知类型(info / warning / error / success) IsRead bool // 是否已读 CreatedAt time.Time UpdatedAt time.Time } ``` ## 通知类型 | 类型 | 用途 | 建议样式 | |------|------|----------| | `info` | 一般信息通知 | 蓝色图标 | | `success` | 操作成功通知 | 绿色图标 | | `warning` | 警告通知 | 黄色图标 | | `error` | 错误通知 | 红色图标 | ## 核心能力 | 能力 | 说明 | |------|------| | 发送通知 | 向指定用户创建通知 | | 查询通知 | 按用户分页查询通知列表 | | 标记已读 | 单条或全部标记为已读 | | 未读计数 | 获取用户未读通知数量 | | 删除通知 | 删除指定通知 | ## 使用方式 ### 发送通知 ```bash curl -X POST http://localhost:2222/api/v1/notifications \ -H "Authorization: Bearer " \ -H "X-Tenant-ID: " \ -H "Content-Type: application/json" \ -d '{ "user_id": 1, "title": "欢迎加入", "content": "您的账号已成功创建,请完善个人资料。", "type": "info" }' ``` ### 获取用户通知列表 ```bash curl "http://localhost:2222/api/v1/notifications/user/1?page=1&page_size=20" \ -H "Authorization: Bearer " \ -H "X-Tenant-ID: " ``` ### 获取未读数量 ```bash curl http://localhost:2222/api/v1/notifications/unread-count \ -H "Authorization: Bearer " \ -H "X-Tenant-ID: " ``` 响应: ```json { "code": 0, "message": "success", "data": { "count": 5 } } ``` ### 全部标记已读 ```bash curl -X PUT http://localhost:2222/api/v1/notifications/read-all \ -H "Authorization: Bearer " \ -H "X-Tenant-ID: " ``` ## 扩展方向 当前通知模块为基础版本,后续可扩展: - WebSocket 实时推送 - 通知模板引擎 - 邮件 / 短信多渠道通知 - 通知分组与标签 - 定时通知与批量发送 ## API 端点 | 方法 | 路径 | 说明 | |------|------|------| | POST | `/api/v1/notifications` | 创建通知 | | GET | `/api/v1/notifications/:id` | 获取通知详情 | | GET | `/api/v1/notifications/user/:userId` | 获取用户通知列表 | | PUT | `/api/v1/notifications/:id/read` | 标记通知已读 | | PUT | `/api/v1/notifications/read-all` | 全部标记已读 | | GET | `/api/v1/notifications/unread-count` | 获取未读数量 | | DELETE | `/api/v1/notifications/:id` | 删除通知 | 详细接口参数见 [通知 API 参考](/api/notification)。