KEFU_ID=KEFU_ID!=""? KEFU_ID:"kefu2"; new Vue({ el: '#app', delimiters:["<{","}>"], data: { window:window, server:getWsBaseUrl()+"/chat_server", socket:null, msgList:[], messageContent:"", chatTitle:"正在连接...", visitor:{}, face:[], showKfonline:false, socketClosed:false, }, methods: { //初始化websocket initConn() { let socket = new ReconnectingWebSocket(this.server);//创建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() { this.chatTitle="连接成功!" let mes = {} mes.type = "userInit"; this.visitor.refer=REFER; mes.data = this.visitor; this.socket.send(JSON.stringify(mes)); }, OnMessage(e) { 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.chatTitle=msg.name+",正在与您沟通!" $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); this.showKfonline=true; } 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,true); content.is_kefu = false; content.time = msg.time; this.msgList.push(content); //this.saveHistory(content); this.scrollBottom(); flashTitle();//标题闪烁 this.alertSound();//提示音 } if (redata.type == "close") { this.chatTitle="连接关闭!请重新打开页面"; $(".chatBox").append("
"+this.chatTitle+"
"); this.scrollBottom(); this.socket.close(); this.socketClosed=true; } window.parent.postMessage(redata); }, //发送给客户 chatToUser() { this.messageContent=this.messageContent.trim("\r\n"); if(this.messageContent==""||this.messageContent=="\r\n"){ this.$message({ message: '不能发送空白信息', type: 'warning' }); return; } if(this.socketClosed){ this.$message({ message: '连接关闭!请重新打开页面', type: 'warning' }); return; } let _this=this; 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("/message",mes,function(res){ if(res.code!=200){ _this.$message({ message: res.msg, type: 'error' }); return; } 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(); _this.msgList.push(content); //_this.saveHistory(content); _this.scrollBottom(); _this.messageContent = ""; }); }, OnClose() { this.chatTitle="连接关闭!请重新打开页面"; $(".chatBox").append("
"+this.chatTitle+"
"); }, //获取当前用户信息 getUserInfo(){ let obj=this.getCache("visitor"); if(!obj){ let _this=this; //发送消息 $.post("/visitor_login",{refer:REFER,to_id:KEFU_ID,client_ip:returnCitySN["cip"],},function(res){ if(res.code!=200){ _this.$message({ message: res.msg, type: 'error' }); return; } _this.visitor=res.result; _this.setCache("visitor",res.result); _this.initConn(); }); }else{ this.visitor=obj; this.initConn(); } }, //获取信息列表 getMesssagesByVisitorId(){ 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){ let msgList=data.result; _this.msgList=[]; for(let i=0;i { $(".chatBox").append("
—— 以上是历史消息 ——
"); }); } if(data.code!=200){ _this.$message({ message: data.msg, type: 'error' }); } } }); }, //滚动到底部 scrollBottom:function(){ this.$nextTick(() => { //debugger; $('body').scrollTop($("body")[0].scrollHeight); }); }, //获取日期 getNowDate : function() {// 获取日期 var d = new Date(new Date()); return d.getFullYear() + '-' + this.digit(d.getMonth() + 1) + '-' + this.digit(d.getDate()) + ' ' + this.digit(d.getHours()) + ':' + this.digit(d.getMinutes()) + ':' + this.digit(d.getSeconds()); }, //补齐数位 digit : function (num) { return num < 10 ? '0' + (num | 0) : num; }, setCache : function (key,obj){ if(typeof(Storage) !== "undefined"){ localStorage.setItem(key, JSON.stringify(obj)); } },getCache : function (key){ return JSON.parse(localStorage.getItem(key)); }, //获取自动欢迎语句 getNotice : function (){ let _this=this; $.get("/notice?kefu_id="+KEFU_ID,function(res) { //debugger; if (res.result != null) { let msg = res.result; for(let i=0;i