高精度操作系统检测函数

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /**
 * 高精度操作系统检测函数
 * @param {boolean} [withVersion=false] - 是否返回系统版本信息
 * @returns {Object|string} 若 withVersion 为 true,返回 { type: 系统类型, version: 版本号, device: 设备类型 };否则返回系统类型字符串
 */
function detectOS(withVersion) {
  // 1. 兜底处理:UA 为空/非字符串时的默认值
  const userAgent = (navigator?.userAgent || '').toLowerCase().trim();
  if (!userAgent) {
    const result = { type: 'Unknown', version: 'unknown', device: 'unknown' };
    return withVersion ? result : result.type;
  }

  // 定义系统检测规则(优先级:移动端 → 桌面端 → 小众系统)
  const osRules = [
    // 移动端系统
    {
      // iOS(iPhone/iPad/iPod):UA 含 iphone/ipad/ipod,且排除桌面端伪装
      test: (ua) => /iphone|ipad|ipod/.test(ua) && !/windows|android/.test(ua),
      getType: () => 'iOS',
      getVersion: (ua) => {
        // 解析 iOS 版本(如 iPhone OS 17_0 → 17.0)
        const match = ua.match(/iphone os (\d+)(?:_(\d+))?(?:_(\d+))?/);
        return match ? `${match[1]}.${match[2] || 0}.${match[3] || 0}` : 'unknown';
      },
      device: 'mobile/tablet'
    },
    {
      // Android(手机/平板/电视):排除 ChromeOS(含 android 但属于桌面端)
      test: (ua) => /android/.test(ua) && !/chrome os/.test(ua),
      getType: () => 'Android',
      getVersion: (ua) => {
        // 解析 Android 版本(如 Android 14 → 14)
        const match = ua.match(/android (\d+)(?:\.(\d+))?(?:\.(\d+))?/);
        return match ? `${match[1]}.${match[2] || 0}.${match[3] || 0}` : 'unknown';
      },
      device: 'mobile/tablet/tv'
    },
    // 桌面端系统
    {
      // Windows(含 Win10/11、Server 版本)
      test: (ua) => /windows/.test(ua),
      getType: () => 'Windows',
      getVersion: (ua) => {
        // 解析 Windows 版本(Win11: 10.0.22621 / Win10: 10.0.x / Win7: 6.1.x)
        const match = ua.match(/windows nt (\d+)\.(\d+)(?:\.(\d+))?/);
        if (!match) return 'unknown';
        const [, major, minor] = match;
        const versionMap = {
          '10.0': (() => {
            if (major === '10' && minor === '0') {
              if (ua.includes('windows nt 10.0; win64; x64') && ua.includes('chrome')) {
                return ua.includes('build 22') ? '11' : '10';
              }
              return '10';
            }
            return '10.0';
          })(), // 区分 Win10/11
          '6.3': '8.1',
          '6.2': '8',
          '6.1': '7',
          '6.0': 'Vista',
          '5.1': 'XP'
        };
        return versionMap[`${major}.${minor}`] || `${major}.${minor}`;
      },
      device: 'desktop'
    },
    {
      // macOS(兼容新旧 UA:Mac OS X / macOS)
      test: (ua) => /mac os x|macos|macintosh/.test(ua) && !/iphone|ipad|ipod/.test(ua),
      getType: () => 'macOS',
      getVersion: (ua) => {
        // 解析 macOS 版本(如 10_15_7 → 10.15.7;14.0 → Sonoma)
        const match = ua.match(/mac os x (\d+)_(\d+)(?:_(\d+))?/) || ua.match(/macos (\d+)\.(\d+)(?:\.(\d+))?/);
        if (!match) return 'unknown';
        const [, major, minor, patch] = match;
        // macOS 11+ 版本号映射(11=Big Sur, 12=Monterey, 13=Ventura, 14=Sonoma)
        const versionNameMap = {
          '11': '11 (Big Sur)',
          '12': '12 (Monterey)',
          '13': '13 (Ventura)',
          '14': '14 (Sonoma)',
          '15': '15 (Sequoia)'
        };
        const versionStr = `${major}.${minor}.${patch || 0}`;
        return versionNameMap[major] || versionStr;
      },
      device: 'desktop'
    },
    {
      // Linux(含发行版:Ubuntu/CentOS 等)
      test: (ua) => /linux/.test(ua) && !/android|chrome os/.test(ua),
      getType: () => {
        // 细化 Linux 发行版
        if (userAgent.includes('ubuntu')) return 'Linux (Ubuntu)';
        if (userAgent.includes('centos')) return 'Linux (CentOS)';
        if (userAgent.includes('debian')) return 'Linux (Debian)';
        if (userAgent.includes('fedora')) return 'Linux (Fedora)';
        return 'Linux';
      },
      getVersion: (ua) => {
        const match = ua.match(/ubuntu (\d+\.\d+)/) || ua.match(/centos (\d+)/);
        return match ? match[1] : 'unknown';
      },
      device: 'desktop/server'
    },
    {
      // ChromeOS(谷歌云本)
      test: (ua) => /chrome os/.test(ua),
      getType: () => 'ChromeOS',
      getVersion: (ua) => {
        const match = ua.match(/chrome os (\d+)\.(\d+)/);
        return match ? `${match[1]}.${match[2]}` : 'unknown';
      },
      device: 'chromebook'
    },
    // 小众系统
    {
      test: (ua) => /freebsd/.test(ua),
      getType: () => 'FreeBSD',
      getVersion: (ua) => {
        const match = ua.match(/freebsd (\d+\.\d+)/);
        return match ? match[1] : 'unknown';
      },
      device: 'desktop/server'
    },
    {
      test: (ua) => /openbsd/.test(ua),
      getType: () => 'OpenBSD',
      getVersion: () => 'unknown',
      device: 'desktop/server'
    }
  ];

  // 遍历规则匹配系统
  for (const rule of osRules) {
    if (rule.test(userAgent)) {
      const result = {
        type: rule.getType(),
        version: rule.getVersion(userAgent),
        device: rule.device
      };
      return withVersion ? result : result.type;
    }
  }

  // 未匹配到任何系统
  const fallbackResult = { type: 'Unknown', version: 'unknown', device: 'unknown' };
  return withVersion ? fallbackResult : fallbackResult.type;
}

