KEFU_ID=KEFU_ID!=""? KEFU_ID:"kefu2"; new Vue({ el: '#app', delimiters:["<{","}>"], data: { window:window, server:getWsBaseUrl()+"/ws_visitor", socket:null, msgList:[], msgListNum:[], messageContent:"", chatTitle:GOFLY_LANG[LANG]['connecting'], visitor:{}, face:[], showKfonline:false, socketClosed:false, focusSendConn:false, timer:null, sendDisabled:false, flyLang:GOFLY_LANG[LANG], textareaFocused:false, replys:[], noticeName:"", noticeAvatar:"", showIconBtns:false, showFaceIcon:false, isIframe:false, }, methods: { //初始化websocket initConn:function() { let socket = new ReconnectingWebSocket(this.server+"?visitor_id="+this.visitor.visitor_id);//创建Socket实例 socket.maxReconnectAttempts = 30; this.socket = socket this.socket.onmessage = this.OnMessage; this.socket.onopen = this.OnOpen; this.socket.onclose = this.OnClose; //心跳 this.ping(); }, OnOpen:function() { console.log("ws:onopen"); this.chatTitle=GOFLY_LANG[LANG]['connectok']; this.showTitle(this.chatTitle); this.socketClosed=false; this.focusSendConn=false; }, OnMessage:function(e) { console.log("ws:onmessage"); this.socketClosed=false; this.focusSendConn=false; const redata = JSON.parse(e.data); if (redata.type == "kfOnline") { let msg = redata.data if(this.showKfonline && this.visitor.to_id==msg.id){ return; } this.visitor.to_id=msg.id; this.showTitle(msg.name+","+GOFLY_LANG[LANG]['chating']); this.scrollBottom(); this.showKfonline=true; } if (redata.type == "transfer") { var kefuId = redata.data if(!kefuId){ return; } this.visitor.to_id=kefuId; } if (redata.type == "notice") { let msg = redata.data if(!msg){ return; } this.chatTitle=msg $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); } if (redata.type == "message") { let msg = redata.data this.visitor.to_id=msg.id; let content = {} content.avator = msg.avator; content.name = msg.name; content.content =replaceContent(msg.content); content.is_kefu = false; content.time = msg.time; this.msgList.push(content); notify(msg.name, { body: msg.content, icon: msg.avator },function(notification) { window.focus(); notification.close(); }); this.scrollBottom(); flashTitle();//标题闪烁 clearInterval(this.timer); this.alertSound();//提示音 } if (redata.type == "close") { this.chatTitle=GOFLY_LANG[LANG]['closemes']; $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); this.socket.close(); //this.socketClosed=true; this.focusSendConn=true; } if (redata.type == "force_close") { this.chatTitle=GOFLY_LANG[LANG]['forceclosemes']; $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); this.socket.close(); this.socketClosed=true; } if (redata.type == "auto_close") { this.chatTitle=GOFLY_LANG[LANG]['autoclosemes']; $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); this.socket.close(); this.socketClosed=true; } window.parent.postMessage(redata,"*"); }, //发送给客户 chatToUser:function() { var messageContent=this.messageContent.trim("\r\n"); messageContent=messageContent.replace("\n",""); messageContent=messageContent.replace("\r\n",""); if(messageContent==""||messageContent=="\r\n"){ this.messageContent=""; return; } this.messageContent=messageContent; if(this.socketClosed){ this.$message({ message: '连接关闭!请重新打开页面', type: 'warning' }); return; } this.sendDisabled=true; let _this=this; let content = {} content.avator=_this.visitor.avator; content.content = replaceContent(_this.messageContent); content.name = _this.visitor.name; content.is_kefu = true; content.time = _this.getNowDate(); content.show_time=false; _this.msgList.push(content); _this.scrollBottom(); let mes = {}; mes.type = "visitor"; mes.content = this.messageContent; mes.from_id = this.visitor.visitor_id; mes.to_id = this.visitor.to_id; mes.content = this.messageContent; //发送消息 $.post("/2/message",mes,function(res){ _this.sendDisabled=false; if(res.code!=200){ _this.msgList.pop(); _this.$message({ message: res.msg, type: 'error' }); return; } _this.messageContent = ""; clearInterval(_this.timer); _this.sendSound(); }); }, //正在输入 inputNextText:function(){ if(this.socketClosed||!this.socket){ return; } //console.log(this.messageContent); var message = {} message.type = "inputing"; message.data = { from : this.visitor.visitor_id, to : this.visitor.to_id, content:this.messageContent }; this.socket.send(JSON.stringify(message)); }, OnClose:function() { console.log("ws:onclose"); this.focusSendConn=true; //this.socketClosed=true; // this.chatTitle="连接关闭!请重新打开页面"; // $(".chatBox").append("
"+this.chatTitle+"
"); // this.scrollBottom(); }, //获取当前用户信息 getUserInfo:function(){ let obj=this.getCache("visitor"); var visitor_id="" var to_id=KEFU_ID; if(obj){ visitor_id=obj.visitor_id; //to_id=obj.to_id; } let _this=this; var extra=getQuery("extra"); //发送消息 $.post("/visitor_login",{visitor_id:visitor_id,refer:REFER,to_id:to_id,extra:extra},function(res){ if(res.code!=200){ _this.$message({ message: res.msg, type: 'error' }); _this.sendDisabled=true; return; } _this.visitor=res.result; _this.setCache("visitor",res.result); //_this.getMesssagesByVisitorId(); _this.initConn(); }); // }else{ // this.visitor=obj; // this.initConn(); // } }, //获取信息列表 getMesssagesByVisitorId:function(isAll){ let _this=this; $.ajax({ type:"get", url:"/messages?visitorId="+this.visitor.visitor_id, success: function(data) { if(data.code==200 && data.result!=null&&data.result.length!=0){ _this.msgListNum=data.result.length; let msgList=data.result; _this.msgList=[]; if(!isAll&&msgList.length>1){ var i=msgList.length-1 }else{ _this.msgListNum=0; var i=0; } for(;i