Skip to content

isPC

检测当前是否为 PC 环境。

使用方法

typescript
import { isPC } from '@fu/matrix';

if (isPC()) {
  // PC 环境特有的处理
  useDesktopFeatures();
} else {
  // 移动端处理
  useMobileFeatures();
}

实现原理

通过检查 userAgent 中的系统标识判断:

  1. PC 操作系统标识:

    • Windows NT (Windows 系统)
    • Macintosh (macOS 系统)
    • Linux (不包含 Android)
    • X11 (Unix/Linux 系统)
  2. 排除标识:

    • Android
    • Mobile

支持的系统

  • Windows 系列 (Windows 7/8/10/11)
  • macOS 系列 (Intel/ARM)
  • Linux 发行版 (Ubuntu/CentOS/Mint等)
  • Unix 系统

返回值

  • true: 当前设备是 PC
  • false: 当前设备不是 PC

使用场景

  1. 功能适配
typescript
const features = isPC() 
  ? desktopFeatures 
  : mobileFeatures;
  1. UI 渲染
typescript
function Layout() {
  return isPC() 
    ? <DesktopLayout /> 
    : <MobileLayout />;
}

注意事项

  1. 服务端环境下默认返回 true
  2. 发生错误时返回 false
  3. 不会将 iPad 等平板设备识别为 PC
  4. 优先级高于其他平台检测
  5. 能正确区分 PC Linux 和 Android Linux
  6. 支持新的硬件平台(如 ARM Mac)

兼容性

  1. Windows
typescript
// Windows 10
Mozilla/5.0 (Windows NT 10.0; Win64; x64) ...

// Windows 11
Mozilla/5.0 (Windows NT 11.0; Win64; x64) ...
  1. macOS
typescript
// Intel Mac
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ...

// M1/M2 Mac
Mozilla/5.0 (Macintosh; ARM Mac OS X 10_15_7) ...
  1. Linux/Unix
typescript
// Ubuntu
Mozilla/5.0 (X11; Linux x86_64) ...

// Linux Mint
Mozilla/5.0 (X11; Linux x86_64; rv:109.0) ...