聊天本地缓存

This commit is contained in:
2020-08-06 15:57:56 +08:00
parent 9054432b59
commit e6b7b50a1e
3 changed files with 161 additions and 15 deletions

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;