diff --git a/frontend/src/app/pages/SharePage.tsx b/frontend/src/app/pages/SharePage.tsx index 81a4055..62c6eaf 100644 --- a/frontend/src/app/pages/SharePage.tsx +++ b/frontend/src/app/pages/SharePage.tsx @@ -24,6 +24,27 @@ import bridge from "../services/bridge"; type SaveStatus = "idle" | "generating" | "success" | "error"; +/** 调试:检查小程序环境 */ +function checkEnv(): string[] { + const lines: string[] = []; + lines.push('UA: ' + navigator.userAgent.substring(0, 60)); + try { + const w = (window as any).wx; + lines.push('wx 对象: ' + (typeof w !== 'undefined' ? '存在' : '不存在')); + if (w) { + lines.push('wx.miniProgram: ' + (w.miniProgram ? '存在' : '不存在')); + if (w.miniProgram) { + lines.push('navigateBack: ' + (typeof w.miniProgram.navigateBack)); + lines.push('postMessage: ' + (typeof w.miniProgram.postMessage)); + w.miniProgram.getEnv((res: any) => { + lines.push('getEnv: ' + JSON.stringify(res)); + }); + } + } + } catch (e: any) { lines.push('wx 检测异常: ' + e.message); } + return lines; +} + export function SharePage() { const navigate = useNavigate(); const { reportId } = useParams(); @@ -32,8 +53,48 @@ export function SharePage() { const [saveStatus, setSaveStatus] = useState("idle"); const [imageUrl, setImageUrl] = useState(null); const [linkCopied, setLinkCopied] = useState(false); + const [debugLog, setDebugLog] = useState([]); const cardRef = useRef(null); + const addLog = useCallback((msg: string) => { + console.log('[ShareDebug]', msg); + setDebugLog(prev => [...prev.slice(-15), new Date().toLocaleTimeString() + ' ' + msg]); + }, []); + + // 分享按钮(带调试日志) + const handleShare = useCallback(() => { + setDebugLog([]); + const envInfo = checkEnv(); + envInfo.forEach(m => addLog(m)); + + const w = (window as any).wx; + const mp = w?.miniProgram; + + if (!mp) { + addLog('❌ wx.miniProgram 不存在,分享功能不可用'); + return; + } + + addLog('→ postMessage 发送 share 数据'); + mp.postMessage({ + data: { + action: 'share', + title: '孕育无忧 · 报告解读', + summary: shareData?.overall_summary || shareData?.emotional_summary || '', + href: window.location.href, + scene: 'WXSceneSession', + } + }); + + addLog('→ 调用 navigateBack({delta:1})'); + mp.navigateBack({ + delta: 1, + success: () => addLog('✅ navigateBack 成功'), + fail: (err: any) => addLog('❌ navigateBack 失败: ' + JSON.stringify(err)), + complete: () => addLog('→ navigateBack complete'), + }); + }, [addLog, shareData]); + // 复制链接(客户端优先,bridge 降级) const handleCopyLink = useCallback(async () => { const shareUrl = `${window.location.origin}/app_03/share/${reportId}`; @@ -304,6 +365,27 @@ export function SharePage() { + {/* 调试面板:点击分享后显示执行步骤 */} + + {debugLog.length > 0 && ( + + +
+

🔍 分享调试

+ +
+
+ {debugLog.map((m, i) =>
{m}
)} +
+
+
+ )} +
+ {/* 操作提示 */} {/* 微信分享按钮(仅在 web-view 环境下生效) */}