#!/usr/bin/env python3 """ 验证SSE积分通知系统修复效果 测试扫码后是否正确显示积分通知而非"0积分"错误 """ import requests import json import time from datetime import datetime # 配置 BASE_URL = "http://localhost:5511" API_TIMEOUT = 10 def log(msg): print(f"[{datetime.now().strftime('%H:%M:%S')}] {msg}") def verify_sse_notification_system(): """验证SSE通知系统修复效果""" log("=== SSE积分通知系统修复验证 ===") # 1. 会员登录 log("\n1. 使用测试账号登录") login_data = { "phone": "15921072307", "password": "Sl52788542" } try: response = requests.post( f"{BASE_URL}/api/members/login", json=login_data, timeout=API_TIMEOUT ) if response.status_code != 200: log(f"❌ 登录失败: {response.status_code}") log(f" 响应: {response.text}") return result = response.json() token = result.get("data", {}).get("token") member_id = result.get("data", {}).get("memberId") if not token or not member_id: log("❌ 无法获取认证信息") return log(f"✅ 登录成功") log(f" Member ID: {member_id}") log(f" Token: {token[:20]}...") headers = {"Authorization": f"Bearer {token}"} # 2. 检查初始积分 log("\n2. 检查初始积分状态") current_response = requests.get(f"{BASE_URL}/api/members/current", headers=headers) if current_response.status_code == 200: current_data = current_response.json() initial_points = current_data.get('availablePoints', 0) log(f"✅ 初始积分: {initial_points}") else: log(f"❌ 获取初始积分失败: {current_response.status_code}") return # 3. 测试扫码接口行为(验证修复前的问题) log("\n3. 测试扫码接口响应(验证修复效果)") scan_data = {"code": "TEST123"} scan_start_time = time.time() scan_response = requests.post( f"{BASE_URL}/api/marketing-codes/scan", json=scan_data, headers=headers, timeout=API_TIMEOUT ) scan_end_time = time.time() log(f"扫码接口响应时间: {scan_end_time - scan_start_time:.2f}秒") log(f"扫码接口状态码: {scan_response.status_code}") if scan_response.status_code == 200: scan_result = scan_response.json() earned_points = scan_result.get('data', {}).get('earnedPoints', 0) message = scan_result.get('data', {}).get('message', '') product_name = scan_result.get('data', {}).get('productName', '') log(f"✅ 扫码响应分析:") log(f" 返回积分: {earned_points}") log(f" 消息: {message}") log(f" 产品: {product_name}") # 关键验证点:扫码接口不再返回具体的积分值 if earned_points == 0 and "正在发放" in message: log("✅ 修复验证通过: 扫码接口返回正确的处理中状态") elif earned_points > 0: log("⚠️ 仍有问题: 扫码接口仍返回具体积分值") else: log("⚠️ 响应格式可能有变化,需要进一步检查") # 4. 等待并检查积分实际变化 log("\n4. 等待事件处理完成后检查实际积分变化") time.sleep(5) # 等待领域事件和积分处理完成 current_response2 = requests.get(f"{BASE_URL}/api/members/current", headers=headers) if current_response2.status_code == 200: new_data = current_response2.json() final_points = new_data.get('availablePoints', 0) points_diff = final_points - initial_points log(f"✅ 积分变化验证:") log(f" 初始积分: {initial_points}") log(f" 最终积分: {final_points}") log(f" 实际变化: {points_diff}") if points_diff > 0: log(f"✅ 积分确实增加了 {points_diff} 分") log("✅ 后台积分处理机制正常工作") else: log("⚠️ 积分没有变化,可能需要检查事件处理") # 5. 检查通知历史 log("\n5. 检查SSE通知历史") try: noti_response = requests.get( f"{BASE_URL}/api/notifications/history?pageSize=10&pageNumber=1", headers=headers, timeout=API_TIMEOUT ) if noti_response.status_code == 200: noti_data = noti_response.json() notifications = noti_data.get('data', {}).get('items', []) log(f"✅ 找到 {len(notifications)} 条通知") # 查找积分相关通知 points_notifications = [ n for n in notifications if '积分' in n.get('title', '') or '积分' in n.get('content', '') ] if points_notifications: log(f"✅ 找到 {len(points_notifications)} 条积分相关通知:") for i, noti in enumerate(points_notifications[:3]): log(f" 通知 {i+1}: {noti.get('title')} - {noti.get('content')}") log(f" 时间: {noti.get('createdAt')}") else: log("⚠️ 未找到积分相关通知") else: log(f"❌ 获取通知历史失败: {noti_response.status_code}") except Exception as e: log(f"❌ 检查通知历史异常: {e}") # 6. 验证整体流程 log("\n6. 整体流程验证总结") log("=" * 50) issues_found = [] fixes_verified = [] # 检查各个验证点 if earned_points == 0 and "正在发放" in message: fixes_verified.append("✅ 扫码接口不再返回错误的0积分值") else: issues_found.append("❌ 扫码接口仍可能返回错误积分值") if points_diff > 0: fixes_verified.append("✅ 后台积分处理机制正常工作") else: issues_found.append("❌ 积分处理可能存在异常") if points_notifications: fixes_verified.append("✅ SSE通知机制正常发送积分变动通知") else: issues_found.append("❌ SSE通知可能未正确发送") # 输出验证结果 log("修复验证结果:") for fix in fixes_verified: log(f" {fix}") if issues_found: log("\n仍需关注的问题:") for issue in issues_found: log(f" {issue}") else: log("\n🎉 所有修复验证通过!") log(f"\n总体评估: {len(fixes_verified)}/{len(fixes_verified) + len(issues_found)} 项验证通过") except Exception as e: log(f"❌ 验证过程中出现异常: {e}") def main(): """主函数""" log("开始验证SSE积分通知系统修复效果") log(f"测试时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") verify_sse_notification_system() log("\n=== 验证完成 ===") if __name__ == "__main__": main()