Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -1,52 +1,52 @@
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { onMounted, ref } from "vue";
|
||||
|
||||
export function usePWAInstall() {
|
||||
const deferredPrompt = ref<any>(null)
|
||||
const canInstall = ref(false)
|
||||
const isInstalled = ref(false)
|
||||
const deferredPrompt = ref<any>(null);
|
||||
const canInstall = ref(false);
|
||||
const isInstalled = ref(false);
|
||||
|
||||
onMounted(() => {
|
||||
// 检查是否已安装
|
||||
if (window.matchMedia('(display-mode: standalone)').matches) {
|
||||
isInstalled.value = true
|
||||
return
|
||||
if (window.matchMedia("(display-mode: standalone)").matches) {
|
||||
isInstalled.value = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// 监听安装提示事件
|
||||
window.addEventListener('beforeinstallprompt', (e) => {
|
||||
e.preventDefault()
|
||||
deferredPrompt.value = e
|
||||
canInstall.value = true
|
||||
})
|
||||
window.addEventListener("beforeinstallprompt", (e) => {
|
||||
e.preventDefault();
|
||||
deferredPrompt.value = e;
|
||||
canInstall.value = true;
|
||||
});
|
||||
|
||||
// 监听安装成功事件
|
||||
window.addEventListener('appinstalled', () => {
|
||||
deferredPrompt.value = null
|
||||
canInstall.value = false
|
||||
isInstalled.value = true
|
||||
})
|
||||
})
|
||||
window.addEventListener("appinstalled", () => {
|
||||
deferredPrompt.value = null;
|
||||
canInstall.value = false;
|
||||
isInstalled.value = true;
|
||||
});
|
||||
});
|
||||
|
||||
async function install() {
|
||||
if (!deferredPrompt.value) {
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
await deferredPrompt.value.prompt()
|
||||
const { outcome } = await deferredPrompt.value.userChoice
|
||||
await deferredPrompt.value.prompt();
|
||||
const { outcome } = await deferredPrompt.value.userChoice;
|
||||
|
||||
if (outcome === 'accepted') {
|
||||
deferredPrompt.value = null
|
||||
canInstall.value = false
|
||||
return true
|
||||
if (outcome === "accepted") {
|
||||
deferredPrompt.value = null;
|
||||
canInstall.value = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false
|
||||
return false;
|
||||
}
|
||||
catch (error) {
|
||||
console.error('安装失败:', error)
|
||||
return false
|
||||
console.error("安装失败:", error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,5 +54,5 @@ export function usePWAInstall() {
|
||||
canInstall,
|
||||
isInstalled,
|
||||
install,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,32 +1,32 @@
|
||||
import type { Platform } from '~/types'
|
||||
import type { Platform } from "~/types";
|
||||
|
||||
export function usePlatformDetection() {
|
||||
const platform = useState<Platform>('platform', () => 'unknown')
|
||||
const platform = useState<Platform>("platform", () => "unknown");
|
||||
|
||||
function detectPlatform(): Platform {
|
||||
if (import.meta.server)
|
||||
return 'unknown'
|
||||
return "unknown";
|
||||
|
||||
const ua = navigator.userAgent.toLowerCase()
|
||||
const ua = navigator.userAgent.toLowerCase();
|
||||
|
||||
if (/iphone|ipad|ipod/.test(ua))
|
||||
return 'ios'
|
||||
return "ios";
|
||||
else if (/android/.test(ua))
|
||||
return 'android'
|
||||
return "android";
|
||||
else if (/windows|macintosh|linux/.test(ua))
|
||||
return 'desktop'
|
||||
return "desktop";
|
||||
|
||||
return 'unknown'
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
platform.value = detectPlatform()
|
||||
})
|
||||
platform.value = detectPlatform();
|
||||
});
|
||||
|
||||
return {
|
||||
platform: readonly(platform),
|
||||
isIOS: computed(() => platform.value === 'ios'),
|
||||
isAndroid: computed(() => platform.value === 'android'),
|
||||
isDesktop: computed(() => platform.value === 'desktop'),
|
||||
}
|
||||
isIOS: computed(() => platform.value === "ios"),
|
||||
isAndroid: computed(() => platform.value === "android"),
|
||||
isDesktop: computed(() => platform.value === "desktop"),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user