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;
|
var localHostUrl = hostUrl;
|
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 (e) {
|
// 连接建立之后执行
|
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) {
|
/* timeron=setInterval(function () {
|
//这里会定时3秒执行一次,直到成功为止,条件是timeron==null;
|
if(timeron!=null) {
|
console.log(timeron + ',onError,30秒后重连', e);
|
initWebsocket(localHostUrl, localUserCode, localDbId, localSessionId, userType);
|
}
|
}, 30000);*/
|
};
|
|
websocket.onclose = function (e) {
|
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||json.info.msgType==6015){
|
//重定向到登录页面
|
setTimeout(function (){
|
location="/login.jsp";
|
},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";
|
}
|
});
|
|
}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==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);
|
}
|
}
|
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(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," ");
|
//替换所有的换行符
|
myMessages = myMessages.replace(/\r\n/g,"<br>")
|
myMessages = myMessages.replace(/\n/g,"<br>");
|
//替换所有的空格(中文空格、英文空格都会被替换)
|
// myMessages = myMessages.replace(/\s/g," ");
|
myMessages +="<br/><br/><hr/>" ;
|
myMessages += json["createusername"] + " "
|
+ json["createtime"] ;
|
openToast(myMessages,json["topic"],messageType);
|
}
|