|
|
@ -2,83 +2,130 @@
|
|
|
|
* @Author: ch
|
|
|
|
* @Author: ch
|
|
|
|
* @Date: 2022-05-18 14:54:47
|
|
|
|
* @Date: 2022-05-18 14:54:47
|
|
|
|
* @LastEditors: ch
|
|
|
|
* @LastEditors: ch
|
|
|
|
* @LastEditTime: 2022-05-24 22:51:47
|
|
|
|
* @LastEditTime: 2022-06-14 17:24:14
|
|
|
|
* @Description: file content
|
|
|
|
* @Description: file content
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
|
|
|
|
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
|
|
|
|
const connect = Symbol('connect'),
|
|
|
|
import './potoReq';
|
|
|
|
send = Symbol('send'),
|
|
|
|
import './protoRsp'
|
|
|
|
onResponse = Symbol('onResponse'),
|
|
|
|
const connect = Symbol('connect');
|
|
|
|
onMessage = Symbol('onMessage'),
|
|
|
|
const send = Symbol('send');
|
|
|
|
updateData = Symbol('updateData')
|
|
|
|
const onMessage = Symbol('onMessage');
|
|
|
|
;
|
|
|
|
const fromatPotoReq = (traceId, traceType, content) => {
|
|
|
|
export default class MsbIm {
|
|
|
|
let messageModel = new proto.ReqModel();
|
|
|
|
option = {
|
|
|
|
messageModel.setTraceid(traceId);
|
|
|
|
|
|
|
|
messageModel.setTracetype(traceType);
|
|
|
|
|
|
|
|
content && messageModel.setContent(JSON.stringify(content));
|
|
|
|
|
|
|
|
return messageModel.serializeBinary();
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
fromatPotoRsp = (data) => {
|
|
|
|
|
|
|
|
const res = proto.RspModel.deserializeBinary(new Uint8Array(data));
|
|
|
|
|
|
|
|
let ctx = res.getContent();
|
|
|
|
|
|
|
|
ctx = ctx ? JSON.parse(ctx) : {};
|
|
|
|
|
|
|
|
if (ctx.payload) {
|
|
|
|
|
|
|
|
ctx.payload = JSON.parse(ctx.payload);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
|
|
content: ctx,
|
|
|
|
|
|
|
|
traceId: res.getTraceid(),
|
|
|
|
|
|
|
|
traceType: res.getTracetype(),
|
|
|
|
|
|
|
|
code: res.getCode(),
|
|
|
|
|
|
|
|
message: res.getMessage(),
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MsbIm {
|
|
|
|
|
|
|
|
defaultOption = {
|
|
|
|
ioKey: 'traceId',
|
|
|
|
ioKey: 'traceId',
|
|
|
|
reconnect: true,
|
|
|
|
reconnect: true,
|
|
|
|
logout : false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
socket = null;
|
|
|
|
socket = null;
|
|
|
|
|
|
|
|
isOpen = false;
|
|
|
|
queue = {};
|
|
|
|
queue = {};
|
|
|
|
interceptors = {
|
|
|
|
interceptors = {
|
|
|
|
dataChangeBefore: null,
|
|
|
|
dataChangeBefore: null,
|
|
|
|
dataChangeAfter: null,
|
|
|
|
dataChangeAfter: null,
|
|
|
|
onLogout : null,
|
|
|
|
onClose: null,
|
|
|
|
onMessage: null
|
|
|
|
onMessage: null,
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
};
|
|
|
|
sessionData = [];
|
|
|
|
sessionData = [];
|
|
|
|
curSessionId = null;
|
|
|
|
curSessionId = null;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(option) {
|
|
|
|
constructor(option) {
|
|
|
|
this.option = {
|
|
|
|
this.option = {
|
|
|
|
...this.option,
|
|
|
|
...this.defaultOption,
|
|
|
|
...option
|
|
|
|
...option,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 创建连接返回一个Promise 创建成功并成功打开连接算连接成功
|
|
|
|
* 创建连接返回一个Promise 创建成功并成功打开连接算连接成功
|
|
|
|
* @param {*} option
|
|
|
|
* @param {*} option
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
[connect](option) {
|
|
|
|
[connect](option) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.socket = uni.connectSocket({
|
|
|
|
const open = () => {
|
|
|
|
...option,
|
|
|
|
console.log('[im] open');
|
|
|
|
fail(e){
|
|
|
|
this.isOpen = true;
|
|
|
|
reject(e);
|
|
|
|
resolve(this.socket);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
});
|
|
|
|
const message = async (res) => {
|
|
|
|
this.socket.onOpen(() => {
|
|
|
|
const result = fromatPotoRsp(res.data);
|
|
|
|
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();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.interceptors.onMessage && this.interceptors.onMessage(result);
|
|
|
|
this.interceptors.onMessage && this.interceptors.onMessage(result);
|
|
|
|
// 处理服务端主动推送的消息
|
|
|
|
// 处理服务端主动推送的消息
|
|
|
|
this[onMessage](result);
|
|
|
|
this[onMessage](result);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果再消息堆里有此消息回调,则执行回调,并删除
|
|
|
|
// 如果再消息堆里有此消息回调,则执行回调,并删除
|
|
|
|
const cbk = this.queue[result[this.option.ioKey]];
|
|
|
|
const cbk = this.queue[result[this.option.ioKey]];
|
|
|
|
|
|
|
|
|
|
|
|
if (cbk) {
|
|
|
|
if (cbk) {
|
|
|
|
cbk(result.code !== 200 ? { error: result } : { result: result });
|
|
|
|
cbk(result.code !== 200 ? { error: result } : { result: result });
|
|
|
|
delete this.queue[result[this.option.ioKey]];
|
|
|
|
delete this.queue[result[this.option.ioKey]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
resolve(this.socket);
|
|
|
|
const close = () => {
|
|
|
|
|
|
|
|
console.log('[im] close');
|
|
|
|
|
|
|
|
this.interceptors.onClose && this.interceptors.onClose();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let isUni = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
isUni = uni;
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
isUni = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUni) {
|
|
|
|
|
|
|
|
this.socket = uni.connectSocket({
|
|
|
|
|
|
|
|
...this.option,
|
|
|
|
|
|
|
|
fail(e) {
|
|
|
|
|
|
|
|
reject(e);
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.onOpen(() => {
|
|
|
|
|
|
|
|
open();
|
|
|
|
|
|
|
|
this.socket.onMessage((res) => {
|
|
|
|
|
|
|
|
message(res);
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.socket.onClose(() => {
|
|
|
|
this.socket.onClose(() => {
|
|
|
|
if (this.option.reconnect && !this.option.logout) {
|
|
|
|
close();
|
|
|
|
this[connect]();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.option.logout = false;
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (WebSocket) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
this.socket = new WebSocket(this.option.url);
|
|
|
|
|
|
|
|
this.socket.binaryType = 'arraybuffer';
|
|
|
|
|
|
|
|
this.socket.onopen = () => {
|
|
|
|
|
|
|
|
open();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.onmessage = (res) => {
|
|
|
|
|
|
|
|
message(res);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.onclose = () => {
|
|
|
|
|
|
|
|
close();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
reject(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -87,6 +134,9 @@ export default class MsbIm {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
[send](data) {
|
|
|
|
[send](data) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
if (!this.isOpen) {
|
|
|
|
|
|
|
|
return reject('连接未打开');
|
|
|
|
|
|
|
|
}
|
|
|
|
this.queue[data[this.option.ioKey]] = ({ result, error }) => {
|
|
|
|
this.queue[data[this.option.ioKey]] = ({ result, error }) => {
|
|
|
|
if (result) {
|
|
|
|
if (result) {
|
|
|
|
resolve(result);
|
|
|
|
resolve(result);
|
|
|
@ -94,13 +144,26 @@ export default class MsbIm {
|
|
|
|
reject(error);
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const par = fromatPotoReq(data.traceId, data.traceType, data.content);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let isUni = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
isUni = uni;
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
isUni = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isUni) {
|
|
|
|
this.socket.send({
|
|
|
|
this.socket.send({
|
|
|
|
data : JSON.stringify(data),
|
|
|
|
data: par,
|
|
|
|
fail(e) {
|
|
|
|
fail(e) {
|
|
|
|
reject({ error: e });
|
|
|
|
reject({ error: e });
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
} else if (WebSocket) {
|
|
|
|
|
|
|
|
this.socket.send(par);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 服务端推送消息,只处理服务端主动推送的消息
|
|
|
|
* 服务端推送消息,只处理服务端主动推送的消息
|
|
|
@ -111,10 +174,11 @@ export default class MsbIm {
|
|
|
|
if (data[this.option.ioKey] || data.code !== 200) {
|
|
|
|
if (data[this.option.ioKey] || data.code !== 200) {
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log('[im] 主动接收的消息', data);
|
|
|
|
let ctx = data.content;
|
|
|
|
let ctx = data.content;
|
|
|
|
let historyData = [...this.sessionData],
|
|
|
|
let historyData = [...this.sessionData],
|
|
|
|
newData = [];
|
|
|
|
newData = [];
|
|
|
|
const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId);
|
|
|
|
const hisIndex = historyData.findIndex((i) => i.id === ctx.sessionId);
|
|
|
|
if (hisIndex >= 0) {
|
|
|
|
if (hisIndex >= 0) {
|
|
|
|
// 存在会话往现有会话增加一条消息,并修改最后一条消息为当前消息
|
|
|
|
// 存在会话往现有会话增加一条消息,并修改最后一条消息为当前消息
|
|
|
|
const curHisData = historyData[hisIndex];
|
|
|
|
const curHisData = historyData[hisIndex];
|
|
|
@ -123,55 +187,68 @@ export default class MsbIm {
|
|
|
|
// 不在当前会话窗口则向会话消息加1条未读
|
|
|
|
// 不在当前会话窗口则向会话消息加1条未读
|
|
|
|
if (ctx.sessionId !== this.curSessionId) {
|
|
|
|
if (ctx.sessionId !== this.curSessionId) {
|
|
|
|
curHisData.unreadCount++;
|
|
|
|
curHisData.unreadCount++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.setRead({
|
|
|
|
|
|
|
|
content: {
|
|
|
|
|
|
|
|
sessionId: this.curSessionId,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
newData = historyData;
|
|
|
|
newData = historyData;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 会话列表不存在,则创建一个会话
|
|
|
|
// 会话列表不存在,则创建一个会话
|
|
|
|
newData = [...historyData, {
|
|
|
|
newData = [
|
|
|
|
fromAvatar : ctx.fromAvatar,
|
|
|
|
...historyData,
|
|
|
|
fromId : ctx.fromId,
|
|
|
|
{
|
|
|
|
fromNickname : ctx.fromNickname,
|
|
|
|
fromAvatar: ctx.session.fromAvatar,
|
|
|
|
id : ctx.id,
|
|
|
|
fromId: ctx.session.fromId,
|
|
|
|
|
|
|
|
fromNickname: ctx.session.fromNickname,
|
|
|
|
|
|
|
|
id: ctx.sessionId,
|
|
|
|
lastMessage: ctx,
|
|
|
|
lastMessage: ctx,
|
|
|
|
messageList: [ctx],
|
|
|
|
messageList: [ctx],
|
|
|
|
unreadCount : 1
|
|
|
|
updateTimeStamp: ctx.createTimeStamp,
|
|
|
|
}]
|
|
|
|
unreadCount: 1,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.setSessionData(newData)
|
|
|
|
this.setSessionData(newData);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
init(config) {
|
|
|
|
init(config) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const heart = () => {
|
|
|
|
const heart = () => {
|
|
|
|
|
|
|
|
// 要优化 心跳没回复需要重连
|
|
|
|
setTimeout(async () => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
await this[send]({
|
|
|
|
await this[send]({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: '0',
|
|
|
|
traceType: 0,
|
|
|
|
content: { text: "ping" }
|
|
|
|
content: { text: 'ping' },
|
|
|
|
}).catch((e)=>{});
|
|
|
|
});
|
|
|
|
heart();
|
|
|
|
|
|
|
|
},5000)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this[connect]({
|
|
|
|
heart();
|
|
|
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
this.option = {
|
|
|
|
|
|
|
|
...this.option,
|
|
|
|
...config,
|
|
|
|
...config,
|
|
|
|
}).then(() => {
|
|
|
|
};
|
|
|
|
|
|
|
|
this[connect]()
|
|
|
|
|
|
|
|
.then((res) => {
|
|
|
|
|
|
|
|
resolve(res);
|
|
|
|
heart();
|
|
|
|
heart();
|
|
|
|
resolve(this);
|
|
|
|
|
|
|
|
}).catch((e)=>{});
|
|
|
|
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
.catch((e) => {
|
|
|
|
}
|
|
|
|
console.log('eeeee', e);
|
|
|
|
logout() {
|
|
|
|
});
|
|
|
|
this.option.logout = true;
|
|
|
|
});
|
|
|
|
this.socket.close();
|
|
|
|
|
|
|
|
this.interceptors.onLogout && this.interceptors.onLogout();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 设置数据
|
|
|
|
* 设置数据
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
setSessionData(data) {
|
|
|
|
setSessionData(data) {
|
|
|
|
this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(data, this.sessionData);
|
|
|
|
let newData = JSON.parse(JSON.stringify(data));
|
|
|
|
this.sessionData = data;
|
|
|
|
this.interceptors.dataChangeBefore && this.interceptors.dataChangeBefore(newData, this.sessionData);
|
|
|
|
|
|
|
|
this.sessionData = newData;
|
|
|
|
this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData);
|
|
|
|
this.interceptors.dataChangeAfter && this.interceptors.dataChangeAfter(this.sessionData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -187,71 +264,93 @@ export default class MsbIm {
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async getSessionList(params) {
|
|
|
|
async getSessionList(params) {
|
|
|
|
|
|
|
|
const par = {
|
|
|
|
let {error, result} = await ToAsyncAwait(this[send]({
|
|
|
|
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: 1,
|
|
|
|
traceType: 1,
|
|
|
|
...params
|
|
|
|
...params,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[im] 获取会话列表--start', par);
|
|
|
|
|
|
|
|
let { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
|
|
|
console.log('[im] 获取会话列表--end', result, error);
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { content } = result;
|
|
|
|
const { content } = result;
|
|
|
|
content.sessionVOS.forEach(item => {
|
|
|
|
// let newData = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
content.sessionVOS.forEach((item) => {
|
|
|
|
if (item.lastMessage) {
|
|
|
|
if (item.lastMessage) {
|
|
|
|
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
|
|
|
|
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let historyData = this.sessionData;
|
|
|
|
let historyData = this.sessionData;
|
|
|
|
let hisIndex = historyData.findIndex(i => i.id === item.id);
|
|
|
|
let hisIndex = historyData.findIndex((i) => i.id === item.id);
|
|
|
|
if(hisIndex >= 0){
|
|
|
|
if (hisIndex < 0) {
|
|
|
|
historyData[hisIndex].lastMessage = item.lastMessage;
|
|
|
|
|
|
|
|
historyData[hisIndex].unreadCount++;
|
|
|
|
|
|
|
|
this.setSessionData(historyData)
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
item.messageList = [];
|
|
|
|
item.messageList = [];
|
|
|
|
const newData = [...historyData, item]
|
|
|
|
|
|
|
|
this.setSessionData(newData);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// let historyData = this.sessionData;
|
|
|
|
|
|
|
|
// let hisIndex = historyData.findIndex((i) => i.id === item.id);
|
|
|
|
|
|
|
|
// if (hisIndex >= 0) {
|
|
|
|
|
|
|
|
// historyData[hisIndex].lastMessage = item.lastMessage;
|
|
|
|
|
|
|
|
// historyData[hisIndex].unreadCount++;
|
|
|
|
|
|
|
|
// newData.push(historyData[hisIndex]);
|
|
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
|
|
// item.messageList = [];
|
|
|
|
|
|
|
|
// newData = [...newData, item];
|
|
|
|
|
|
|
|
// }
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.setSessionData(content.sessionVOS);
|
|
|
|
return Promise.resolve(result);
|
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 获取会话的历史消息记录
|
|
|
|
* 获取会话的历史消息记录
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async getHistoryMsg(params) {
|
|
|
|
async getHistoryMsg() {
|
|
|
|
const {error, result} = await ToAsyncAwait(this[send]({
|
|
|
|
const curSessionIdx = this.sessionData.findIndex((i) => i.id === this.curSessionId);
|
|
|
|
|
|
|
|
const curSession = this.sessionData[curSessionIdx];
|
|
|
|
|
|
|
|
console.log(curSession, this.curSessionId, 'this.curSessionId');
|
|
|
|
|
|
|
|
const msgList = curSession.messageList || [];
|
|
|
|
|
|
|
|
const par = {
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: 23,
|
|
|
|
traceType: 2,
|
|
|
|
...params
|
|
|
|
content: {
|
|
|
|
}));
|
|
|
|
sessionId: this.curSessionId,
|
|
|
|
|
|
|
|
topMessageId: msgList.length ? msgList[0].id : null,
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log('[im] 获取会话历史消息--start', par);
|
|
|
|
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
|
|
|
console.log('[im] 获取会话历史消息--end', result, error);
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { content } = result;
|
|
|
|
const { content } = result;
|
|
|
|
if (content.length) {
|
|
|
|
if (content.length) {
|
|
|
|
let newData = this.sessionData;
|
|
|
|
let newData = this.sessionData;
|
|
|
|
const hisIdx = newData.findIndex(i => i.id === content[0].sessionId);
|
|
|
|
content.forEach((item) => {
|
|
|
|
content.forEach(item => {
|
|
|
|
item.payload = JSON.parse(item.payload);
|
|
|
|
item.payload = JSON.parse(item.payload)
|
|
|
|
});
|
|
|
|
})
|
|
|
|
newData[curSessionIdx].messageList = content.concat(newData[curSessionIdx].messageList);
|
|
|
|
newData[hisIdx].messageList = newData[hisIdx].messageList.concat(content);
|
|
|
|
|
|
|
|
this.setSessionData(newData);
|
|
|
|
this.setSessionData(newData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
return Promise.resolve(result);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 会话已读
|
|
|
|
* 会话已读
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async setRead(params) {
|
|
|
|
async setRead(params) {
|
|
|
|
await this[send]({
|
|
|
|
const par = {
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType : "6",
|
|
|
|
traceType: '6',
|
|
|
|
...params
|
|
|
|
...params,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
let newData = this.sessionData.map(item => {
|
|
|
|
console.log('[im] 会话已读--start', par);
|
|
|
|
|
|
|
|
const { error, result } = await this[send](par);
|
|
|
|
|
|
|
|
console.log('[im] 会话已读--end', result, error);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let newData = this.sessionData.map((item) => {
|
|
|
|
if (item.id == params.content.sessionId) {
|
|
|
|
if (item.id == params.content.sessionId) {
|
|
|
|
item.unreadCount = 0;
|
|
|
|
item.unreadCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -265,26 +364,34 @@ export default class MsbIm {
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async sendMsg(params) {
|
|
|
|
async sendMsg(params) {
|
|
|
|
const index = this.sessionData.findIndex(i => i.id === this.curSessionId)
|
|
|
|
const index = this.sessionData.findIndex((i) => i.id === this.curSessionId);
|
|
|
|
let curSession = this.sessionData[index];
|
|
|
|
let curSession = this.sessionData[index];
|
|
|
|
// 临时消息体
|
|
|
|
// 临时消息体
|
|
|
|
let par = {
|
|
|
|
let par = {
|
|
|
|
...params,
|
|
|
|
...params,
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: 20,
|
|
|
|
traceType: 3,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let msgCtx = {
|
|
|
|
let msgCtx = {
|
|
|
|
...params.content,
|
|
|
|
...params.content,
|
|
|
|
...par,
|
|
|
|
...par,
|
|
|
|
fromId: params.fromId,
|
|
|
|
createTimeStamp: new Date().getTime(),
|
|
|
|
createTimeStamp : (new Date()).getTime(),
|
|
|
|
sendStatus: 'loading',
|
|
|
|
sendStatus : 'loading'
|
|
|
|
};
|
|
|
|
|
|
|
|
if (typeof msgCtx.payload === 'string') {
|
|
|
|
|
|
|
|
msgCtx.payload = JSON.parse(msgCtx.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 点发送,立即把消息加入消息列表,标记为发送中状态
|
|
|
|
// 点发送,立即把消息加入消息列表,标记为发送中状态
|
|
|
|
|
|
|
|
curSession.lastMessage = msgCtx;
|
|
|
|
curSession.messageList.push(msgCtx);
|
|
|
|
curSession.messageList.push(msgCtx);
|
|
|
|
|
|
|
|
this.setSessionData(this.sessionData);
|
|
|
|
|
|
|
|
|
|
|
|
// 超过时间未返回视为发送失败
|
|
|
|
// 超过时间未返回视为发送失败
|
|
|
|
this.timerStatus(msgCtx);
|
|
|
|
this.timerStatus(msgCtx);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[im] 发送消息--start', par);
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
|
|
|
console.log('[im] 发送消息--end', result, error);
|
|
|
|
// 接到通知,标记消息是否发送成功
|
|
|
|
// 接到通知,标记消息是否发送成功
|
|
|
|
for (let i = curSession.messageList.length; i--; ) {
|
|
|
|
for (let i = curSession.messageList.length; i--; ) {
|
|
|
|
const item = curSession.messageList[i];
|
|
|
|
const item = curSession.messageList[i];
|
|
|
@ -294,6 +401,7 @@ export default class MsbIm {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let newData = [...this.sessionData];
|
|
|
|
let newData = [...this.sessionData];
|
|
|
|
|
|
|
|
curSession.lastMessage = msgCtx;
|
|
|
|
newData[index] = curSession;
|
|
|
|
newData[index] = curSession;
|
|
|
|
this.setSessionData(newData);
|
|
|
|
this.setSessionData(newData);
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
@ -308,12 +416,17 @@ export default class MsbIm {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async resend(params) {
|
|
|
|
async resend(params) {
|
|
|
|
params.sendStatus = 'loading';
|
|
|
|
params.sendStatus = 'loading';
|
|
|
|
this.timerStatus(params)
|
|
|
|
this.timerStatus(params);
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send]({
|
|
|
|
|
|
|
|
|
|
|
|
console.log('[im] 重新发送消息--start', params);
|
|
|
|
|
|
|
|
const { error, result } = await ToAsyncAwait(
|
|
|
|
|
|
|
|
this[send]({
|
|
|
|
traceId: params.traceId,
|
|
|
|
traceId: params.traceId,
|
|
|
|
traceType: params.traceType,
|
|
|
|
traceType: params.traceType,
|
|
|
|
content : params.content
|
|
|
|
content: params.content,
|
|
|
|
}));
|
|
|
|
})
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log('[im] 重新发送消息--end', result, error);
|
|
|
|
params.createTimeStamp = result.createTimeStamp;
|
|
|
|
params.createTimeStamp = result.createTimeStamp;
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
params.sendStatus = 'fail';
|
|
|
|
params.sendStatus = 'fail';
|
|
|
@ -323,7 +436,6 @@ export default class MsbIm {
|
|
|
|
return Promise.resolve(result);
|
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
timerStatus(msg) {
|
|
|
|
timerStatus(msg) {
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (msg.sendStatus === 'loading') {
|
|
|
|
if (msg.sendStatus === 'loading') {
|
|
|
|
msg.sendStatus = 'fail';
|
|
|
|
msg.sendStatus = 'fail';
|
|
|
@ -336,28 +448,36 @@ export default class MsbIm {
|
|
|
|
* @param {*} params
|
|
|
|
* @param {*} params
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async createSession(params) {
|
|
|
|
async createSession(params) {
|
|
|
|
const { result, error } = await ToAsyncAwait(this[send]({
|
|
|
|
const par = {
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType : 21,
|
|
|
|
traceType: 9,
|
|
|
|
...params
|
|
|
|
...params,
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
console.log('[im] 主动创建会话--start', par);
|
|
|
|
|
|
|
|
const { result, error } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
|
|
|
console.log('[im] 主动创建会话--end', result, error);
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { content } = result;
|
|
|
|
const { content } = result;
|
|
|
|
let historyData = this.sessionData;
|
|
|
|
let historyData = this.sessionData;
|
|
|
|
let curSession = historyData.find(i => i.id === content.id);
|
|
|
|
let curSession = historyData.find((i) => i.id === content.id);
|
|
|
|
if (!curSession) {
|
|
|
|
if (!curSession) {
|
|
|
|
curSession = {
|
|
|
|
curSession = {
|
|
|
|
...content,
|
|
|
|
...content,
|
|
|
|
unreadCount: 0,
|
|
|
|
unreadCount: 0,
|
|
|
|
messageList : []
|
|
|
|
messageList: [],
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const newData = [...historyData, curSession];
|
|
|
|
const newData = [...historyData, curSession];
|
|
|
|
this.setSessionData(newData);
|
|
|
|
this.setSessionData(newData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
return Promise.resolve(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close() {
|
|
|
|
|
|
|
|
this.socket.close();
|
|
|
|
|
|
|
|
this.socket = null;
|
|
|
|
|
|
|
|
this.isOpen = false;
|
|
|
|
|
|
|
|
this.setSessionData([]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MsbIm;
|