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.7 KiB

3 years ago
/*
* @Author: ch
* @Date: 2022-05-18 15:27:02
* @LastEditors: ch
* @LastEditTime: 2022-05-19 16:42:44
3 years ago
* @Description: file content
*/
import { MsbWebSktNew } from "../plugins/webSkt";
import { CreateUUID, FormatDate } from '@/common/utils';
3 years ago
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;
3 years ago
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;
3 years ago
}else{
// 会话列表不存在,则创建一个会话
MsbSkt.sessionData = [...historyData, {
3 years ago
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);
3 years ago
});
/**
* 初始化IM
*/
export const MsbSktInit = () => {
return new Promise((resolve, reject) => {
const heart = () => {
setTimeout(async () => {
await MsbSkt.send({
traceId: CreateUUID(),
3 years ago
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(),
3 years ago
...params
});
content.sessionVOS.forEach(item => {
if (item.lastMessage) {
item.lastMessage.createTimeStamp = FormatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii')
3 years ago
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
}
let historyData = MsbSkt.sessionData;
3 years ago
let hisIndex = historyData.findIndex(i => i.id === item.id);
if(hisIndex >= 0){
historyData[hisIndex].lastMessage = item.lastMessage;
historyData[hisIndex].unreadCount++;
MsbSkt.sessionData = historyData
3 years ago
}else{
item.messageList = [];
MsbSkt.sessionData = [...historyData, item];
3 years ago
}
});
}
/**
* 获取历史消息
*/
export const MsbSktGetHistoryMsg = async (params) => {
let { content } = await MsbSkt.send({
traceId: CreateUUID(),
3 years ago
traceType : 23,
...params
})
if(!content.length){
return false;
}
// ctx.reverse();
let newData = MsbSkt.sessionData;
3 years ago
const hisIdx = newData.findIndex(i => i.id === content[0].sessionId);
content.forEach(item => {
item.createTimeStamp = FormatDate(item.createTimeStamp, 'mm-dd hh:ii');
3 years ago
item.payload = JSON.parse(item.payload)
})
newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content);
MsbSkt.sessionData = newData;
3 years ago
store.commit('SET_SESSION_DATA', newData);
return Promise.resolve(content);
}
/**
* 设置为已读
*/
export const MsbSktSetRead = async (params) => {
await MsbSkt.send({
traceId : CreateUUID(),
3 years ago
traceType : "6",
...params
});
// 计算页头消息数
MsbSkt.curSession.unreadCount = 0;
}
/**
* 发送消息
*/
export const MsbSktSendMsg = async (params) => {
const { content } = await MsbSkt.send({
traceId: CreateUUID(),
3 years ago
traceType: 20,
...params
});
return Promise.resolve(content)
}
/**
* 主动创建会话
*/
export const MsbSktCreateSession = async (params) => {
const { content } = await MsbSkt.send({
traceId : CreateUUID(),
3 years ago
traceType : 21,
...params
});
let historyData = MsbSkt.sessionData;
3 years ago
let curSession = historyData.find(i => i.id === content.id);
if (!curSession) {
curSession = {
...content,
unreadCount: 0,
messageList : []
}
MsbSkt.sessionData = [...historyData, curSession];
3 years ago
}
MsbSkt.curSession = curSession;
store.commit('SET_SESSION_DATA',MsbSkt.sessionData);
3 years ago
store.commit('SET_SESSION_MSG_ID',curSession.id);
return Promise.resolve(content);
}