From e6b7b50a1e9c0297e38d0cd0a97e14403d248052 Mon Sep 17 00:00:00 2001 From: luyuan <1162963624@qq.com> Date: Thu, 6 Aug 2020 15:57:56 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=81=8A=E5=A4=A9=E6=9C=AC=E5=9C=B0?= =?UTF-8?q?=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pageD/privateChat/privateChat.vue | 87 ++++++++++++++++++++++++++++++- pages/information/information.vue | 5 +- static/imservice.js | 84 +++++++++++++++++++++++++---- 3 files changed, 161 insertions(+), 15 deletions(-) diff --git a/pageD/privateChat/privateChat.vue b/pageD/privateChat/privateChat.vue index 17aadff..b6ef7c7 100644 --- a/pageD/privateChat/privateChat.vue +++ b/pageD/privateChat/privateChat.vue @@ -193,6 +193,10 @@ sendMessage() {//发送消息 if (this.content.trim() != '') { this.imService.sendPrivateTextMessage(this.friend.uuid, this.content); + let that = this + setTimeout(function(){ + that.scrollToBottom(); + },500) } this.content = ""; }, @@ -322,7 +326,7 @@ } .chatInterface .message-item{ - max-height: 400rpx; + /* max-height: 400rpx; */ margin-top: 40rpx; overflow: hidden; } @@ -342,7 +346,7 @@ .chatInterface .content{ font-size: 34rpx; line-height: 44rpx; - max-height: 400rpx; + /* max-height: 400rpx; */ display: flex; align-items: center; justify-content: right; @@ -356,6 +360,7 @@ .chatInterface .content .text-content{ padding: 16rpx; border-radius: 12rpx; + max-width: 520rpx; } .chatInterface .content .pending{ background: url("../images/pending.gif") no-repeat center; @@ -528,5 +533,83 @@ left: 50%; margin:-20rpx; } + .chatInterface .avatar{ + overflow: hidden; + float: left; + } + .chatInterface .avatar img{ + width: 100%; + height: 100%; + display: block; + } + .chatInterface .content{ + float: left; + overflow: hidden; + } + .chatInterface .content span{ + font-family: Source Han Sans CN; + letter-spacing: -0.41px; + color: #262628; + background: #efefef; + display: inline-block; + word-break: break-all; + } + .chatInterface .message-item.self{ + margin-right: 0; + } + .chatInterface .message-item.self .avatar{ + margin-right: 0; + float: right; + } + .chatInterface .message-item.self .content{ + text-align: right; + float: right; + } + .chatInterface .message-item.self .content span{ + color: #ffffff; + background:#D02129; + word-break: break-all; + text-align: left; + max-width: 520rpx; + } + .chatInterface .action-box{ + background: #FAFAFA; + display: flex; + align-content: center; + align-items: center; + } + .chatInterface .message-input{ + background: #efefef; + border: 0; + outline: none; + } + + .chatInterface .send-message-btn{ + flex-grow: 1; + text-align: center; + color: #95949A; + } + + .chatInterface .member-layer{ + width:100%; + height: 100%; + background: #FFFFFF; + top: 0; + left: 0; + position: absolute; + } + .member-layer .member{ + display: flex; + flex-wrap: wrap; + } + + .chatInterface .group-icon{ + position: absolute; + } + + .chatInterface .gray{ + color: gray!important; + text-decoration: none!important; + } diff --git a/pages/information/information.vue b/pages/information/information.vue index 2bee790..d901848 100644 --- a/pages/information/information.vue +++ b/pages/information/information.vue @@ -21,7 +21,7 @@ {{item.name}} - {{item.text}} + {{item.text}} {{item.time}} @@ -129,7 +129,8 @@ onFriendListChange(onlineFriends) { //todo:比较垃圾的处理方式,因为Uniapp的基于MAP的双向绑定在H5端不稳定,H5端偶尔会抽风 // 下边这一行删掉,有时候页面可以更新,有时候不行,大家有优雅的方式,也欢迎跟我们沟通 - this.information_dl = this.imService.friends; + console.log(onlineFriends,121212) + this.information_dl = onlineFriends; this.$forceUpdate(); }, diff --git a/static/imservice.js b/static/imservice.js index 2fa0de8..23f1c56 100644 --- a/static/imservice.js +++ b/static/imservice.js @@ -8,14 +8,14 @@ import GoEasyIM from './goeasy-im-1.0.9'; import restApi from './restapi'; -function Friend(uuid, name, avatar) { +function Friend(uuid, name, avatar,time = "") { this.uuid = uuid; this.name = name; this.avatar = avatar; this.online = false; this.unReadMessage = 0; this.text = ""; - this.time = ""; + this.time = time; } function Group(uuid, name, avatar) { @@ -40,6 +40,7 @@ function IMService() { this.currentUser = null; //我的好友 this.friends = {}; + this.friendsarr = []; //我的群 this.groups = {}; //私聊消息记录,map格式,每个好友对应一个数组 @@ -74,7 +75,7 @@ IMService.prototype.login = function (uuid, name, avatar) { //初始化当前用户 this.currentUser = new CurrentUser(uuid, name, avatar); //初始化联系人信息,包括群 - // this.initialContacts(); + this.initialContacts(); return true; }; @@ -83,11 +84,36 @@ IMService.prototype.login = function (uuid, name, avatar) { IMService.prototype.initialContacts = function (friendList) { //查询并初始化好友信息 // let friendList = restApi.findFriends(this.currentUser); - + let value = uni.getStorageSync('imlist'); + if(value != undefined && !value){ + return ; + } + value = JSON.parse(value) + let that = this + for(let i of value){ + const token = uni.getStorageSync('token'); + console.log(token) + uni.request({ + url:"https://dmmall.sdbairui.com/api/Specialci/getAtwillUserInfo", + data:{ + userId:i[0] + }, + method:"POST", + header:{ + "Authorization" : 'Bearer' + " " + token + }, + success(res){ + console.log(res) + that.friends[i[0]] = new Friend(res.data.data.member_id, res.data.data.member_nickname, res.data.data.member_avatar,i[1]); + console.log(that.friends) + that.onFriendListChange(that.friends); + } + }) + } //将用户列表初始化为一个map,便于后续根据friendId得到friend - friendList.map(friend => { - this.friends[friend.uuid] = new Friend(friend.uuid, friend.name, friend.avatar); - }); + // friendList.map(friend => { + // this.friends[friend.uuid] = new Friend(friend.uuid, friend.name, friend.avatar); + // }); //查询并初始化与自己相关的群信息 // let groupList = restApi.findGroups(this.currentUser); @@ -207,6 +233,19 @@ IMService.prototype.initialIMListeners = function () { let friend = this.friends[message.senderId]; console.log(friend) // return ; + let sorts = function (friends){ + let paixu = function (a,b){ + if(a.item > b.item){ + return 0; + }else{ + return 1; + } + + } + friends.sort(paixu) + + } + let that = this if(!friend && friend == undefined){ const token = uni.getStorageSync('token'); @@ -229,8 +268,20 @@ IMService.prototype.initialIMListeners = function () { friend.text = message.type != "text" ? "其他消息" : message.payload.text let time = new Date(message.timestamp) friend.time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() - that.onFriendListChange(that.friends); console.log(that.friends) + that.friendsarr = [] + for(let i in that.friends){ + console.log(i) + that.friendsarr.push(that.friends[i]) + } + sorts(that.friendsarr) + let arr = [] + for(let i in that.friends){ + arr.push([that.friends[i].uuid,that.friends[i].time]) + uni.setStorageSync('imlist',JSON.stringify(arr)) + } + that.onFriendListChange(that.friends); + } }) }else{ @@ -239,11 +290,22 @@ IMService.prototype.initialIMListeners = function () { friend.text = message.type != "text" ? "其他消息" : message.payload.text let time = new Date(message.timestamp) friend.time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() - this.onFriendListChange(this.friends); console.log(this.friends) + that.friendsarr = [] + for(let i in this.friends){ + that.friendsarr.push(this.friends[i]) + } + sorts(that.friendsarr) + this.onFriendListChange(this.friends); + } - - + // let value = uni.getStorageSync('imlist'); + // value = JSON.parse(value) + // if(value.indexOf(message.senderId) == -1){ + // value.unshift(message.senderId) + // } + // uni.setStorageSync('imlist',JSON.stringify(value)) + } //更新私聊消息记录 let friendId; From e11f20885fb3d12f6b8e68277917451d3c9e0518 Mon Sep 17 00:00:00 2001 From: luyuan <1162963624@qq.com> Date: Thu, 6 Aug 2020 16:22:40 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- static/imservice.js | 43 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/static/imservice.js b/static/imservice.js index 23f1c56..070908c 100644 --- a/static/imservice.js +++ b/static/imservice.js @@ -8,14 +8,15 @@ import GoEasyIM from './goeasy-im-1.0.9'; import restApi from './restapi'; -function Friend(uuid, name, avatar,time = "") { +function Friend(uuid, name, avatar,time = "", text = "",date = "") { this.uuid = uuid; this.name = name; this.avatar = avatar; this.online = false; this.unReadMessage = 0; - this.text = ""; + this.text = text; this.time = time; + this.date = date } function Group(uuid, name, avatar) { @@ -90,6 +91,7 @@ IMService.prototype.initialContacts = function (friendList) { } value = JSON.parse(value) let that = this + console.log(value) for(let i of value){ const token = uni.getStorageSync('token'); console.log(token) @@ -104,9 +106,28 @@ IMService.prototype.initialContacts = function (friendList) { }, success(res){ console.log(res) - that.friends[i[0]] = new Friend(res.data.data.member_id, res.data.data.member_nickname, res.data.data.member_avatar,i[1]); + that.friends[i[0]] = new Friend(res.data.data.member_id, res.data.data.member_nickname, res.data.data.member_avatar,i[1],i[2]); console.log(that.friends) + let sorts = function (friends){ + let paixu = function (a,b){ + if(a.date > b.date){ + return 0; + }else{ + return 1; + } + + } + friends.sort(paixu) + + } + that.friendsarr = [] + for(let i in that.friends){ + console.log(i) + that.friendsarr.push(that.friends[i]) + } + sorts(that.friendsarr) that.onFriendListChange(that.friends); + } }) } @@ -235,7 +256,7 @@ IMService.prototype.initialIMListeners = function () { // return ; let sorts = function (friends){ let paixu = function (a,b){ - if(a.item > b.item){ + if(a.date > b.date){ return 0; }else{ return 1; @@ -267,6 +288,7 @@ IMService.prototype.initialIMListeners = function () { friend.unReadMessage++; friend.text = message.type != "text" ? "其他消息" : message.payload.text let time = new Date(message.timestamp) + friend.date = message.timestamp friend.time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() console.log(that.friends) that.friendsarr = [] @@ -277,9 +299,11 @@ IMService.prototype.initialIMListeners = function () { sorts(that.friendsarr) let arr = [] for(let i in that.friends){ - arr.push([that.friends[i].uuid,that.friends[i].time]) - uni.setStorageSync('imlist',JSON.stringify(arr)) + arr.push([that.friends[i].uuid,that.friends[i].time,that.friends[i].text,that.friends[i].date]) } + console.log(arr) + uni.setStorageSync('imlist',JSON.stringify(arr)) + that.onFriendListChange(that.friends); } @@ -289,6 +313,7 @@ IMService.prototype.initialIMListeners = function () { friend.unReadMessage++; friend.text = message.type != "text" ? "其他消息" : message.payload.text let time = new Date(message.timestamp) + friend.date = message.timestamp friend.time = time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() console.log(this.friends) that.friendsarr = [] @@ -296,6 +321,12 @@ IMService.prototype.initialIMListeners = function () { that.friendsarr.push(this.friends[i]) } sorts(that.friendsarr) + let arr = [] + for(let i in that.friends){ + arr.push([that.friends[i].uuid,that.friends[i].time,that.friends[i].text,that.friends[i].date]) + } + console.log(arr) + uni.setStorageSync('imlist',JSON.stringify(arr)) this.onFriendListChange(this.friends); }