xinyb
2024-09-19 18ffbca9acaccd5099a7a63652f52210f59a7e40
WebRoot/js/index/WebSocketMessage.js
@@ -1,5 +1,7 @@
var timeron=null;
function initWebsocket(hostUrl,userCode,dbId,sessionId,userType) {
var wsOnTimers=null;
var layerMaintenanceIndex=null;
var reloginIndex=null;
function initWebsocket(hostUrl,userCode,dbId,sessionId,userType,isColseLayer) {
//  初始化weosocket
    var userFromType=1;
    if(userType!=undefined) userFromType=userType;
@@ -7,51 +9,213 @@
    var localUserCode = userCode ;
    var localDbId = dbId;
    var localSessionId = sessionId ;
    var colseLayer=isColseLayer;
    if(localUserCode==null||localUserCode==""||localUserCode=="null") localUserCode="temp_userCode_"+Math.random();
    if(localDbId==null||localDbId==""||localDbId=="null") localDbId=Math.floor(Math.random() * (10000 - 2000)) + 2000;
    if(localUserCode==null||localUserCode==""||localUserCode=="null") localUserCode="temp_session_"+Math.random();
    var info = '/ws/websocketMessage/'+localUserCode + '/'+ localDbId+'/' + localSessionId+'/'+userFromType ;
    var url = localHostUrl + info ;
    websocket = new WebSocket(url);
    
    websocket.onopen = function () {
    websocket.onopen = function (e) {
        //  连接建立之后执行
        //console.log('WebSocket连接成功');
        // this.connectUser()
        // this.$indicator.close()
        clearTimeout(timeron);//by danaus 2020/3/20 20:42
        console.log('WebSocket连接成功');
        if(wsOnTimers!=null){
            //console.log("onOpen-wsOnTimers:"+wsOnTimers);
            clearInterval(wsOnTimers);
            wsOnTimers=null;
        }
        if(colseLayer==true){
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
            parent.layer.close(index); //再执行关闭
        }
      }
      websocket.onerror = function (e) {
          //  连接建立失败3S重连
          timeron=setTimeout(function () {
              initWebsocket(localHostUrl,localUserCode,localDbId,localSessionId,userType);
          }, 3000);
          console.log('连接建立失败,3秒后重连', e);
         /* timeron=setInterval(function () {
              //这里会定时3秒执行一次,直到成功为止,条件是timeron==null;
              if(timeron!=null) {
                  console.log(timeron + ',onError,30秒后重连', e);
                  initWebsocket(localHostUrl, localUserCode, localDbId, localSessionId, userType);
              }
          }, 30000);*/
          this.websocket=null;
        };
        websocket.onclose = function (e) {
          //  连接建立失败3S重连
          // setTimeout(function () {
          // initWebsocket(localHostUrl,localUserCode,localDbId,localSessionId);
          // }, 3000);
            if (webSocket != null){
                websocket.close();
            }
          console.log('断开连接', e);
            this.websocket=null;
            wsOnTimers=setInterval(function () {
                //这里会定时3秒执行一次,直到成功为止,条件是timeron==null;
                if(wsOnTimers!=null) {
                    console.log(wsOnTimers + ',onClose,10秒后重连', e);
                    initWebsocket(localHostUrl, localUserCode, localDbId, localSessionId, userType);
                }
                }, 10000);
           // console.log("wsOnTimers:"+wsOnTimers);
        };
      
      // 接收消息
      websocket.onmessage = function (e) {
            var json= JSON.parse(e.data);
            if(json.info.msgType==6014){
            if(json.info.msgType==6014||json.info.msgType==6015){
                //重定向到登录页面
                location="/login.jsp?m=6015&d="+json.info.dbId;
            }else if(json.info.msgType==6015){
                //刷新当前页面
                location="/login.jsp";
                setTimeout(function (){
                    location="/login.jsp?dbId="+json.info.dbId;//如果不指定dbid会造成取到同一个域名的其他数据源问题
                },500);
            }else if(json.info.msgType==6016){
              //重定向到登录页面
               /* $.messager.show("提示", "您的账户已在其他设备浏览器登录,点击确定后返回登录页面", "info", function () {
                    location="/login.jsp";
                });*/
                layer.open({
                    title: '提示'
                    ,content: json.info.msg
                    ,closeBtn:0
                    ,yes: function(index, layero){
                        //do something
                        layer.close(index); //如果设定了yes回调,需进行手工关闭
                        location="/login.jsp?dbid="+json.info.dbId;
                    }
                });
            }else if(json.info.msgType==6017){
                if(reloginIndex==null) {
                    reloginIndex = layui.layer.open({
                        type: 2,
                        area: ['400px', '200px'],
                        closeBtn: 0,
                        isOutAnim: false,
                        title: [json.info.msg, 'font-size:18px;text-align:center;padding:0px'],
                        resize: false,
                        fixed: false,
                        content: '/reloadsession.jsp?userCode=' + userCode,
                        move: false,
                        success: function (layero, index) {
                        }
                    });
                }
            }else if(json.info.msgType==6019||json.info.msgType==6020||json.info.msgType==6022){
                //百望云扫脸,6019表示扫脸成功,6020表示二维码失效,需要重新刷新
                try {
                   top.layIframeWindow.qrCodeFail(json);
                }catch (e){console.log(e)}
            }else if(json.info.msgType==6021){
                $.messager.alert('提示', json.info.msg, "info", function () {
                    window.open(json.info.url,'',' width='+ (screen.availWidth - 10) +',height='+ (screen.availHeight-50) +',scrollbars,resizable=yes,toolbar=no');
                    $(top.layeerFace).css("display", "none");
                    return;
                });
            }else if(json.info.msgType==6023){
                top.layIframeWindow.goalDom.qrCodeRefreshFun();
            }else if(json.info.msgType==6002){
                //右下角弹窗
                //console.log('接收消息', json);
                popMessage(json.info.msg);
            }else if(json.info.msgType==6999){
                //显示维护界面
                if(layerMaintenanceIndex==null) {
                    layerMaintenanceIndex = layer.open({
                        title: '系统维护',
                        type: 0,
                        resize: false,
                        area: ['800px', '600px'],
                        btn: null,
                       // closeBtn: 0,
                       // cancel: function(){
                       //     layerMaintenanceIndex=null;
                       // },
                        content: '<img style="width: 800px" src="/images/maintenance.jpg">'
                    });
                }
            }else if(json.info.msgType==6998){
                //关闭维护界面
                if(layerMaintenanceIndex!=null){
                    layer.close(layerMaintenanceIndex);
                    layerMaintenanceIndex=null;
                }
            }
        // console.log('接收消息', json);
         //console.log('接收消息', json);
      }
}
var popflag=false;
function popMessage(message){
    var json=JSON.parse(message);
    var message6002="";
    var colt=0;
    var zong=1;
    for(var i=0;i<json.length;i++){
        if($.cookie("dbid")!=undefined&&$.cookie("dbid")!=json[i]["dbid"]){
            //不是当前数据源的消息不弹出 by danaus 2024-08-08 14:49
            continue;
        }
        if(json[i]["isPublicUser"]==1){
            funB6003(json[i]);
        }else{
            message6002+=getMessage6002(json[i],colt,zong);
             colt++;
             zong++;
        }
    }
    if(message6002!="") {
        funB6002(message6002);
    }
}
function messageToString(obj) {
    if(obj==null)
        return "";
    else
        return obj+"";
}
function getMessage6002(entity,colt,zong) {
    var message="";
    var Bcolor="";
        var messagetxt =entity["messagetxt"].replaceAll("(\\n|\\r|(\\r\\n)|(\u0085)|(\u2028)|(\u2029))","");
        if (colt % 2 == 0) {
            Bcolor = "background:#FFFFFF;";
        } else {
            Bcolor = "background:#DDDDDD;";
        }
        if (entity["formid"] != undefined) {
            message="<div style=" + Bcolor + "><a id="+entity["messid"]+" style=line-height:15pt;cursor:pointer;text-decoration:underline; onClick=funcLink('"+entity["formid"]+"','"+entity["formtype"]+"','"+messageToString(entity["origfields"]).replace("'", "")+"','"+messageToString(entity["linkfields"]).replace("'", "")+"','"+entity["linkmode"]+"','"+messageToString(entity["self_datafields"])+"','"+messageToString(entity["link_datafields"])+"','"+"','"+messageToString(entity["efilter"]).replace("'", "\\'")+"','"+"','"+"','"+"','"+"','"+"','"+entity["messid"]+",message');>"+zong + ":  "+messagetxt+"</a></div>";
        } else if (entity["url"]!=undefined&&entity["url"]!="") {
            var url = "";
            if (entity["url"].indexOf("http://")<0) {
                url = "http://" + entity["url"];
            } else {
                url = entity["url"];
            }
            message="<div style=" + Bcolor + "><a style=line-height:15pt;cursor:pointer;text-decoration:underline;  onClick=window.open('"+url+"')>"+zong + ":  "+messagetxt+"</a></div>";
        } else {
            message="<div style=" + Bcolor + "><a id="+entity["messid"]+" style=line-height:15pt;" + Bcolor + "cursor:pointer;>"+zong + ":  "+messagetxt+"</a></div>";
        }
    return message;
}
function funB6002(message){
    var init = $.cookie(userCode);
    if(init==null){
        $.cookie(userCode,popflag);
    }
    if( $.cookie(userCode)=="false"){
        $.messagerTip.lays(300, 220);
        $.messagerTip.show(0, message);
        popflag= $("input[name='isTip']").is(":checked");
        $.cookie(userCode,popflag,{expires: 1})
    }
}
function funB6003(json) {
    var messageType = json["messagetype"] ;
    var myMessages = json["messagetxt"]  ;
    myMessages = myMessages.replace(/\t/g,"&nbsp;&nbsp;");
    //替换所有的换行符
    myMessages = myMessages.replace(/\r\n/g,"<br>")
    myMessages = myMessages.replace(/\n/g,"<br>");
    //替换所有的空格(中文空格、英文空格都会被替换)
    // myMessages = myMessages.replace(/\s/g,"&nbsp;");
    myMessages +="<br/><br/><hr/>" ;
    myMessages += json["createusername"] + "&nbsp;&nbsp;"
        + json["createtime"] ;
    openToast(myMessages,json["topic"],messageType);
}