funs.js 4.41 KB
/**
 * 根据item id 编写对应的前端检测方法
 */
(function(global) {
  function testWebSocket(ip,port,cb){
    //ws://192.168.7.201:7070/ws/ 如果是https下 wss   wss://192.168.7.201:7070/ws/
    var url = '';
    var hostname ="";
    if(port=='7443'){
        hostname = "wss";
    }else{
        hostname = "ws";
    }
    url = hostname+'://'+ip+':'+port+'/ws/';
    var ws = new WebSocket(url,'xmpp');

    ws.onerror = function () {
        cb(false);
    };
        
    ws.onopen = function () {
        cb(true);
    };
  }
  
  function IEVersion() {
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串  
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器  
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器  
    var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
    if(isIE) {
        var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
        reIE.test(userAgent);
        var fIEVersion = parseFloat(RegExp["$1"]);
        if(fIEVersion == 7) {
            return 7;
        } else if(fIEVersion == 8) {
            return 8;
        } else if(fIEVersion == 9) {
            return 9;
        } else if(fIEVersion == 10) {
            return 10;
        } else {
            return 6;//IE版本<=7
        }   
    } else if(isEdge) {
        return 'edge';//edge
    } else if(isIE11) {
        return 11; //IE11  
    }else{
        return -1;//不是ie浏览器
    }
  }
  
  function execCallback(fun, result, resolution) {
  	if(result === "" && resolution === "") {
  		result === i18n.t("detect_pass");
  	}
  	if (typeof fun === 'function') {
  		fun(result, resolution);
  	}
  }
  
  function parseURL(url, proto) {
    if (url.indexOf('://') === -1) {
      url = proto + '://' + url;
    }
    var a = document.createElement('a');
    a.href = url;
    return {
      source: url,
      protocol: a.protocol.replace(':',''),
      host: a.hostname,
      port: a.port,
      query: a.search,
      params: (function(){
        var ret = {},
          seg = a.search.replace(/^\?/,'').split('&'),
          len = seg.length, i = 0, s; //len = 2
        for (;i<len;i++) {
          if (!seg[i]) { continue; }
          s = seg[i].split('=');
          ret[s[0]] = s[1];
        }
        return ret;
      })(),
      file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
      hash: a.hash.replace('#',''),
      path: a.pathname.replace(/^([^\/])/,'/$1'),
      relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
      segments: a.pathname.replace(/^\//,'').split('/')
    };
  }
  
  function getMappingHost() {
  	var hostInfo = {
  		host: "",
  		port: ""
  	}
  	if (!sysinfo.ipConfig) {
  		return hostInfo;
  	}
  	for (var key in sysinfo.ipConfig) {
		if(key.indexOf(window.location.hostname) >= 0) {
			hostInfo.host = sysinfo.ipConfig[key]
			break;
		}
	}
	var parsed = parseURL(hostInfo.host, 'ws');
	hostInfo.host = parsed.host;
	hostInfo.port = parsed.port;
	return hostInfo;
  }
  
  var ClientFuns = {
  	// pc连接消息服务的端口检测
    1: function (row, callback) {
      var targetHost, targetPort;
      var hostInfo = getMappingHost();
      targetHost = hostInfo.host || sysinfo.openfireEmessageClientUrl;
      targetPort = hostInfo.port || sysinfo.openfireHttpbindPort;
      testWebSocket(targetHost, targetPort, function(pass) {
      	var result = "", resolution = "";
      	if(pass) {
      		result = i18n.t("detect_pass");
      	}else {
      		result = i18n.t("pcConnectServerError");
      		resolution = i18n.t("pcConnectServerError_detail0") + targetHost+ i18n.t("pcConnectServerError_detail1") +targetPort+ i18n.t("pcConnectServerError_detail2") +"<a href='https://yq.weaver.com.cn/eb/qa/view/#/question/71401bb39e2f4ae8b5cff979abe263c3' target='_blank'>"+ i18n.t("pcConnectServerError_detail3") +"</a>"
      	}
      	execCallback(callback, result, resolution);
      });
    },
    // web端加载检测
    8: function(row, callback) {
      	var versionNO = IEVersion();
      	var result = "", resolution = "";
    	if(versionNO > 0 && versionNO < 10){
    		result = i18n.t("currentBrowserNotSupport");
    		resolution = i18n.t("iegt9");
    	}
    	execCallback(callback, result, resolution);
    }
  }
  global.ClientFuns = ClientFuns
  global.ApiUrl = "/social/detect/interface/detect.jsp?m="
  global.testWebSocket = testWebSocket
})(window)