fix(share): 用模块级对象属性 _wp.url 彻底避免 minifier 变量名不一致

This commit is contained in:
2026-07-08 19:32:23 +08:00
parent 7bc273e666
commit c5abc50c33

View File

@@ -33,6 +33,9 @@ function supportsWxMiniProgramShare(): boolean {
} catch { return false; } } catch { return false; }
} }
/** 模块级状态:避免构建压缩时变量名不一致导致 ReferenceError */
const _wp = { url: null as string | null, tick: 0 };
export function SharePage() { export function SharePage() {
const navigate = useNavigate(); const navigate = useNavigate();
const { reportId } = useParams(); const { reportId } = useParams();
@@ -93,7 +96,7 @@ export function SharePage() {
let cancelled = false; let cancelled = false;
generateWxacode({ width: 430 }) generateWxacode({ width: 430 })
.then((url) => { .then((url) => {
if (!cancelled) { wxacodeUrl.current = url; forceUpdate(n => n + 1); } if (!cancelled) { _wp.url = url; forceUpdate(n => n + 1); }
}) })
.catch((err) => { .catch((err) => {
console.warn("[SharePage] 太阳码生成失败:", err); console.warn("[SharePage] 太阳码生成失败:", err);
@@ -106,7 +109,7 @@ export function SharePage() {
// 卸载时清理 blob URL // 卸载时清理 blob URL
useEffect(() => { useEffect(() => {
return () => { return () => {
if (wxacodeUrl.current?.startsWith("blob:")) URL.revokeObjectURL(wxacodeUrl.current); if (_wp.url?.startsWith("blob:")) URL.revokeObjectURL(_wp.url);
}; };
}, []); }, []);
@@ -115,11 +118,11 @@ export function SharePage() {
if (!cardRef.current) return null; if (!cardRef.current) return null;
// 等待太阳码渲染完成(blob URL 需浏览器解码) // 等待太阳码渲染完成(blob URL 需浏览器解码)
if (shareData && !wxacodeUrl.current) { if (shareData && !_wp.url) {
await new Promise<void>((resolve) => { await new Promise<void>((resolve) => {
const start = Date.now(); const start = Date.now();
const tick = () => { const tick = () => {
if (wxacodeUrl.current || Date.now() - start > 1500) { if (_wp.url || Date.now() - start > 1500) {
resolve(); resolve();
return; return;
} }
@@ -127,7 +130,7 @@ export function SharePage() {
}; };
tick(); tick();
}); });
if (!wxacodeUrl.current) { if (!_wp.url) {
console.warn("[SharePage] 太阳码未就绪,截图中可能缺失二维码"); console.warn("[SharePage] 太阳码未就绪,截图中可能缺失二维码");
} }
} }
@@ -666,7 +669,7 @@ function ShareCardContent({
)} )}
{/* 太阳码区(截图包含) */} {/* 太阳码区(截图包含) */}
{wxacodeUrl.current && ( {_wp.url && (
<div <div
style={{ style={{
textAlign: "center", textAlign: "center",
@@ -675,7 +678,7 @@ function ShareCardContent({
}} }}
> >
<img <img
src={wxacodeUrl.current} src={_wp.url}
alt="扫码下载孕育无忧" alt="扫码下载孕育无忧"
crossOrigin="anonymous" crossOrigin="anonymous"
style={{ style={{