You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
6.1 KiB
166 lines
6.1 KiB
import storage from '@/uni_modules/vrapile-im/utils/storage'
|
|
import constant from '@/uni_modules/vrapile-im/utils/constant'
|
|
import { getChatKey } from '@/uni_modules/vrapile-im/utils/tiosocket';
|
|
|
|
// 外部需提供获取用户好友,获取用户所有群,以及群中所有用户三个接口
|
|
import { getUserAllFriend, getUserAllGroup, getGroupUser } from '@/api/chat'
|
|
|
|
const chat = {
|
|
state: {
|
|
friendList: storage.get(constant.friendList) || [],
|
|
groupList: storage.get(constant.groupList) || [],
|
|
userObject: storage.get(constant.userObject) || {},
|
|
groupObject: storage.get(constant.groupObject) || {},
|
|
chatList: storage.get(constant.chatList) || []
|
|
},
|
|
|
|
mutations: {
|
|
// 清空聊天列表
|
|
CLEAN_CHAT_LIST: (state) => {
|
|
state.chatList = new Array();
|
|
storage.set(constant.chatList, new Array());
|
|
state.friendList = new Array();
|
|
storage.set(constant.friendList, new Array());
|
|
state.groupList = new Array();
|
|
storage.set(constant.groupList, new Array());
|
|
state.userObject = {};
|
|
storage.set(constant.userObject, {});
|
|
state.groupObject = {};
|
|
storage.set(constant.groupObject, {});
|
|
},
|
|
// 存储好友聊天列表
|
|
SET_CHAT_FRIEND_LIST: (state, data) => {
|
|
state.friendList = data;
|
|
storage.set(constant.friendList, data)
|
|
|
|
for(let item of data){
|
|
item["key"] = getChatKey(item["type"], item["friendId"], item["userId"])
|
|
item["haveHistory"] = 1;
|
|
item["id"] = item["friendId"];
|
|
item["name"] = item["remarkName"] || item["nickName"];
|
|
item["messageNum"] = 0;
|
|
item["messageList"] = [];
|
|
item["messageShow"] = "";
|
|
item["messageLast"] = {};
|
|
state.chatList.push(item)
|
|
}
|
|
storage.set(constant.chatList, state.chatList)
|
|
},
|
|
// 存储群聊列表
|
|
SET_CHAT_GROUP_LIST: (state, data) => {
|
|
state.groupList = data;
|
|
storage.set(constant.groupList, data)
|
|
|
|
for(let item of data){
|
|
item["key"] = getChatKey(item["type"], item["id"], item["userId"])
|
|
item["haveHistory"] = 1;
|
|
item["id"] = item["id"];
|
|
item["name"] = item["remarkName"] || item["name"]
|
|
item["messageNum"] = 0;
|
|
item["messageList"] = [];
|
|
item["messageShow"] = "";
|
|
item["messageLast"] = {};
|
|
state.chatList.push(item)
|
|
}
|
|
storage.set(constant.chatList, state.chatList)
|
|
},
|
|
// 存储用户对象,方便寻找用户名,头像等
|
|
SET_USER_OBJECT: (state, userObject) => {
|
|
state.userObject[userObject.userId] = userObject;
|
|
storage.set(constant.userObject, state.userObject);
|
|
},
|
|
// 存储群组对象,方便寻找群组名,头像等
|
|
SET_GROUP_OBJECT: (state, groupObject) => {
|
|
state.groupObject[groupObject.id] = groupObject;
|
|
storage.set(constant.groupObject, state.groupObject);
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
// 获取登录用户所有聊天列表
|
|
GetChatList({ commit, state }, params) {
|
|
return new Promise((resolve, reject) => {
|
|
commit('CLEAN_CHAT_LIST');
|
|
// 查询好友
|
|
getUserAllFriend().then(res => {
|
|
let listFriend = []
|
|
for(let itemFriend of res.data){
|
|
delete itemFriend["createBy"]
|
|
delete itemFriend["updateBy"]
|
|
delete itemFriend["createTime"]
|
|
delete itemFriend["updateTime"]
|
|
listFriend.push({type: 0, ...itemFriend})
|
|
commit('SET_USER_OBJECT', {...itemFriend, userId: itemFriend.friendId})
|
|
}
|
|
commit('SET_CHAT_FRIEND_LIST', listFriend)
|
|
|
|
// 查询群组
|
|
getUserAllGroup().then(res1 => {
|
|
let listGroup = []
|
|
for(let itemGroup of res1.data){
|
|
delete itemGroup["createBy"];
|
|
delete itemGroup["updateBy"];
|
|
delete itemGroup["createTime"];
|
|
delete itemGroup["updateTime"];
|
|
listGroup.push({type: 1, ...itemGroup});
|
|
// 查询群组人员
|
|
// 后面将作废此接口 改为批量查询 /im/groupUser/getPatchGroupUser
|
|
getGroupUser(itemGroup.id).then(res2 => {
|
|
for(let itemGroupUser of res2.data){
|
|
delete itemGroupUser["createBy"];
|
|
delete itemGroupUser["updateBy"];
|
|
delete itemGroupUser["createTime"];
|
|
delete itemGroupUser["updateTime"];
|
|
delete itemGroupUser["groupId"];
|
|
delete itemGroupUser["groupName"];
|
|
commit('SET_USER_OBJECT', itemGroupUser);
|
|
}
|
|
});
|
|
delete itemGroup["createBy"];
|
|
delete itemGroup["updateBy"];
|
|
delete itemGroup["createTime"];
|
|
delete itemGroup["updateTime"];
|
|
commit('SET_GROUP_OBJECT', itemGroup);
|
|
}
|
|
commit('SET_CHAT_GROUP_LIST', listGroup);
|
|
}).catch(error => {
|
|
reject(error);
|
|
})
|
|
}).catch(error => {
|
|
reject(error);
|
|
}).finally(() => {
|
|
resolve();
|
|
})
|
|
})
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
getUserAvatar: (state) => (userId, defaultAvatar) => {
|
|
if(state.userObject[userId] && state.userObject[userId]["avatar"]){
|
|
return state.userObject[userId]["avatar"]
|
|
}
|
|
return defaultAvatar || '/uni_modules/vrapile-im/static/image/yy.png';
|
|
},
|
|
getGroupAvatar: (state) => (groupId, defaultAvatar) => {
|
|
if(state.groupObject[groupId] && state.groupObject[groupId]["avatar"]){
|
|
return state.groupObject[groupId]["avatar"]
|
|
}
|
|
return defaultAvatar || '/uni_modules/vrapile-im/static/image/yy.png';
|
|
},
|
|
getUserName: (state) => (userId, defaultName) => {
|
|
if(state.userObject[userId]){
|
|
return state.userObject[userId]["remarkName"] || state.userObject[userId]["nickName"] || defaultName
|
|
}
|
|
return defaultName || "无名";
|
|
},
|
|
getGroupName: (state) => (groupId, defaultName) => {
|
|
if(state.groupObject[groupId]){
|
|
return state.groupObject[userId]["remarkName"] || state.groupObject[userId]["name"] || defaultName
|
|
}
|
|
return defaultName || "无名";
|
|
}
|
|
}
|
|
}
|
|
|
|
export default chat
|