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.
138 lines
4.4 KiB
138 lines
4.4 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, state.chatList)
|
|
},
|
|
// 存储好友聊天列表
|
|
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 list = []
|
|
for(let item of res.data){
|
|
list.push({type: 0, ...item, userId: params.userId})
|
|
commit('SET_USER_OBJECT', item)
|
|
}
|
|
commit('SET_CHAT_FRIEND_LIST', list)
|
|
|
|
// 查询群组
|
|
getUserAllGroup().then(res1 => {
|
|
let list = []
|
|
for(let item1 of res1.data){
|
|
list.push({type: 1, ...item1, userId: params.userId})
|
|
// 查询群组人员
|
|
getGroupUser(item1.id).then(res2 => {
|
|
for(let user of res2.data){
|
|
commit('SET_USER_OBJECT', user)
|
|
}
|
|
resolve(res2)
|
|
});
|
|
commit('SET_GROUP_OBJECT', item1)
|
|
}
|
|
commit('SET_CHAT_GROUP_LIST', list)
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
}).catch(error => {
|
|
reject(error)
|
|
})
|
|
})
|
|
}
|
|
},
|
|
|
|
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
|