|
|
|
@ -2,7 +2,7 @@
|
|
|
|
|
* @Author: ch
|
|
|
|
|
* @Date: 2022-05-18 14:54:47
|
|
|
|
|
* @LastEditors: ch
|
|
|
|
|
* @LastEditTime: 2022-05-21 19:32:49
|
|
|
|
|
* @LastEditTime: 2022-05-24 22:51:47
|
|
|
|
|
* @Description: file content
|
|
|
|
|
*/
|
|
|
|
|
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
|
|
|
|
@ -52,6 +52,9 @@ export default class MsbIm {
|
|
|
|
|
this.socket.onOpen(() => {
|
|
|
|
|
this.socket.onMessage(async (res) => {
|
|
|
|
|
const result = JSON.parse(res.data);
|
|
|
|
|
if (result.content?.payload) {
|
|
|
|
|
result.content.payload = JSON.parse(result.content.payload);
|
|
|
|
|
}
|
|
|
|
|
// 401主动退出
|
|
|
|
|
if (result.code === 401) {
|
|
|
|
|
this.logout();
|
|
|
|
@ -109,7 +112,6 @@ export default class MsbIm {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
let ctx = data.content;
|
|
|
|
|
ctx.payload = JSON.parse(ctx.payload || {});
|
|
|
|
|
let historyData = [...this.sessionData],
|
|
|
|
|
newData = [];
|
|
|
|
|
const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId);
|
|
|
|
@ -263,30 +265,72 @@ export default class MsbIm {
|
|
|
|
|
* @param {*} params
|
|
|
|
|
*/
|
|
|
|
|
async sendMsg(params) {
|
|
|
|
|
let curSession = this.sessionData.find(i => i.id === this.curSessionId);
|
|
|
|
|
|
|
|
|
|
curSession.messageList.push({
|
|
|
|
|
const index = this.sessionData.findIndex(i => i.id === this.curSessionId)
|
|
|
|
|
let curSession = this.sessionData[index];
|
|
|
|
|
// 临时消息体
|
|
|
|
|
let par = {
|
|
|
|
|
...params,
|
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
|
traceType: 20,
|
|
|
|
|
}
|
|
|
|
|
let msgCtx = {
|
|
|
|
|
...params.content,
|
|
|
|
|
...par,
|
|
|
|
|
fromId: params.fromId,
|
|
|
|
|
createTimeStamp : (new Date()).getTime(),
|
|
|
|
|
sendStatus : 'loading'
|
|
|
|
|
});
|
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send]({
|
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
|
traceType: 20,
|
|
|
|
|
...params
|
|
|
|
|
}));
|
|
|
|
|
for (let item of curSession.messageList) {
|
|
|
|
|
if (item[this.option.ioKey] === params[this.option.ioKey]) {
|
|
|
|
|
item.sendStatus = error ? 'fail' : 'success';
|
|
|
|
|
}
|
|
|
|
|
// 点发送,立即把消息加入消息列表,标记为发送中状态
|
|
|
|
|
curSession.messageList.push(msgCtx);
|
|
|
|
|
// 超过时间未返回视为发送失败
|
|
|
|
|
this.timerStatus(msgCtx);
|
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
// 接到通知,标记消息是否发送成功
|
|
|
|
|
for (let i = curSession.messageList.length; i--;) {
|
|
|
|
|
const item = curSession.messageList[i];
|
|
|
|
|
if (item[this.option.ioKey] === par[this.option.ioKey]) {
|
|
|
|
|
curSession.messageList[i].sendStatus = msgCtx.sendStatus = error ? 'fail' : 'success';
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let newData = [...this.sessionData];
|
|
|
|
|
newData[index] = curSession;
|
|
|
|
|
this.setSessionData(newData);
|
|
|
|
|
if (error) {
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Promise.resolve(result);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 发送失败时,重新发送
|
|
|
|
|
* @param {*} params
|
|
|
|
|
*/
|
|
|
|
|
async resend(params) {
|
|
|
|
|
params.sendStatus = 'loading';
|
|
|
|
|
this.timerStatus(params)
|
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send]({
|
|
|
|
|
traceId: params.traceId,
|
|
|
|
|
traceType: params.traceType,
|
|
|
|
|
content : params.content
|
|
|
|
|
}));
|
|
|
|
|
params.createTimeStamp = result.createTimeStamp;
|
|
|
|
|
if (error) {
|
|
|
|
|
params.sendStatus = 'fail';
|
|
|
|
|
return Promise.reject(error);
|
|
|
|
|
}
|
|
|
|
|
params.sendStatus = 'success';
|
|
|
|
|
return Promise.resolve(result);
|
|
|
|
|
}
|
|
|
|
|
timerStatus(msg) {
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
if (msg.sendStatus === 'loading') {
|
|
|
|
|
msg.sendStatus = 'fail';
|
|
|
|
|
delete this.queue[msg.traceId];
|
|
|
|
|
}
|
|
|
|
|
}, 3000);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 主动创建会话
|
|
|
|
|
* @param {*} params
|
|
|
|
|