// ==================== 使用示例 ====================
// 1. 基础使用(仅获取系统类型)
let osType = detectOS();
console.log('系统类型:', osType); // Windows / macOS / iOS / Android 等

// 2. 高级使用(获取版本+设备类型)
const osDetail_1 = detectOS(true);
console.log('系统详情:', osDetail_1);
// 示例输出:
// { type: 'macOS', version: '14.0.0 (Sonoma)', device: 'desktop' }
// { type: 'Windows', version: '11', device: 'desktop' }
// { type: 'iOS', version: '17.0.2', device: 'mobile/tablet' }

// 3. 业务常用判断(封装快捷方法)
const osDetail_2 = detectOS(true);
const isWindows = osDetail_2.type.includes('Windows');
const isMacOS = osDetail_2.type === 'macOS';
const isMobile = ['mobile/tablet', 'mobile/tablet/tv'].includes(osDetail_2.device);
console.log('是否Windows:', isWindows);
console.log('是否macOS:', isMacOS);
console.log('是否移动端:', isMobile);

  </script>
</body>
</html>
Logo

openvela 操作系统专为 AIoT 领域量身定制,以轻量化、标准兼容、安全性和高度可扩展性为核心特点。openvela 以其卓越的技术优势,已成为众多物联网设备和 AI 硬件的技术首选,涵盖了智能手表、运动手环、智能音箱、耳机、智能家居设备以及机器人等多个领域。

更多推荐