"use client" import { useState, useEffect } from "react" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { Download, X, Smartphone } from "lucide-react" import { Alert, AlertDescription } from "@/components/ui/alert" export function PWAInstaller() { const [deferredPrompt, setDeferredPrompt] = useState(null) const [isInstallable, setIsInstallable] = useState(false) const [isInstalled, setIsInstalled] = useState(false) const [showPrompt, setShowPrompt] = useState(false) useEffect(() => { // 检查是否已安装 if (typeof window !== "undefined") { const isStandalone = window.matchMedia('(display-mode: standalone)').matches const isIOSStandalone = (window.navigator as any).standalone === true if (isStandalone || isIOSStandalone) { setIsInstalled(true) return } } // 监听 beforeinstallprompt 事件 const handleBeforeInstallPrompt = (e: Event) => { e.preventDefault() setDeferredPrompt(e) setIsInstallable(true) // 延迟显示提示(给用户一些时间浏览应用) setTimeout(() => { const hasSeenPrompt = localStorage.getItem('pwa-install-prompt-seen') if (!hasSeenPrompt) { setShowPrompt(true) } }, 30000) // 30秒后显示 } // 监听安装完成事件 const handleAppInstalled = () => { setIsInstalled(true) setShowPrompt(false) setIsInstallable(false) } if (typeof window !== "undefined") { window.addEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.addEventListener('appinstalled', handleAppInstalled) return () => { window.removeEventListener('beforeinstallprompt', handleBeforeInstallPrompt) window.removeEventListener('appinstalled', handleAppInstalled) } } }, []) const handleInstallClick = async () => { if (!deferredPrompt) return // 显示安装提示 deferredPrompt.prompt() // 等待用户响应 const { outcome } = await deferredPrompt.userChoice if (outcome === 'accepted') { console.log('用户接受了安装') } else { console.log('用户拒绝了安装') } setDeferredPrompt(null) setShowPrompt(false) localStorage.setItem('pwa-install-prompt-seen', 'true') } const handleDismiss = () => { setShowPrompt(false) localStorage.setItem('pwa-install-prompt-seen', 'true') } if (isInstalled) { return null } if (!isInstallable) { return null } return ( <> {/* 浮动安装提示 */} {showPrompt && (
安装应用
将家谱应用添加到主屏幕,随时随地访问
)} {/* 设置页面的安装按钮 */} {!showPrompt && ( 将应用安装到设备 )} ) }