From 37e0f9dbc52f993619a4d67fb407dbeea0f362e1 Mon Sep 17 00:00:00 2001 From: Tim Date: Fri, 8 Aug 2025 14:21:58 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A7=BB=E5=8A=A8=E7=AB=AF=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E5=99=A8=E6=B8=B2=E6=9F=93=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend_nuxt/utils/screen.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/frontend_nuxt/utils/screen.js b/frontend_nuxt/utils/screen.js index 3d3aebc03..a32e999aa 100644 --- a/frontend_nuxt/utils/screen.js +++ b/frontend_nuxt/utils/screen.js @@ -3,6 +3,20 @@ import { ref, computed } from 'vue' const width = ref(0) const isClient = ref(false) +// 检测移动设备的用户代理字符串 +const isMobileUserAgent = () => { + if (typeof navigator === 'undefined') return false + + const userAgent = navigator.userAgent.toLowerCase() + const mobileKeywords = [ + 'android', 'iphone', 'ipad', 'ipod', 'blackberry', 'windows phone', + 'mobile', 'tablet', 'opera mini', 'iemobile' + ] + + return mobileKeywords.some(keyword => userAgent.includes(keyword)) +} + +// 客户端初始化 if (typeof window !== 'undefined') { isClient.value = true width.value = window.innerWidth @@ -11,4 +25,13 @@ if (typeof window !== 'undefined') { }) } -export const isMobile = computed(() => isClient.value && width.value <= 768) +// 服务端和客户端的移动设备检测 +export const isMobile = computed(() => { + if (isClient.value) { + // 客户端:优先使用窗口宽度,如果窗口宽度不可用则使用用户代理 + return width.value > 0 ? width.value <= 768 : isMobileUserAgent() + } else { + // 服务端:使用用户代理字符串 + return isMobileUserAgent() + } +})