|
|
|
@ -2,31 +2,13 @@
|
|
|
|
|
* @Author: ch
|
|
|
|
|
* @Date: 2022-04-25 14:39:19
|
|
|
|
|
* @LastEditors: ch
|
|
|
|
|
* @LastEditTime: 2022-04-26 16:28:01
|
|
|
|
|
* @LastEditTime: 2022-04-27 10:12:40
|
|
|
|
|
* @Description: file content
|
|
|
|
|
*/
|
|
|
|
|
import store from "../store";
|
|
|
|
|
import {createUUID, formatDate} from '@/common/utils';
|
|
|
|
|
export const MsbWebSkt = uni.connectSocket({
|
|
|
|
|
url : `ws://192.168.10.92:8090/ws?client=${store.state.token}`, // url是websocket连接ip
|
|
|
|
|
success: () => {
|
|
|
|
|
console.log('websocket连接成功!')
|
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
console.log('连接失败' + e)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
MsbWebSkt.onClose(()=>{
|
|
|
|
|
console.log('WebSocket关闭...!')
|
|
|
|
|
})
|
|
|
|
|
let sessionListFlag = false;
|
|
|
|
|
MsbWebSkt.onMessage((res)=>{
|
|
|
|
|
const data = JSON.parse(res.data || {});
|
|
|
|
|
if(data.code === 200){
|
|
|
|
|
const ctx = data.content;
|
|
|
|
|
switch(data.traceType){
|
|
|
|
|
// 会话列表
|
|
|
|
|
case 1 :
|
|
|
|
|
const sessionList = (ctx) => {
|
|
|
|
|
sessionListFlag = true;
|
|
|
|
|
// 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框
|
|
|
|
|
ctx.sessionVOS.forEach(item => {
|
|
|
|
@ -45,9 +27,8 @@ MsbWebSkt.onMessage((res)=>{
|
|
|
|
|
});
|
|
|
|
|
// 消息总数
|
|
|
|
|
store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount || 10);
|
|
|
|
|
break;
|
|
|
|
|
// 历史消息
|
|
|
|
|
case 2 :
|
|
|
|
|
}
|
|
|
|
|
const historyMsg = (ctx) => {
|
|
|
|
|
let newData = store.state.sessionData;
|
|
|
|
|
const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId);
|
|
|
|
|
ctx.forEach(item => {
|
|
|
|
@ -56,14 +37,12 @@ MsbWebSkt.onMessage((res)=>{
|
|
|
|
|
})
|
|
|
|
|
newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList);
|
|
|
|
|
store.commit('SET_SESSION_DATA', newData);
|
|
|
|
|
console.log(newData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
// 会话消息
|
|
|
|
|
case 7 :
|
|
|
|
|
const sessionMsg = (ctx)=>{
|
|
|
|
|
// 没接到会话列表时接到的消息不做处理
|
|
|
|
|
if(!sessionListFlag){
|
|
|
|
|
break;
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// ctx.forEach(item => {
|
|
|
|
|
ctx.payload = JSON.parse(ctx.payload || {});
|
|
|
|
@ -91,16 +70,45 @@ MsbWebSkt.onMessage((res)=>{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// });
|
|
|
|
|
}
|
|
|
|
|
export let MsbWebSkt = null;
|
|
|
|
|
|
|
|
|
|
export const MsbWebSktInit = () => {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
MsbWebSkt = uni.connectSocket({
|
|
|
|
|
url : `ws://192.168.10.92:8090/ws?client=${store.state.token}`, // url是websocket连接ip
|
|
|
|
|
fail: e => {
|
|
|
|
|
reject(e)
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
MsbWebSkt.onOpen(()=>{
|
|
|
|
|
MsbWebSkt.onMessage((res)=>{
|
|
|
|
|
const data = JSON.parse(res.data || {});
|
|
|
|
|
if(data.code === 200){
|
|
|
|
|
const ctx = data.content;
|
|
|
|
|
switch(data.traceType){
|
|
|
|
|
// 会话列表
|
|
|
|
|
case 1 :
|
|
|
|
|
sessionList(ctx);
|
|
|
|
|
break;
|
|
|
|
|
// 历史消息
|
|
|
|
|
case 2 :
|
|
|
|
|
historyMsg(ctx)
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
// 会话消息
|
|
|
|
|
case 7 :
|
|
|
|
|
sessionMsg(ctx)
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
MsbWebSkt.onError(()=>{
|
|
|
|
|
console.log('WebSocket连接错误')
|
|
|
|
|
resolve();
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|