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.
shop-app/common/utils/webSkt.js

185 lines
4.6 KiB

3 years ago
/*
* @Author: ch
* @Date: 2022-05-18 15:27:02
* @LastEditors: ch
* @LastEditTime: 2022-05-19 15:50:12
* @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.data;
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.data = historyData;
}else{
// 会话列表不存在,则创建一个会话
MsbSkt.data = [...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.data);
});
/**
* 初始化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.data;
let hisIndex = historyData.findIndex(i => i.id === item.id);
if(hisIndex >= 0){
historyData[hisIndex].lastMessage = item.lastMessage;
historyData[hisIndex].unreadCount++;
MsbSkt.data = historyData
}else{
item.messageList = [];
MsbSkt.data = [...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.data;
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.data = 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.data;
let curSession = historyData.find(i => i.id === content.id);
if (!curSession) {
curSession = {
...content,
unreadCount: 0,
messageList : []
}
MsbSkt.data = [...historyData, curSession];
}
MsbSkt.curSession = curSession;
store.commit('SET_SESSION_DATA',MsbSkt.data);
store.commit('SET_SESSION_MSG_ID',curSession.id);
return Promise.resolve(content);
}