|
|
|
/*
|
|
|
|
* @Author: ch
|
|
|
|
* @Date: 2022-05-18 15:27:02
|
|
|
|
* @LastEditors: ch
|
|
|
|
* @LastEditTime: 2022-05-19 16:42:44
|
|
|
|
* @Description: file content
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { MsbWebSktNew } from "../plugins/webSkt";
|
|
|
|
import { CreateUUID, FormatDate } from '@/common/utils';
|
|
|
|
import store from "../store";
|
|
|
|
|
|
|
|
const MsbSkt = new MsbWebSktNew();
|
|
|
|
MsbSkt.use('onResponse', (data) => {
|
|
|
|
if (data.code !== 500) {
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
return Promise.reject(data);
|
|
|
|
|
|
|
|
})
|
|
|
|
MsbSkt.use('onMessage', (data) => {
|
|
|
|
// 判断非服务端主动推送消息不做处理
|
|
|
|
if (data[MsbSkt.option.ioKey]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ctx = data.content;
|
|
|
|
ctx.payload = JSON.parse(ctx.payload || {});
|
|
|
|
ctx.createTimeStamp = FormatDate(ctx.createTimeStamp || new Date(), 'mm-dd hh:ii')
|
|
|
|
let historyData = MsbSkt.sessionData;
|
|
|
|
const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId);
|
|
|
|
// 不在当前会话框则全局消息加1
|
|
|
|
// if(ctx.sessionId !== store.state.sessionMsgId){
|
|
|
|
// store.commit('SET_SESSION_MSG_COUNT', store.state.sessionMsgCount + 1);
|
|
|
|
// }
|
|
|
|
if(hisIndex >= 0){
|
|
|
|
// 存在会话往现有会话增加一条消息
|
|
|
|
const curHisData = historyData[hisIndex];
|
|
|
|
curHisData.messageList.push(ctx); //= [ ctx, ...(curHisData.messageList || [])]
|
|
|
|
// 不在当前会话框则会话框消息加1
|
|
|
|
// if(ctx.sessionId !== store.state.sessionMsgId){
|
|
|
|
// curHisData.unreadCount++;
|
|
|
|
// }
|
|
|
|
MsbSkt.sessionData = historyData;
|
|
|
|
}else{
|
|
|
|
// 会话列表不存在,则创建一个会话
|
|
|
|
MsbSkt.sessionData = [...historyData, {
|
|
|
|
fromAvatar : ctx.fromAvatar,
|
|
|
|
fromId : ctx.fromId,
|
|
|
|
fromNickname : ctx.fromNickname,
|
|
|
|
id : ctx.id,
|
|
|
|
lastMessage : ctx,
|
|
|
|
messageList : [ctx],
|
|
|
|
unreadCount : 1
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
store.commit('SET_SESSION_DATA',MsbSkt.sessionData);
|
|
|
|
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* 初始化IM
|
|
|
|
*/
|
|
|
|
export const MsbSktInit = () => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const heart = () => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
await MsbSkt.send({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: '0',
|
|
|
|
content: { text: "ping" }
|
|
|
|
});
|
|
|
|
heart();
|
|
|
|
},5000)
|
|
|
|
}
|
|
|
|
MsbSkt.connect({
|
|
|
|
// url : `wss://you-gateway.mashibing.com/ws?client=${store.state.token}&type=1`
|
|
|
|
url : `wss://k8s-horse-gateway.mashibing.cn/ws?client=${store.state.token}&type=1`, // url是websocket连接ip
|
|
|
|
|
|
|
|
}).then(() => {
|
|
|
|
heart();
|
|
|
|
resolve(MsbSkt);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 获取会话列表
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
export const MsbSktGetSessionList = async (params) => {
|
|
|
|
let {content} = await MsbSkt.send({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
...params
|
|
|
|
});
|
|
|
|
content.sessionVOS.forEach(item => {
|
|
|
|
if (item.lastMessage) {
|
|
|
|
item.lastMessage.createTimeStamp = FormatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii')
|
|
|
|
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
|
|
|
|
}
|
|
|
|
let historyData = MsbSkt.sessionData;
|
|
|
|
let hisIndex = historyData.findIndex(i => i.id === item.id);
|
|
|
|
if(hisIndex >= 0){
|
|
|
|
historyData[hisIndex].lastMessage = item.lastMessage;
|
|
|
|
historyData[hisIndex].unreadCount++;
|
|
|
|
MsbSkt.sessionData = historyData
|
|
|
|
}else{
|
|
|
|
item.messageList = [];
|
|
|
|
MsbSkt.sessionData = [...historyData, item];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 获取历史消息
|
|
|
|
*/
|
|
|
|
export const MsbSktGetHistoryMsg = async (params) => {
|
|
|
|
let { content } = await MsbSkt.send({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType : 23,
|
|
|
|
...params
|
|
|
|
})
|
|
|
|
if(!content.length){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// ctx.reverse();
|
|
|
|
let newData = MsbSkt.sessionData;
|
|
|
|
const hisIdx = newData.findIndex(i => i.id === content[0].sessionId);
|
|
|
|
content.forEach(item => {
|
|
|
|
item.createTimeStamp = FormatDate(item.createTimeStamp, 'mm-dd hh:ii');
|
|
|
|
item.payload = JSON.parse(item.payload)
|
|
|
|
})
|
|
|
|
newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content);
|
|
|
|
MsbSkt.sessionData = newData;
|
|
|
|
store.commit('SET_SESSION_DATA', newData);
|
|
|
|
return Promise.resolve(content);
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 设置为已读
|
|
|
|
*/
|
|
|
|
export const MsbSktSetRead = async (params) => {
|
|
|
|
await MsbSkt.send({
|
|
|
|
traceId : CreateUUID(),
|
|
|
|
traceType : "6",
|
|
|
|
...params
|
|
|
|
});
|
|
|
|
// 计算页头消息数
|
|
|
|
MsbSkt.curSession.unreadCount = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 发送消息
|
|
|
|
*/
|
|
|
|
export const MsbSktSendMsg = async (params) => {
|
|
|
|
const { content } = await MsbSkt.send({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: 20,
|
|
|
|
...params
|
|
|
|
});
|
|
|
|
return Promise.resolve(content)
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* 主动创建会话
|
|
|
|
*/
|
|
|
|
export const MsbSktCreateSession = async (params) => {
|
|
|
|
const { content } = await MsbSkt.send({
|
|
|
|
traceId : CreateUUID(),
|
|
|
|
traceType : 21,
|
|
|
|
...params
|
|
|
|
});
|
|
|
|
let historyData = MsbSkt.sessionData;
|
|
|
|
let curSession = historyData.find(i => i.id === content.id);
|
|
|
|
if (!curSession) {
|
|
|
|
curSession = {
|
|
|
|
...content,
|
|
|
|
unreadCount: 0,
|
|
|
|
messageList : []
|
|
|
|
}
|
|
|
|
MsbSkt.sessionData = [...historyData, curSession];
|
|
|
|
}
|
|
|
|
MsbSkt.curSession = curSession;
|
|
|
|
store.commit('SET_SESSION_DATA',MsbSkt.sessionData);
|
|
|
|
store.commit('SET_SESSION_MSG_ID',curSession.id);
|
|
|
|
return Promise.resolve(content);
|
|
|
|
}
|
|
|
|
|