peixue-dev/Archive/[一次性]消息通知功能问题诊断.md

228 lines
5.9 KiB
Markdown
Raw Permalink 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.

# 消息通知功能问题诊断
## 📋 问题描述
1. **陪伴员消息通知页面只显示铃铛图标,没有显示标题和内容**
2. **管理师在"提醒管理"发送提醒后,陪伴员无法收到或显示不正确**
3. **未读消息数量显示不对**
## 🔍 问题分析
### 数据流程
```
管理师发送提醒
ManagerReminderController.sendReminder()
NotificationService.createNotificationForUsers()
创建 Notification 记录到数据库
陪伴员前端调用 /api/notification/list
NotificationController.getList()
返回 Page<Notification> 数据
前端 notification/list.vue 显示
```
### 可能的原因
#### 1. 前端数据解析问题
前端代码第99-110行
```javascript
const res = await notificationApi.getList(params)
const list = res.data || res || []
```
后端返回的数据格式可能是:
```json
{
"code": 200,
"data": {
"records": [...],
"total": 10,
"pages": 1
}
}
```
但前端直接使用 `res.data``res`,没有正确提取 `records` 数组。
#### 2. 数据字段映射问题
前端期望的字段:
- `title` - 标题
- `content` - 内容
- `type` - 类型
- `isRead` - 是否已读
- `createTime` - 创建时间
后端 Notification 实体可能使用的字段:
- `title`
- `content`
- `type`
- `is_read``isRead`
- `create_time``createTime`
#### 3. userId 获取问题
后端 `getCurrentUserId()` 方法默认返回 `1L`,可能导致:
- 陪伴员看到的是用户ID=1的消息
- 实际发送给陪伴员的消息userId不匹配
## 🔧 解决方案
### ✅ 方案1修复前端数据解析已完成
**问题原因:**
后端返回的是MyBatis-Plus的`Page<Notification>`对象,包含`records`数组,但前端直接使用`res.data`,没有提取`records`字段。
**修复内容:**
修改 `common-package/pages/notification/list.vue` 的`loadList()`和`loadUnreadCount()`方法:
1. **loadList方法** - 正确解析分页数据:
```javascript
// 处理后端返回的分页数据格式
let list = []
if (res.data && res.data.records) {
// MyBatis-Plus分页格式: { records: [...], total: 10, pages: 1 }
list = res.data.records
} else if (Array.isArray(res.data)) {
// 直接返回数组
list = res.data
} else if (Array.isArray(res)) {
// 兼容旧格式
list = res
}
```
2. **loadUnreadCount方法** - 正确解析未读数量:
```javascript
// 后端返回的是 Map<String, Object>,包含 total 字段
if (res.data && typeof res.data === 'object') {
this.unreadCount = res.data.total || res.data.all || 0
} else if (typeof res.data === 'number') {
this.unreadCount = res.data
}
```
3. **添加调试日志** - 方便排查问题:
```javascript
console.log('=== 开始加载通知列表 ===')
console.log('请求参数:', params)
console.log('API返回原始数据:', res)
console.log('解析后的列表:', list)
```
### 方案2检查数据库中的通知记录待执行
**目的:**
验证管理师发送的提醒是否正确保存到数据库以及userId是否正确。
**执行步骤:**
1. 运行SQL查询文件`Archive/[一次性]检查消息通知数据.sql`
2. 检查以下内容:
- notification表中是否有新记录
- user_id字段是否正确对应陪伴员
- teacher表中的user_id是否都有值
- 是否有userId为NULL的陪伴员
**预期结果:**
- notification表中应该有记录user_id应该对应陪伴员的userId
- teacher表中audit_status=1的记录应该都有user_id
- 如果有user_id为NULL的陪伴员需要修复数据
### 方案3修复userId获取逻辑待确认
**潜在问题:**
`ManagerReminderController.sendReminder()`方法中获取陪伴员userId的逻辑
```java
for (Teacher teacher : teachers) {
if (teacher.getUserId() != null) {
userIds.add(teacher.getUserId());
} else if (teacher.getId() != null) {
userIds.add(teacher.getId()); // ⚠️ 这里可能有问题
}
}
```
当teacher.getUserId()为null时使用teacher.getId()作为userId可能导致消息发送给错误的用户。
**建议修复:**
```java
for (Teacher teacher : teachers) {
if (teacher.getUserId() != null) {
userIds.add(teacher.getUserId());
} else {
// 记录警告日志,不添加到发送列表
log.warn("陪伴员ID={}没有关联的userId跳过发送", teacher.getId());
}
}
```
### 方案4添加调试日志已完成
前端已添加详细的调试日志,可以在微信开发者工具的控制台中查看:
- 请求参数
- API返回的原始数据
- 数据解析过程
- 最终显示的列表
## 📝 测试步骤
1. **清除缓存并重新编译**
```bash
cd peidu/uniapp
npm run dev:mp-weixin
```
2. **测试管理师发送提醒**
- 登录管理师端
- 进入"提醒管理"
- 创建一个新提醒
- 点击"发送"按钮
- 查看是否提示"发送成功"
3. **检查数据库**
- 运行上面的SQL查询
- 确认notification表中有新记录
- 确认user_id字段是否正确
4. **测试陪伴员接收**
- 登录陪伴员端
- 进入"消息通知"
- 打开开发者工具查看控制台日志
- 确认API返回的数据格式
- 确认列表是否正确显示
## 🎯 预期结果
修复后,陪伴员的消息通知页面应该显示:
- ✅ 消息标题
- ✅ 消息内容
- ✅ 消息时间
- ✅ 未读标记(红点)
- ✅ 正确的未读数量
---
## 💡 后续优化建议
1. **统一数据格式**
- 确保所有API返回统一的数据格式
- 使用统一的Result包装类
2. **添加错误处理**
- 当数据格式不正确时,给出明确的错误提示
- 添加数据验证逻辑
3. **优化用户体验**
- 添加下拉刷新功能
- 添加消息已读/未读切换
- 添加消息删除功能
4. **添加实时推送**
- 使用WebSocket或轮询实现实时消息推送
- 新消息到达时显示提示