挪动端推断wifi

用window.navigator.connection来检测,别的微信6.0版本以上,UA有个NetType,6.0版本以下,可以用微信私有接口WeixinJSBridge

    var wifi = true;
    var ua = window.navigator.userAgent;
    var con = window.navigator.connection;
    // 假如是微信
    if(/MicroMessenger/.test(ua)){
        // 假如是微信6.0以上版本,用UA来推断
        if(/NetType/.test(ua)){
            if(ua.match(/NetType\/(\S*)$/)[1] != 'WIFI'){
                wifi = false;
            }
        // 假如是微信6.0以下版本,挪用微信私有接口WeixinJSBridge
        }else{
            document.addEventListener("WeixinJSBridgeReady",function onBridgeReady(){
                WeixinJSBridge.invoke('getNetworkType',{},function(e){
                    if(e.err_msg != "network_type:wifi"){
                        wifi = false;
                    }
                });
            });
        }
    // 假如支撑navigator.connection
    }else if(con){
        var network = con.type|| con.effectiveType;
        if(network != "wifi" && network != "2" && network != "unknown"){ 
        // unknown是为了兼容Chrome Canary
            wifi = false;
        }
    }
   window.networkWIFI = wifi;


  

附加

推断是不是小顺序
window.__wxjs_environment=='miniprogram'||window.globalEvn=='miniprogram'

// 是不是是微信浏览器
isWeixin () {
   let ua = navigator.userAgent.toLowerCase()
   if (ua.match(/MicroMessenger/i) === 'micromessenger') {
      return true
   } else {
      return false
   }
 }
    原文作者:catalina
    原文地址: https://segmentfault.com/a/1190000013555145
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