diff --git a/frontend/src/app/pages/SharePage.tsx b/frontend/src/app/pages/SharePage.tsx index 3a7c070..1256c35 100644 --- a/frontend/src/app/pages/SharePage.tsx +++ b/frontend/src/app/pages/SharePage.tsx @@ -14,6 +14,7 @@ import { Download, Check, Image as ImageIcon, + Smartphone, } from "lucide-react"; import { toPng } from "html-to-image"; import { Card } from "../components/ui/card"; @@ -24,25 +25,12 @@ 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)); +/** 检测是否支持微信小程序分享(wx.miniProgram 必须存在) */ +function supportsWxMiniProgramShare(): boolean { 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; + return !!(w && w.miniProgram && typeof w.miniProgram.navigateBack === 'function'); + } catch { return false; } } export function SharePage() { @@ -53,48 +41,13 @@ export function SharePage() { const [saveStatus, setSaveStatus] = useState("idle"); const [imageUrl, setImageUrl] = useState(null); const [linkCopied, setLinkCopied] = useState(false); - const [debugLog, setDebugLog] = useState([]); + const [canShareToWechat, setCanShareToWechat] = useState(false); const cardRef = useRef(null); - const addLog = useCallback((msg: string) => { - console.log('[ShareDebug]', msg); - setDebugLog(prev => [...prev.slice(-15), new Date().toLocaleTimeString() + ' ' + msg]); + useEffect(() => { + setCanShareToWechat(supportsWxMiniProgramShare()); }, []); - // 分享按钮(带调试日志) - 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}`; @@ -365,27 +318,6 @@ export function SharePage() { - {/* 调试面板:点击分享后显示执行步骤 */} - - {debugLog.length > 0 && ( - - -
-

🔍 分享调试

- -
-
- {debugLog.map((m, i) =>
{m}
)} -
-
-
- )} -
- {/* 操作提示 */}
- {/* 微信分享按钮(仅在 web-view 环境下生效) */} - + {/* 微信分享:仅 wx.miniProgram 可用时显示 */} + {canShareToWechat ? ( + + ) : ( + +
+ +
+

📱 如何分享

+

+ 长按上方预览图片 → 保存到相册,然后通过微信发送给好友 +

+
+
+
+ )}