From 19e552009ad2fc49fa8c10564e74660f0422287f Mon Sep 17 00:00:00 2001 From: estel <690930@qq.com> Date: Sat, 9 Aug 2025 22:32:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B8=85=E6=A5=9AwxShare=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E7=9A=84elint=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/components/shared/wxShare.vue | 33 ++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/app/components/shared/wxShare.vue b/app/components/shared/wxShare.vue index a1e7b1a..1cdc830 100644 --- a/app/components/shared/wxShare.vue +++ b/app/components/shared/wxShare.vue @@ -11,11 +11,8 @@ const props = defineProps<{ // 仅在客户端挂载后执行,避免 SSR 阶段访问 window/location onMounted(async () => { try { - console.log('[WxShare] mounted with props:', { ...props }) await loadWxSdk() - console.log('[WxShare] wx sdk loaded:', !!(window as any).wx) const { appId, timestamp, nonceStr, signature } = await getWxConfig() - console.log('[WxShare] got config:', { appId, timestamp, nonceStr, signature: signature.slice(0, 8) + '...' }) setupShare(appId, timestamp, nonceStr, signature) } catch (err) { console.error('[WxShare] init error:', err) @@ -34,13 +31,34 @@ watch(() => props.url, async (newUrl, oldUrl) => { } }) +type WeChat = { + config: (cfg: { + debug?: boolean + appId: string + timestamp: number + nonceStr: string + signature: string + jsApiList: string[] + }) => void + ready: (cb: () => void) => void + error: (cb: (e: unknown) => void) => void + updateTimelineShareData: (opts: { title: string, link: string, imgUrl: string, success?: () => void }) => void + updateAppMessageShareData: (opts: { title: string, desc: string, link: string, imgUrl: string, success?: () => void }) => void +} + +function getWx(): WeChat | undefined { + if (typeof window === 'undefined') return undefined + const w = window as unknown as { wx?: WeChat } + return w.wx +} + function loadWxSdk(): Promise { console.log('loadWxSdk') if (typeof window === 'undefined') return Promise.resolve() - if ((window as any).wx) return Promise.resolve() + if (getWx()) return Promise.resolve() return new Promise((resolve, reject) => { const existing = document.getElementById('wx-jssdk') as HTMLScriptElement | null - if (existing && (window as any).wx) return resolve() + if (existing && getWx()) return resolve() const script = existing ?? document.createElement('script') if (!existing) { script.id = 'wx-jssdk' @@ -62,7 +80,8 @@ async function getWxConfig(): Promise<{ appId: string, timestamp: number, nonceS } function setupShare(appId: string, timestamp: number, nonceStr: string, signature: string) { - const wx = (window as any).wx + const wx = getWx() + if (!wx) return const shareTitle = props.title || document.title || '' const shareDesc = props.desc || document.title || '' const shareLink = props.url @@ -94,7 +113,7 @@ function setupShare(appId: string, timestamp: number, nonceStr: string, signatur }) }) - wx.error((e: unknown) => { + wx.error((e) => { console.error('[WxShare] wx.error:', e) }) }