From 652574f288c780320711da6e5019774d75bb689c Mon Sep 17 00:00:00 2001 From: taoshihan1991 <630892807@qq.com> Date: Mon, 4 Jan 2021 15:33:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=89=8D=E7=AB=AF=E6=9B=B4=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=B8=80=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- readme.md | 3 ++ static/html/chat_page.html | 6 ++-- static/js/chat-page.js | 64 +++++++++++++++++++++++--------------- tools/stringutil_test.go | 22 +++++++++++++ 4 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 tools/stringutil_test.go diff --git a/readme.md b/readme.md index 6d2b34e..af114a3 100644 --- a/readme.md +++ b/readme.md @@ -131,6 +131,9 @@ server{ 1. 需要先安装swag 2. 在根目录swag init -g go-fly.go ### 感谢赞助 + +2020年12月31日 **强 8.88元 + 2020年12月24日 **松 8.88元 2020年12月23日 **渊 10元 diff --git a/static/html/chat_page.html b/static/html/chat_page.html index c030340..1fa549b 100644 --- a/static/html/chat_page.html +++ b/static/html/chat_page.html @@ -66,10 +66,10 @@ @@ -79,5 +79,5 @@ var KEFU_ID='{{.KEFU_ID}}'; var REFER='{{.Refer}}'; - + diff --git a/static/js/chat-page.js b/static/js/chat-page.js index 72a35b2..4b5c879 100644 --- a/static/js/chat-page.js +++ b/static/js/chat-page.js @@ -19,7 +19,7 @@ new Vue({ }, methods: { //初始化websocket - initConn() { + initConn:function() { let socket = new ReconnectingWebSocket(this.server+"?visitor_id="+this.visitor.visitor_id);//创建Socket实例 socket.maxReconnectAttempts = 30; this.socket = socket @@ -29,7 +29,7 @@ new Vue({ //心跳 this.ping(); }, - OnOpen() { + OnOpen:function() { this.chatTitle="连接成功!" // let mes = {} // mes.type = "userInit"; @@ -37,7 +37,7 @@ new Vue({ // mes.data = this.visitor; // this.socket.send(JSON.stringify(mes)); }, - OnMessage(e) { + OnMessage:function(e) { const redata = JSON.parse(e.data); if (redata.type == "kfOnline") { let msg = redata.data @@ -84,10 +84,10 @@ new Vue({ this.socket.close(); this.socketClosed=true; } - window.parent.postMessage(redata); + //window.parent.postMessage(redata); }, //发送给客户 - chatToUser() { + chatToUser:function() { this.messageContent=this.messageContent.trim("\r\n"); if(this.messageContent==""||this.messageContent=="\r\n"){ this.$message({ @@ -136,12 +136,12 @@ new Vue({ }); }, - OnClose() { + OnClose:function() { this.chatTitle="连接关闭!请重新打开页面"; $(".chatBox").append("
"+this.chatTitle+"
"); }, //获取当前用户信息 - getUserInfo(){ + getUserInfo:function(){ let obj=this.getCache("visitor"); var visitor_id="" if(obj){ @@ -168,7 +168,7 @@ new Vue({ // } }, //获取信息列表 - getMesssagesByVisitorId(){ + getMesssagesByVisitorId:function(){ let _this=this; $.ajax({ type:"get", @@ -194,7 +194,7 @@ new Vue({ _this.msgList.push(content); _this.scrollBottom(); } - _this.$nextTick(() => { + _this.$nextTick(function(){ $(".chatBox").append("
—— 以上是历史消息 ——
"); }); } @@ -209,7 +209,7 @@ new Vue({ }, //滚动到底部 scrollBottom:function(){ - this.$nextTick(() => { + this.$nextTick(function(){ //debugger; $('body').scrollTop($("body")[0].scrollHeight); }); @@ -242,10 +242,14 @@ new Vue({ var i=0; if(len>0){ _this.timer=setInterval(function(){ - if(i>=len){ + if(i>=len||typeof msg[i]=="undefined"||msg[i]==null){ clearInterval(_this.timer); + return; } let content = msg[i]; + if(typeof content.content =="undefined"){ + return; + } content.content = replaceContent(content.content); _this.msgList.push(content); _this.scrollBottom(); @@ -259,7 +263,7 @@ new Vue({ } }); }, - initCss(){ + initCss:function(){ var _this=this; $(function () { //$(".chatContext").css("max-height",$(window).height()); @@ -283,7 +287,7 @@ new Vue({ }); }, //心跳 - ping(){ + ping:function(){ let _this=this; let mes = {} mes.type = "ping"; @@ -295,7 +299,7 @@ new Vue({ },10000); }, //初始化 - init(){ + init:function(){ this.initCss(); $("#app").click(function(){ clearTimeout(titleTimer); @@ -309,12 +313,12 @@ new Vue({ }); }, //表情点击事件 - faceIconClick(index){ + faceIconClick:function(index){ $('.faceBox').hide(); this.messageContent+="face"+this.face[index].name; }, //上传图片 - uploadImg (url){ + uploadImg:function (url){ let _this=this; $('#uploadImg').after(''); $("#uploadImgFile").click(); @@ -348,7 +352,7 @@ new Vue({ }); }, //上传文件 - uploadFile (url){ + uploadFile:function (url){ let _this=this; $('#uploadFile').after(''); $("#uploadRealFile").click(); @@ -384,7 +388,7 @@ new Vue({ }); }, //粘贴上传图片 - onPasteUpload(event){ + onPasteUpload:function(event){ let items = event.clipboardData && event.clipboardData.items; let file = null if (items && items.length) { @@ -426,18 +430,28 @@ new Vue({ }); }, //提示音 - alertSound(){ + alertSound:function(){ var b = document.getElementById("chatMessageAudio"); - var p = b.play(); - p && p.then(function(){}).catch(function(e){}); + if (b.canPlayType('audio/mpeg;')) { + b.type= 'audio/mpeg'; + b.src= '/static/images/alert2.ogg'; + var p = b.play(); + p && p.then(function () { + }).catch(function (e) { + }); + } }, - sendSound(){ + sendSound:function(){ var b = document.getElementById("chatMessageSendAudio"); - var p = b.play(); - p && p.then(function(){}).catch(function(e){}); + if (b.canPlayType('audio/mpeg;')) { + b.type= 'audio/mpeg'; + b.src= '/static/images/sent.ogg'; + var p = b.play(); + p && p.then(function(){}).catch(function(e){}); + } } }, - mounted() { + mounted:function() { document.addEventListener('paste', this.onPasteUpload) }, created: function () { diff --git a/tools/stringutil_test.go b/tools/stringutil_test.go new file mode 100644 index 0000000..4934577 --- /dev/null +++ b/tools/stringutil_test.go @@ -0,0 +1,22 @@ +package tools + +import "testing" + +func TestShowString(t *testing.T) { + ShowStringByte("hello,世界") +} +func TestReverse2(t *testing.T) { + cases := []struct { + in, want string + }{ + {"Hello, world", "dlrow ,olleH"}, + {"Hello, 世界", "界世 ,olleH"}, + {"", ""}, + } + for _, c := range cases { + got := Reverse2(c.in) + if got != c.want { + t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want) + } + } +}