聊天本地缓存

This commit is contained in:
luyuan 2020-08-06 15:57:56 +08:00
parent 9054432b59
commit e6b7b50a1e
Signed by: theluyuan
GPG Key ID: A7972FD973317FF3
3 changed files with 161 additions and 15 deletions

View File

@ -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;
}
</style>

View File

@ -21,7 +21,7 @@
<image :src="item.avatar"></image>
</view>
<view class="title">{{item.name}}</view>
<view class="contentes">{{item.text}}</view>
<view class="contentes u-line-1">{{item.text}}</view>
<view class="times">{{item.time}}</view>
</view>
</view>
@ -129,7 +129,8 @@
onFriendListChange(onlineFriends) {
//todo:UniappMAPH5H5
//
this.information_dl = this.imService.friends;
console.log(onlineFriends,121212)
this.information_dl = onlineFriends;
this.$forceUpdate();
},

View File

@ -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;