清楚wxShare组件的elint错误
This commit is contained in:
@@ -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<void> {
|
||||
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)
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user