美文网首页
js获取设备类型

js获取设备类型

作者: Ysj1111 | 来源:发表于2018-09-04 08:41 被阅读15次
     //1获取浏览器名称和版本
      getBrower = ()=>{
        let agent = navigator.userAgent.toLowerCase();
        let browser = { appname: 'unknown', version: '0' };
        if(/(qqbrowser|ubrowser)\D+(\d[\d.]*)/.test(agent)) {//qq浏览器,UC浏览器
          browser.appname = RegExp.$1;
          browser.version = RegExp.$2;
        } else if (/se[\s\.a-zA-Z\d]+metasr/.test(agent)) {
          browser.appname = 'sougou';
          browser.version = '0';
        } else if (/(msie|firefox|opera|chrome|netscape)\D+(\d[\d.]*)/.test(agent)) {
          browser.appname = RegExp.$1;
          browser.version = RegExp.$2;
        } else if (/version\D+(\d[\d.]*).*safari/.test(agent)) { // safari  
          browser.appname = 'safari';
          browser.version = RegExp.$2;
        }
        return browser;
        // console.log(browser);
      }
      //2获取浏览器的操作系统平台
      getPlatform = ()=>{
        let os = {
          platform: navigator.platform.toLowerCase(),
        };
        return os;
      // console.log(os);
      };
      //3获取当前设备类型
      getDevice = ()=>{
        let agent = navigator.userAgent.toLowerCase();
        let result = {
          device: function () {
            if (/windows/.test(agent)) {
              return 'windows pc';
            } else if (/iphone|ipod/.test(agent) && /mobile/.test(agent)) {
              return 'iphone';
            } else if (/ipad/.test(agent) && /mobile/.test(agent)) {
              return 'ipad';
            } else if (/android/.test(agent) && /mobile/.test(agent)) {
              return 'android';
            } else if (/linux/.test(agent)) {
              return 'linux pc';
            } else if (/mac/.test(agent)) {
              return 'mac';
            } else {
              return 'other';
            }
          }(),
        };
        return result;
        // console.log(result);
      };
      //4获取IP和城市
        //通过外部接口实现,引入在componentDidMount中
          <script type="text/javascript" src="https://ipip.yy.com/get_ip_info.php"></script>
          
      //5获取用户访问时间,停留时间,访问路径等
        //写在组件Frame中
    constructor(props) {
        super(props);
        this.state = ({
          viewCount:0,
        });
        window.addEventListener('hashchange', this.hashChg, false);
      }
      //hashchange
      hashChg = (e) => {
        // 记录用户在页面的停留时间
        let item = {};
        item.accessUrl = e.oldURL;
        item.accessQuery = e.oldURL.split('?')[1]?decodeURI(e.oldURL.split('?')[1]):'';
        item.accessTimeIn = time;
        time = item.accessTimeOut = Date.now();
        item.accessTime = item.accessTimeOut - item.accessTimeIn;
        item.browserName = this.getBrower().appname;
        item.browserVersion = this.getBrower().version;
        item.browserPlatform = this.getPlatform().platform;
        item.accessDevice = this.getDevice().device;
        item.accessIp = window.returnInfo?window.returnInfo.cip:'';
        item.accessCname = window.returnInfo?window.returnInfo.province:'';
        item.accessCity = window.returnInfo?window.returnInfo.city:'';
        item.ipSupplier = window.returnInfo?window.returnInfo.isp:'';
        item.screen = window.screen.width +'*'+ window.screen.height;
        item.cookies = this.getCookie('userUuid');
        // console.log(item);
        // 新增用户动态接口对接
        postService(port+ '/usernews/usernews/single', item ,data => {
          // console.log('新增用户动态:'+data.msg);
        });
      }
    
    
    componentWillUnmount = () => {
        window.removeEventListener('hashchange', this.hashChg, false);
      }
    
    

    设备访问日志

    android:
    2017-09-16 17:15:43:61.183.207.98 - - [16/Sep/2017:09:15:43 +0000] "GET /app_e5c76a0a.js HTTP/1.1" 304 0 "http://114.67.134.89/" "Mozilla/5.0 (Linux; Android 7.0; PRA-AL00X Build/HONORPRA-AL00X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.0.0 Mobile Safari/537.36" "-"
    
    windows pc:
    2017-09-16 17:16:02:61.183.207.98 - - [16/Sep/2017:09:16:02 +0000] "GET /__webpack_hmr HTTP/1.1" 404 571 "http://114.67.134.89/" "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36" "-"
    
    ipad:
    2017-09-16 17:19:14:61.183.207.98 - - [16/Sep/2017:09:19:14 +0000] "GET /app_e5c76a0a.js HTTP/1.1" 200 6211314 "http://114.67.134.89/" "Mozilla/5.0 (iPad; CPU OS 10_2 like Mac OS X) AppleWebKit/602.3.12 (KHTML, like Gecko) Version/10.0 Mobile/14C92 Safari/602.1" "-"
    
    iphone:
    2017-09-16 17:18:23:61.183.207.98 - - [16/Sep/2017:09:18:23 +0000] "GET /img/img_f07a08e6.png HTTP/1.1" 200 1109 "http://114.67.134.89/" "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_3 like Mac OS X) AppleWebKit/603.3.8 (KHTML, like Gecko) Version/10.0 Mobile/14G60 Safari/602.1" "-"
    
    

    参考链接

    相关文章

      网友评论

          本文标题:js获取设备类型

          本文链接:https://www.haomeiwen.com/subject/uudysxtx.html