From 463bdbf09cab9810034a1fe34b0e8fe94e556302 Mon Sep 17 00:00:00 2001 From: zdl <3489966805@qq.com> Date: Tue, 11 Nov 2025 11:45:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=B0=83=E8=AF=95=20?= =?UTF-8?q?API=20=20=20=20=20-=20=E6=88=91=E4=BF=AE=E6=94=B9=20Notificatio?= =?UTF-8?q?nContext.js=EF=BC=8C=E6=9A=B4=E9=9C=B2=20addNotification=20?= =?UTF-8?q?=E5=88=B0=20window=20=20=20=20=20-=20=E6=88=96=E8=80=85?= =?UTF-8?q?=E5=9C=A8=E8=B0=83=E8=AF=95=E5=B7=A5=E5=85=B7=20(devtools/notif?= =?UTF-8?q?icationDebugger.js)=20=E4=B8=AD=E6=B7=BB=E5=8A=A0=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=B9=E6=B3=95=20=20=20=20=20-=20=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E6=9E=84=E5=BB=BA=E5=B9=B6=E9=83=A8=E7=BD=B2=20=20=20?= =?UTF-8?q?=20=20-=20=E5=8F=AF=E4=BB=A5=E6=89=8B=E5=8A=A8=E8=A7=A6?= =?UTF-8?q?=E5=8F=91=E7=BD=91=E9=A1=B5=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.production | 4 +- src/contexts/NotificationContext.js | 86 ++++++++++++++++++++++++++++ src/devtools/index.js | 5 +- src/devtools/notificationDebugger.js | 38 ++++++++++++ 4 files changed, 130 insertions(+), 3 deletions(-) diff --git a/.env.production b/.env.production index 9b52a6cc..81f10c55 100644 --- a/.env.production +++ b/.env.production @@ -17,13 +17,13 @@ NODE_ENV=production REACT_APP_ENABLE_MOCK=false # 🔧 调试模式(生产环境临时调试用) -# 开启后会在全局暴露 window.__DEBUG__ 调试 API +# 开启后会在全局暴露 window.__DEBUG__ 和 window.__TEST_NOTIFICATION__ 调试 API # ⚠️ 警告: 调试模式会记录所有 API 请求/响应,调试完成后请立即关闭! # 使用方法: # 1. 设置为 true 并重新构建 # 2. 在浏览器控制台使用 window.__DEBUG__.help() 查看命令 # 3. 调试完成后设置为 false 并重新构建 -REACT_APP_ENABLE_DEBUG=false +REACT_APP_ENABLE_DEBUG=true # 后端 API 地址(生产环境) REACT_APP_API_URL=http://49.232.185.254:5001 diff --git a/src/contexts/NotificationContext.js b/src/contexts/NotificationContext.js index e2e9d55e..ac56a318 100644 --- a/src/contexts/NotificationContext.js +++ b/src/contexts/NotificationContext.js @@ -896,6 +896,92 @@ export const NotificationProvider = ({ children }) => { }; }, [browserPermission, toast]); + // 🔧 开发环境调试:暴露方法到 window + useEffect(() => { + if (process.env.NODE_ENV === 'development' || process.env.REACT_APP_ENABLE_DEBUG === 'true') { + if (typeof window !== 'undefined') { + window.__TEST_NOTIFICATION__ = { + // 手动触发网页通知 + testWebNotification: (type = 'event_alert', priority = 'normal') => { + console.log('%c[Debug] 手动触发网页通知', 'color: #FF9800; font-weight: bold;'); + + const testData = { + id: `test_${Date.now()}`, + type: type, + priority: priority, + title: '🧪 测试网页通知', + content: `这是一条测试${type === 'announcement' ? '公告' : type === 'stock_alert' ? '股票' : type === 'event_alert' ? '事件' : '分析'}通知 (优先级: ${priority})`, + timestamp: Date.now(), + clickable: true, + link: '/home', + }; + + console.log('测试数据:', testData); + addNotification(testData); + console.log('✅ 通知已添加到队列'); + }, + + // 测试所有类型 + testAllTypes: () => { + console.log('%c[Debug] 测试所有通知类型', 'color: #FF9800; font-weight: bold;'); + const types = ['announcement', 'stock_alert', 'event_alert', 'analysis_report']; + types.forEach((type, i) => { + setTimeout(() => { + window.__TEST_NOTIFICATION__.testWebNotification(type, 'normal'); + }, i * 2000); // 每 2 秒一个 + }); + }, + + // 测试所有优先级 + testAllPriorities: () => { + console.log('%c[Debug] 测试所有优先级', 'color: #FF9800; font-weight: bold;'); + const priorities = ['normal', 'important', 'urgent']; + priorities.forEach((priority, i) => { + setTimeout(() => { + window.__TEST_NOTIFICATION__.testWebNotification('event_alert', priority); + }, i * 2000); + }); + }, + + // 帮助 + help: () => { + console.log('\n%c=== 网页通知测试 API ===', 'color: #FF9800; font-weight: bold; font-size: 16px;'); + console.log('\n%c基础用法:', 'color: #2196F3; font-weight: bold;'); + console.log(' window.__TEST_NOTIFICATION__.testWebNotification(type, priority)'); + console.log('\n%c参数说明:', 'color: #2196F3; font-weight: bold;'); + console.log(' type (通知类型):'); + console.log(' - "announcement" 公告通知(蓝色)'); + console.log(' - "stock_alert" 股票动向(红色/绿色)'); + console.log(' - "event_alert" 事件动向(橙色)'); + console.log(' - "analysis_report" 分析报告(紫色)'); + console.log('\n priority (优先级):'); + console.log(' - "normal" 普通(15秒自动关闭)'); + console.log(' - "important" 重要(30秒自动关闭)'); + console.log(' - "urgent" 紧急(不自动关闭)'); + console.log('\n%c示例:', 'color: #4CAF50; font-weight: bold;'); + console.log(' // 测试紧急事件通知'); + console.log(' window.__TEST_NOTIFICATION__.testWebNotification("event_alert", "urgent")'); + console.log('\n // 测试所有类型'); + console.log(' window.__TEST_NOTIFICATION__.testAllTypes()'); + console.log('\n // 测试所有优先级'); + console.log(' window.__TEST_NOTIFICATION__.testAllPriorities()'); + console.log('\n'); + } + }; + + console.log('[NotificationContext] 🔧 调试 API 已加载: window.__TEST_NOTIFICATION__'); + console.log('[NotificationContext] 💡 使用 window.__TEST_NOTIFICATION__.help() 查看帮助'); + } + } + + // 清理函数 + return () => { + if (typeof window !== 'undefined' && window.__TEST_NOTIFICATION__) { + delete window.__TEST_NOTIFICATION__; + } + }; + }, [addNotification]); // 依赖 addNotification 函数 + const value = { notifications, isConnected, diff --git a/src/devtools/index.js b/src/devtools/index.js index bbd88bbe..cb672545 100644 --- a/src/devtools/index.js +++ b/src/devtools/index.js @@ -71,7 +71,10 @@ class DebugToolkit { console.log(''); console.log('%c2️⃣ 通知调试:', 'color: #9C27B0; font-weight: bold;'); console.log(' __DEBUG__.notification.getLogs() - 获取所有通知日志'); - console.log(' __DEBUG__.notification.forceNotification() - 发送测试通知'); + console.log(' __DEBUG__.notification.forceNotification() - 发送测试浏览器通知'); + console.log(' __DEBUG__.notification.testWebNotification(type, priority) - 测试网页通知 🆕'); + console.log(' __DEBUG__.notification.testAllNotificationTypes() - 测试所有类型 🆕'); + console.log(' __DEBUG__.notification.testAllNotificationPriorities() - 测试所有优先级 🆕'); console.log(' __DEBUG__.notification.checkPermission() - 检查通知权限'); console.log(' __DEBUG__.notification.exportLogs() - 导出通知日志'); console.log(''); diff --git a/src/devtools/notificationDebugger.js b/src/devtools/notificationDebugger.js index f2929a22..0ed4867a 100644 --- a/src/devtools/notificationDebugger.js +++ b/src/devtools/notificationDebugger.js @@ -159,6 +159,44 @@ class NotificationDebugger { getRecentEvents(count = 10) { return this.eventLog.slice(0, count); } + + /** + * 测试网页通知(需要 window.__TEST_NOTIFICATION__ 可用) + */ + testWebNotification(type = 'event_alert', priority = 'normal') { + if (typeof window !== 'undefined' && window.__TEST_NOTIFICATION__) { + console.log('[Notification Debugger] 调用测试 API'); + window.__TEST_NOTIFICATION__.testWebNotification(type, priority); + } else { + console.error('[Notification Debugger] ❌ window.__TEST_NOTIFICATION__ 不可用'); + console.error('💡 请确保:'); + console.error(' 1. REACT_APP_ENABLE_DEBUG=true'); + console.error(' 2. NotificationContext 已加载'); + console.error(' 3. 页面已刷新'); + } + } + + /** + * 测试所有通知类型 + */ + testAllNotificationTypes() { + if (typeof window !== 'undefined' && window.__TEST_NOTIFICATION__) { + window.__TEST_NOTIFICATION__.testAllTypes(); + } else { + console.error('[Notification Debugger] ❌ window.__TEST_NOTIFICATION__ 不可用'); + } + } + + /** + * 测试所有优先级 + */ + testAllNotificationPriorities() { + if (typeof window !== 'undefined' && window.__TEST_NOTIFICATION__) { + window.__TEST_NOTIFICATION__.testAllPriorities(); + } else { + console.error('[Notification Debugger] ❌ window.__TEST_NOTIFICATION__ 不可用'); + } + } } // 导出单例