Implement code changes to enhance functionality and improve performance

This commit is contained in:
2026-06-25 11:06:14 +08:00
parent 9b315fd77d
commit 7d474dcf5e

View File

@@ -1,6 +1,7 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { useNavigate, useParams } from "react-router"; import { useNavigate, useParams } from "react-router";
import { motion, AnimatePresence } from "motion/react"; import { motion, AnimatePresence } from "motion/react";
import bridge from "../services/bridge";
import { import {
ChevronLeft, ChevronLeft,
Heart, Heart,
@@ -48,28 +49,33 @@ export function AIAnalysisPage() {
}, [reportId]); }, [reportId]);
const handleCopyLink = async () => { const handleCopyLink = async () => {
const shareUrl = `${window.location.origin}/share/${reportId}`;
// 微信 web-view 中 navigator.clipboard 不可用,用 execCommand 替代
let copied = false;
const textArea = document.createElement('textarea');
textArea.value = shareUrl;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
textArea.style.top = '-999999px';
textArea.style.opacity = '0';
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try { try {
const shareUrl = `${window.location.origin}/share/${reportId}`; copied = document.execCommand('copy');
await navigator.clipboard.writeText(shareUrl); } catch (e) {
console.warn('[copy] execCommand failed:', e);
}
document.body.removeChild(textArea);
if (copied) {
setLinkCopied(true);
setTimeout(() => setLinkCopied(false), 2000);
} else {
// 兜底:通过小程序桥接(postMessage 在页面离开时触发)
bridge.copyLink(shareUrl);
setLinkCopied(true); setLinkCopied(true);
setTimeout(() => setLinkCopied(false), 2000); setTimeout(() => setLinkCopied(false), 2000);
} catch (error) {
console.error('复制失败:', error);
// 降级方案:使用旧的复制方法
const textArea = document.createElement('textarea');
textArea.value = `${window.location.origin}/share/${reportId}`;
textArea.style.position = 'fixed';
textArea.style.left = '-999999px';
document.body.appendChild(textArea);
textArea.select();
try {
document.execCommand('copy');
setLinkCopied(true);
setTimeout(() => setLinkCopied(false), 2000);
} catch (err) {
alert('复制失败,请手动复制链接');
}
document.body.removeChild(textArea);
} }
}; };