获取微信版本、手机操作系统、手机类型

效果图

    let system = {}
    let userAgent = navigator.userAgent
    // 获取微信版本
    let systemInfo = userAgent.match(/MicroMessenger.*?(?= )/)
    if (systemInfo && systemInfo.length > 0) {
      system.wechat = systemInfo[0]
    }
    // 苹果手机
    if (userAgent.includes('iPhone') || userAgent.includes('iPad')) {
      if (userAgent.includes('iPad')) {
          system.device = 'iPad'
      } else {
          system.device = 'iPhone'
      }
      // 获取操作系统版本
      systemInfo = userAgent.match(/iPhone OS .*?(?= )/)
      if (systemInfo && systemInfo.length > 0) {
          system.system = systemInfo[0]
      }
    }
    // 安卓手机
    if (userAgent.includes('Android')) {
      systemInfo = userAgent.match(/Android.*; ?(.*(?= Build))/)
      if (systemInfo && systemInfo.length > 1) {
        system.device = systemInfo[1]
      }
      // 获取操作系统版本
      systemInfo = userAgent.match(/Android.*?(?=;)/)
      if (systemInfo && systemInfo.length > 0) {
        system.system = systemInfo[0]
      }
    }
    alert(JSON.stringify(system))

 

posted @ 2023-11-23 15:21  eternityQSL  阅读(37)  评论(0编辑  收藏  举报