chenlei 7 часов назад
Родитель
Сommit
619a109384
1 измененных файлов с 13 добавлено и 6 удалено
  1. 13 6
      src/index/utils/index.ts

+ 13 - 6
src/index/utils/index.ts

@@ -21,11 +21,18 @@ export function parseUrlParams(url: string): Record<string, string> {
 }
 
 export function judgeIsMobile() {
-  const userAgentInfo = navigator.userAgent;
-  const mobileAgents = ['Android', 'iPhone', 'SymbianOS', 'Windows Phone', 'iPad', 'iPod'];
-  const mobileFlag = mobileAgents.some((mobileAgent) => {
-    return userAgentInfo.indexOf(mobileAgent) > 0;
-  });
+  // 非浏览器环境直接返回false(避免navigator不存在时报错)
+  if (typeof navigator === 'undefined') return false;
 
-  return mobileFlag;
+  const userAgent = navigator.userAgent.toLowerCase();
+  // 匹配移动端核心特征关键词,排除桌面端误判
+  const mobileKeywords =
+    /android|iphone|ipod|ipad|ios|blackberry|webos|windows phone|mobile|symbian|iemobile|opera mini/i;
+  const desktopKeywords = /windows nt|macintosh|linux/i;
+
+  const hasMobileUA = mobileKeywords.test(userAgent);
+  const hasDesktopUA = desktopKeywords.test(userAgent);
+
+  // 核心逻辑:匹配移动端UA且非桌面端,或明确是平板设备
+  return (hasMobileUA && !hasDesktopUA) || /ipad|tablet/i.test(userAgent);
 }