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.
 
 
 
 
 
 

116 lines
3.8 KiB

import store from '@/uni_modules/vrapile-im/store'
import storage from '@/uni_modules/vrapile-im/utils/storage'
import constant from '@/uni_modules/vrapile-im/utils/constant'
import { formatDate } from '@/uni_modules/vrapile-im/utils/nineTool';
import { TioSocket, getChatKey } from '@/uni_modules/vrapile-im/utils/tiosocket';
const socket = {
state: {
websocket: storage.get(constant.websocket),
websocketData: storage.get(constant.websocketData)
},
mutations: {
// 存储登录链接
SET_SOCKET: (state, socket) => {
state.websocket = socket;
storage.set(constant.websocket, socket)
},
// 添加socket消息
ADD_MESSAGE: (state, message) => {
state.websocketData.push(message)
storage.set(constant.websocketData, state.websocketData)
// 添加消息
for(let i=0;i<store.state.chat.chatList.length;i++){
let item = store.state.chat.chatList[i];
let key = getChatKey(message["sendType"], message["toId"], message["fromId"])
if(store.state.chat.chatList[i]["key"] == key){
store.state.chat.chatList[i]["messageList"].push(message)
store.state.chat.chatList[i]["messageTime"] = formatDate(message["sendTime"]);
store.state.chat.chatList[i]["messageLast"] = message;
store.state.chat.chatList[i]["haveHistory"] = 1;
// 自己发送的消息,总消息数是否加1,如果不加,取消下方注释内容
// if(store.state.user.userInfo.userId && store.state.user.userInfo.userId != message["fromId"]){
store.state.chat.chatList[i]["messageNum"] += 1;
// }
if(store.state.chat.chatList[i]["type"] == 0){
store.state.chat.chatList[i]["messageShow"] = message["content"];
}else{
store.state.chat.chatList[i]["messageShow"] = store.getters.getUserName(message["fromId"], message["fromName"]) + ": " + message["content"]
}
storage.set(constant.chatList, store.state.chat.chatList);
break;
}
}
store.commit('COUNT_MESSAGE');
},
// 回执已读消息
READ_MESSAGE: (state, k) => {
// 删除未读消息
for(let i=0;i<state.websocketData.length;i++){
let key = getChatKey(state.websocketData[i]["sendType"], state.websocketData[i]["toId"], state.websocketData[i]["fromId"])
if(key == k){
state.websocketData.splice(i, 1);
i--;
}
}
storage.set(constant.websocketData, state.websocketData)
// 重置已读消息数量
for(let i=0;i<store.state.chat.chatList.length;i++){
if(store.state.chat.chatList[i].key == k){
store.state.chat.chatList[i].messageNum = 0;
storage.set(constant.chatList, store.state.chat.chatList)
break;
}
}
store.commit('COUNT_MESSAGE');
},
// 统计消息总数
COUNT_MESSAGE: (state) => {
let unReadNum = 0
for(let i=0;i<store.state.chat.chatList.length;i++){
unReadNum += store.state.chat.chatList[i].messageNum*1;
}
try{
if(unReadNum > 0){
uni.setTabBarBadge({
index: getApp().globalData.msgTabBarIndex,
text: String(unReadNum),
fail: (e) => {
// console.log(e)
}
})
}else{
uni.removeTabBarBadge({
index: getApp().globalData.msgTabBarIndex
})
}
}catch(e){
}
},
// 清空消息
CLEAN_MESSAGE: (state, message) => {
state.websocketData = new Array();
storage.set(constant.websocketData, new Array())
}
},
actions: {
// 连接websocket
ConnSocket({ commit, state }, params) {
return new Promise((resolve, reject) => {
let url = params.url;
let heartbeatTimeout = 50000; // 心跳超时时间,单位:毫秒
let reconnInterval = 5000; // 重连间隔时间,单位:毫秒
let paramStr = "app=unine&token=" + params.token
let socket = new TioSocket(url, paramStr, heartbeatTimeout, reconnInterval);
socket.connect(false);
})
}
}
}
export default socket