mengstack-website/guide/notification.md
MengStack Dev fca3326e34 docs: 官网文档全面更新 — M2-M8 指南 + API 参考 + v0.2.0 更新日志
- 更新 changelog.md: v0.2.0 记录 M2-M8 全部完成,路线图更新
- 新增 5 篇指南: RBAC、组织管理、审计日志、通知模块、质量横切
- 新增 5 篇 API 文档: RBAC(11)、Org(5)、Audit(1)、Settings(7)、Notification(7)
- 更新 api/overview.md: 完整 37 端点列表
- 更新 config.ts: 侧边栏分组 + 11 页 SEO 元数据
2026-10-03 01:55:55 +08:00

120 lines
2.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
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 <token>" \
-H "X-Tenant-ID: <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 <token>" \
-H "X-Tenant-ID: <tenant_id>"
```
### 获取未读数量
```bash
curl http://localhost:2222/api/v1/notifications/unread-count \
-H "Authorization: Bearer <token>" \
-H "X-Tenant-ID: <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 <token>" \
-H "X-Tenant-ID: <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)。