From 53ec51d8f788e55b4cff0d5f1ef570c038993759 Mon Sep 17 00:00:00 2001 From: cleverqin <705597001@qq.com> Date: Mon, 23 Nov 2020 13:45:47 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=BA=86=E5=8F=91=E9=80=81?= =?UTF-8?q?=E6=96=87=E6=9C=AC=E6=B6=88=E6=81=AF=E4=BC=9A=E4=BA=A7=E7=94=9F?= =?UTF-8?q?xss=E6=94=BB=E5=87=BB=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 2 +- static/css/common.css | 26 +++++++++++++++++++++ static/index.html | 16 +++++++++---- static/js/index.js | 54 ++++++++++++++++++++++++++----------------- 4 files changed, 72 insertions(+), 26 deletions(-) diff --git a/app.js b/app.js index 51b5b19..147993d 100644 --- a/app.js +++ b/app.js @@ -93,7 +93,7 @@ io.sockets.on('connection',(socket)=>{ //删除登录用户信息,并通知所有在线用户 util.removeUser(socket.user.id); socket.broadcast.emit('system', socket.user, 'logout'); - store.saveUser(user,"logout"); + store.saveUser(socket.user,"logout"); } }); let userJson=socket.handshake.query.User; diff --git a/static/css/common.css b/static/css/common.css index a4a70d2..ac9f81a 100644 --- a/static/css/common.css +++ b/static/css/common.css @@ -252,10 +252,36 @@ html,body{ .message-item-send .message-content-container{ background-color: #0087f7; color: #f2f2f2; + position: relative; +} +.message-item-send .message-content-container:after{ + display: block; + content: ''; + position: absolute; + border-top: 5px solid transparent; + border-bottom: 5px solid transparent; + border-left: 5px solid #0087f7; + top: 15px; + left: 100%; +} +.message-item-receive .message-content-container{ + position: relative; + background-color: #ffffff; + color: #606266; } .message-item-receive .message-avatar{ float: left; } +.message-item-receive .message-content-container:before{ + display: block; + content: ''; + position: absolute; + border-top: 5px solid transparent; + border-bottom: 5px solid transparent; + border-right: 5px solid #ffffff; + top: 15px; + right: 100%; +} /****用户组件样式****/ .user-item-avatar{ display: block; diff --git a/static/index.html b/static/index.html index 72f7b74..924f8fa 100644 --- a/static/index.html +++ b/static/index.html @@ -44,7 +44,9 @@ :num="getUnReadNum(item.id)" :key="item.id">
{{getLatestMessage(item.id).time | friendlyTime}}
- + + +
【图片】
@@ -61,7 +63,9 @@
-
+
+ +
@@ -272,7 +276,9 @@ :num="getUnReadNum(item.id)" :key="item.id">
{{getLatestMessage(item.id).time | friendlyTime}}
- + + +
【图片】
@@ -385,7 +391,9 @@
-
+
+ +
diff --git a/static/js/index.js b/static/js/index.js index f2168a1..7498b23 100644 --- a/static/js/index.js +++ b/static/js/index.js @@ -346,27 +346,40 @@ {title:"[飞机]",url: "/飞机.gif"}, {title:"[气球]",url: "/气球.gif"} ]; + let mapData=[]; + expressions.forEach((item)=>{ + mapData[item.title]=item.url; + }); //渲染解析表情 - const renderExpression=function(text,baseUrl){ - let expressionMap={}; - expressions.forEach((item,i)=>{ - expressionMap[item.title]=i; - }); - if(typeof (text) != "undefined") { - let arr = text.match(/\[.*?\]/g); - if(arr&&arr.length>0){ - for(let i = 0; i < arr.length; i++){ - let index=expressionMap[arr[i]]; - if(index!==undefined) { - let url=baseUrl+expressions[index].url; - const img = ""; - text = text.replace(arr[i],img); - } - } + const MessageText=Vue.extend({ + props:{ + text:{ + type:String, + default:"" } + }, + render(h){ + const reg=/\[.*?\]/g; + let result=this.text.replace(reg,(word)=>{ + return "|"+word+"|"; + }); + let arr=result.split('|'); + return h('span', + arr.map((item)=>{ + if(reg.test(item)&&mapData[item]){ + return h('img',{ + class:"expression-img", + attrs:{ + src:mixin.data().baseUrl+mapData[item] + } + }) + }else { + return item; + } + }) + ) } - return text; - }; + }); //消息组件 const MessageItem=Vue.extend({ template:"#message-item", @@ -531,7 +544,7 @@ new Vue({ template: mainTpl, el:"#app", - components:{MessageItem,UserItem,Login}, + components:{MessageItem,UserItem,Login,MessageText}, mixins:[mixin], data(){ return { @@ -560,7 +573,6 @@ pickerExpression(item){ this.text+=item.title; }, - renderExpression, fileChange(e){ let file=e.target.files[0]; let maxSize=1*1024*1024; @@ -639,7 +651,7 @@ _this.socket.on("loginSuccess",(user,users)=>{ _this.loginUser=user; _this.onlineUsers=users; - document.title="聊天室 | "+user.name; + document.title=user.name+" | 聊天室"; }); _this.socket.on("loginFail",(message)=>{ AlterMessage(message,'error')