feat: 测试分享
This commit is contained in:
@@ -52,12 +52,34 @@ async function shareWithWebAPI(data: ShareData): Promise<void> {
|
||||
*/
|
||||
export function shareToWhatsApp(text: string, url?: string): void {
|
||||
const shareText = url ? `${text} ${url}` : text
|
||||
// WhatsApp Web API: https://wa.me/?text=
|
||||
const encodedText = encodeURIComponent(shareText)
|
||||
const whatsappUrl = `https://wa.me/?text=${encodedText}`
|
||||
|
||||
// 打开 WhatsApp 分享链接
|
||||
window.open(whatsappUrl, '_blank')
|
||||
const whatsappScheme = `whatsapp://send?text=${encodedText}`
|
||||
const whatsappWebUrl = `https://wa.me/?text=${encodedText}`
|
||||
const isMobile = typeof navigator !== 'undefined' && /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent)
|
||||
|
||||
if (typeof window === 'undefined') return
|
||||
|
||||
if (isMobile) {
|
||||
// 优先尝试调用已安装的 WhatsApp 应用,失败后回退到 Web 版本
|
||||
let didFallback = false
|
||||
const fallbackTimer = window.setTimeout(() => {
|
||||
didFallback = true
|
||||
window.location.href = whatsappWebUrl
|
||||
}, 1500)
|
||||
|
||||
try {
|
||||
window.location.href = whatsappScheme
|
||||
} catch (error) {
|
||||
window.clearTimeout(fallbackTimer)
|
||||
window.location.href = whatsappWebUrl
|
||||
}
|
||||
|
||||
// 某些浏览器会在成功唤起 App 时终止脚本,无需额外处理
|
||||
return
|
||||
}
|
||||
|
||||
// 桌面端或不支持 scheme 的环境直接打开 WhatsApp Web
|
||||
window.open(whatsappWebUrl, '_blank')
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user