|
|
@ -2,16 +2,40 @@
|
|
|
|
* @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-02 18:10:40
|
|
|
|
* @Description: file content
|
|
|
|
* @Description: file content
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
|
|
|
|
import { CreateUUID, FormatDate, ToAsyncAwait } from "@/common/utils";
|
|
|
|
|
|
|
|
import './potoReq';
|
|
|
|
|
|
|
|
import './protoRsp'
|
|
|
|
const connect = Symbol('connect'),
|
|
|
|
const connect = Symbol('connect'),
|
|
|
|
send = Symbol('send'),
|
|
|
|
send = Symbol('send'),
|
|
|
|
onResponse = Symbol('onResponse'),
|
|
|
|
onResponse = Symbol('onResponse'),
|
|
|
|
onMessage = Symbol('onMessage'),
|
|
|
|
onMessage = Symbol('onMessage'),
|
|
|
|
updateData = Symbol('updateData')
|
|
|
|
updateData = Symbol('updateData')
|
|
|
|
;
|
|
|
|
;
|
|
|
|
|
|
|
|
const fromatPotoReq = (traceId, traceType, content) => {
|
|
|
|
|
|
|
|
let messageModel = new proto.ReqModel();
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
export default class MsbIm {
|
|
|
|
export default class MsbIm {
|
|
|
|
option = {
|
|
|
|
option = {
|
|
|
|
ioKey : 'traceId',
|
|
|
|
ioKey : 'traceId',
|
|
|
@ -19,6 +43,7 @@ export default class MsbIm {
|
|
|
|
logout : false
|
|
|
|
logout : false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
socket = null;
|
|
|
|
socket = null;
|
|
|
|
|
|
|
|
isOpen = false;
|
|
|
|
queue = {};
|
|
|
|
queue = {};
|
|
|
|
interceptors = {
|
|
|
|
interceptors = {
|
|
|
|
dataChangeBefore: null,
|
|
|
|
dataChangeBefore: null,
|
|
|
@ -51,10 +76,11 @@ export default class MsbIm {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
this.socket.onOpen(() => {
|
|
|
|
this.socket.onOpen(() => {
|
|
|
|
this.socket.onMessage(async (res) => {
|
|
|
|
this.socket.onMessage(async (res) => {
|
|
|
|
const result = JSON.parse(res.data);
|
|
|
|
const result = fromatPotoRsp(res.data);
|
|
|
|
if (result.content?.payload) {
|
|
|
|
|
|
|
|
result.content.payload = JSON.parse(result.content.payload);
|
|
|
|
// if (result.content?.payload) {
|
|
|
|
}
|
|
|
|
// result.content.payload = JSON.parse(result.content.payload);
|
|
|
|
|
|
|
|
// }
|
|
|
|
// 401主动退出
|
|
|
|
// 401主动退出
|
|
|
|
if (result.code === 401) {
|
|
|
|
if (result.code === 401) {
|
|
|
|
this.logout();
|
|
|
|
this.logout();
|
|
|
@ -94,8 +120,9 @@ export default class MsbIm {
|
|
|
|
reject(error);
|
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
this.socket.send({
|
|
|
|
this.socket.send({
|
|
|
|
data : JSON.stringify(data),
|
|
|
|
data : fromatPotoReq(data.traceId, data.traceType, data.content),
|
|
|
|
fail(e) {
|
|
|
|
fail(e) {
|
|
|
|
reject({error : e});
|
|
|
|
reject({error : e});
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -146,11 +173,11 @@ export default class MsbIm {
|
|
|
|
setTimeout(async () => {
|
|
|
|
setTimeout(async () => {
|
|
|
|
await this[send]({
|
|
|
|
await this[send]({
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: '0',
|
|
|
|
traceType: 0,
|
|
|
|
content: { text: "ping" }
|
|
|
|
content: { text: "ping" }
|
|
|
|
}).catch((e)=>{});
|
|
|
|
}).catch((e)=>{});
|
|
|
|
heart();
|
|
|
|
heart();
|
|
|
|
},5000)
|
|
|
|
},1000)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this[connect]({
|
|
|
|
this[connect]({
|
|
|
|
...config,
|
|
|
|
...config,
|
|
|
@ -187,16 +214,19 @@ 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
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
let {error, result} = await ToAsyncAwait(this[send](par));
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { content } = result;
|
|
|
|
const { content } = result;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('获取会话列表', par, result);
|
|
|
|
|
|
|
|
|
|
|
|
content.sessionVOS.forEach(item => {
|
|
|
|
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 || {});
|
|
|
@ -219,23 +249,30 @@ export default class MsbIm {
|
|
|
|
* 获取会话的历史消息记录
|
|
|
|
* 获取会话的历史消息记录
|
|
|
|
* @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];
|
|
|
|
|
|
|
|
const msgList = curSession.messageList || [];
|
|
|
|
|
|
|
|
const par = {
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceId: CreateUUID(),
|
|
|
|
traceType: 23,
|
|
|
|
traceType: 23,
|
|
|
|
...params
|
|
|
|
content: {
|
|
|
|
}));
|
|
|
|
sessionId: this.curSessionId,
|
|
|
|
|
|
|
|
topMessageId: msgList.length ? msgList[0].id : null
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
const {error, result} = await ToAsyncAwait(this[send](par));
|
|
|
|
if (error) {
|
|
|
|
if (error) {
|
|
|
|
return Promise.reject(error);
|
|
|
|
return Promise.reject(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const { content } = result;
|
|
|
|
const { content } = result;
|
|
|
|
|
|
|
|
console.log('获取会话历史消息',par, 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[hisIdx].messageList = newData[hisIdx].messageList.concat(content);
|
|
|
|
newData[curSessionIdx].messageList = content.concat(newData[curSessionIdx].messageList);
|
|
|
|
this.setSessionData(newData);
|
|
|
|
this.setSessionData(newData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Promise.resolve(result);
|
|
|
|
return Promise.resolve(result);
|
|
|
@ -246,11 +283,15 @@ export default class MsbIm {
|
|
|
|
* @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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const {error, result } = await this[send](par);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('会话已读', par, result);
|
|
|
|
|
|
|
|
|
|
|
|
let newData = this.sessionData.map(item => {
|
|
|
|
let newData = this.sessionData.map(item => {
|
|
|
|
if (item.id == params.content.sessionId) {
|
|
|
|
if (item.id == params.content.sessionId) {
|
|
|
|
item.unreadCount = 0;
|
|
|
|
item.unreadCount = 0;
|
|
|
@ -285,6 +326,8 @@ export default class MsbIm {
|
|
|
|
// 超过时间未返回视为发送失败
|
|
|
|
// 超过时间未返回视为发送失败
|
|
|
|
this.timerStatus(msgCtx);
|
|
|
|
this.timerStatus(msgCtx);
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
const { error, result } = await ToAsyncAwait(this[send](par));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log('发送消息', par, result);
|
|
|
|
// 接到通知,标记消息是否发送成功
|
|
|
|
// 接到通知,标记消息是否发送成功
|
|
|
|
for (let i = curSession.messageList.length; i--;) {
|
|
|
|
for (let i = curSession.messageList.length; i--;) {
|
|
|
|
const item = curSession.messageList[i];
|
|
|
|
const item = curSession.messageList[i];
|
|
|
|