peixue-dev/Archive/[一次性]消息通知功能修复总结.md

8.1 KiB
Raw Blame History

消息通知功能修复总结

📋 问题描述

  1. 陪伴员消息通知页面只显示铃铛图标,没有显示标题和内容
  2. 管理师在"提醒管理"发送提醒后,陪伴员无法收到或显示不正确
  3. 未读消息数量显示不对

🔍 问题根本原因

前端数据解析错误

后端返回的是MyBatis-Plus的Page<Notification>分页对象:

{
  "code": 200,
  "data": {
    "records": [
      { "id": 1, "title": "提醒", "content": "内容", ... }
    ],
    "total": 10,
    "pages": 1,
    "current": 1
  }
}

但前端代码直接使用res.data,没有提取records数组:

// ❌ 错误的做法
const list = res.data || res || []
// list = { records: [...], total: 10 } ← 这是一个对象,不是数组!

导致前端无法正确遍历和显示通知列表。

修复方案

1. 修复前端数据解析逻辑

文件: peidu/uniapp/src/common-package/pages/notification/list.vue

修改内容:

(1) loadList方法 - 正确解析分页数据

async loadList() {
  if (this.loading) return
  
  this.loading = true
  try {
    const type = this.currentTab === 'all' ? null : this.currentTab
    const params = {
      page: this.page,
      pageSize: 10
    }
    
    if (type) {
      params.type = type
    }
    
    console.log('=== 开始加载通知列表 ===')
    console.log('请求参数:', params)
    
    const res = await notificationApi.getList(params)
    
    console.log('API返回原始数据:', res)
    console.log('res.data类型:', typeof res.data)
    console.log('res.data内容:', res.data)
    
    // ✅ 正确解析分页数据
    let list = []
    if (res.data && res.data.records) {
      // MyBatis-Plus分页格式
      list = res.data.records
      console.log('从records中提取数据数量:', list.length)
    } else if (Array.isArray(res.data)) {
      // 直接返回数组
      list = res.data
      console.log('直接使用data数组数量:', list.length)
    } else if (Array.isArray(res)) {
      // 兼容旧格式
      list = res
      console.log('直接使用res数组数量:', list.length)
    } else {
      console.warn('未识别的数据格式:', res)
    }
    
    console.log('解析后的列表:', list)
    
    if (this.page === 1) {
      this.list = list
    } else {
      this.list.push(...list)
    }
    
    this.hasMore = list.length >= 10
    
    console.log('=== 加载完成,当前列表数量:', this.list.length, '===')
  } catch (error) {
    console.error('=== 加载通知列表失败 ===')
    console.error('错误信息:', error)
    uni.showToast({ title: '加载失败', icon: 'none' })
  } finally {
    this.loading = false
  }
}

(2) loadUnreadCount方法 - 正确解析未读数量

async loadUnreadCount() {
  try {
    const res = await notificationApi.getUnreadCount()
    console.log('未读数量API返回:', res)
    
    // ✅ 正确解析未读数量
    if (res.data && typeof res.data === 'object') {
      // 后端返回 { total: 5, all: 5, order: 2, ... }
      this.unreadCount = res.data.total || res.data.all || 0
      console.log('未读数量:', this.unreadCount)
    } else if (typeof res.data === 'number') {
      this.unreadCount = res.data
    } else if (typeof res === 'number') {
      this.unreadCount = res
    } else {
      this.unreadCount = 0
    }
  } catch (error) {
    console.error('获取未读数量失败', error)
  }
}

2. 添加调试日志

在关键位置添加了详细的日志输出,方便排查问题:

  • 请求参数
  • API返回的原始数据
  • 数据类型和内容
  • 解析过程
  • 最终结果

📝 测试步骤

步骤1重新编译前端

cd peidu/uniapp
npm run dev:mp-weixin

步骤2测试管理师发送提醒

  1. 登录管理师端
  2. 进入"提醒管理"页面
  3. 创建一个新提醒(例如:"上课提醒"
  4. 点击"发送"按钮
  5. 确认提示"发送成功"

步骤3检查数据库

运行SQL查询Archive/[一次性]检查消息通知数据.sql

检查内容:

  • notification表中是否有新记录
  • user_id字段是否正确
  • title和content字段是否有值

步骤4测试陪伴员接收

  1. 登录陪伴员端(使用微信开发者工具)
  2. 打开"消息通知"页面
  3. 打开控制台查看日志输出
  4. 确认以下内容:
    • API返回的数据格式
    • records数组是否正确提取
    • 列表是否正确显示标题和内容
    • 未读数量是否正确

步骤5验证功能

  • 消息列表显示标题和内容
  • 消息列表显示时间
  • 未读消息显示红点
  • 未读数量显示正确
  • 点击消息可以标记为已读
  • 全部标记为已读功能正常

🎯 预期结果

修复后,陪伴员的消息通知页面应该正常显示:

┌─────────────────────────────────┐
│  全部(5)  服务  反馈  系统      │
├─────────────────────────────────┤
│ 🔔  上课提醒              ● 未读│
│     请准备今天的课程内容         │
│     5分钟前                      │
├─────────────────────────────────┤
│ 📝  服务反馈                     │
│     家长对您的服务提出疑问       │
│     1小时前                      │
├─────────────────────────────────┤
│ 📋  订单通知                     │
│     您有新的订单待处理           │
│     昨天 14:30                   │
└─────────────────────────────────┘

⚠️ 潜在问题和后续优化

问题1陪伴员userId可能为空

位置: ManagerReminderController.sendReminder()

问题代码:

for (Teacher teacher : teachers) {
    if (teacher.getUserId() != null) {
        userIds.add(teacher.getUserId());
    } else if (teacher.getId() != null) {
        userIds.add(teacher.getId());  // ⚠️ 可能导致消息发送给错误的用户
    }
}

建议修复:

for (Teacher teacher : teachers) {
    if (teacher.getUserId() != null) {
        userIds.add(teacher.getUserId());
    } else {
        log.warn("陪伴员ID={}没有关联的userId跳过发送", teacher.getId());
    }
}

问题2NotificationController默认返回userId=1

位置: NotificationController.getCurrentUserId()

问题代码:

private Long getCurrentUserId(HttpServletRequest request) {
    Object userId = request.getAttribute("userId");
    if (userId != null) {
        return Long.valueOf(userId.toString());
    }
    // 默认返回测试用户ID
    return 1L;  // ⚠️ 可能导致所有用户看到userId=1的消息
}

建议: 确保登录拦截器正确设置了userId避免使用默认值。

优化建议

  1. 统一数据格式

    • 确保所有API返回统一的Result格式
    • 前端统一处理分页数据
  2. 添加错误处理

    • 数据格式不正确时给出明确提示
    • 添加数据验证逻辑
  3. 优化用户体验

    • 添加下拉刷新功能
    • 添加消息删除功能
    • 优化加载状态显示
  4. 添加实时推送

    • 使用WebSocket实现实时消息推送
    • 新消息到达时显示提示

📊 修改文件清单

文件 修改内容 状态
peidu/uniapp/src/common-package/pages/notification/list.vue 修复loadList和loadUnreadCount方法的数据解析逻辑 已完成
Archive/[一次性]消息通知功能问题诊断.md 更新诊断文档 已完成
Archive/[一次性]检查消息通知数据.sql 创建数据检查SQL 已完成
Archive/[一次性]消息通知功能修复总结.md 创建修复总结文档 已完成

修复完成时间: 2026-01-26

修复人员: Kiro AI

测试状态: 待用户测试验证