msb_beta
ch 3 years ago
commit 83c831c377

File diff suppressed because one or more lines are too long

@ -2,7 +2,11 @@
* @Author: ch
* @Date: 2022-03-22 18:28:52
* @LastEditors: ch
* @LastEditTime: 2022-04-28 18:43:18
<<<<<<< HEAD
* @LastEditTime: 2022-04-29 14:03:32
=======
* @LastEditTime: 2022-04-28 23:03:06
>>>>>>> feature/1.0.0_im_ch
* @Description: file content
*/
import Vue from 'vue'
@ -26,6 +30,7 @@ export default new Vuex.Store({
address : JSON.parse(uni.getStorageSync(ADDRESS) || '[]'),
sessionData : [],
sessionMsgCount : 0,
sessionMsgId : '',
openId : uni.getStorageSync(OPPED_ID) || ''
},
mutations:{
@ -50,6 +55,9 @@ export default new Vuex.Store({
SET_OPEN_ID (state, data){
state.openId = data;
uni.setStorageSync(OPPED_ID, data);
},
SET_SESSION_MSG_ID (state, data){
state.sessionMsgId = data;
}
},
actions : {

@ -2,15 +2,15 @@
* @Author: ch
* @Date: 2022-03-22 16:52:28
* @LastEditors: ch
* @LastEditTime: 2022-04-26 17:52:05
* @LastEditTime: 2022-04-26 19:04:27
* @Description: 所有工具类统一在这输出
*/
import * as util from './utils';
import * as requset from './requset';
// import * as websocket from './requset';
import * as websocket from './requset';
export * from './utils';
export * from './requset';
// export * from './websocket';
export * from './websocket';
export default { ...util, ...requset}
export default { ...util, ...requset, ...websocket}

@ -2,7 +2,7 @@
* @Author: ch
* @Date: 2022-03-17 17:42:32
* @LastEditors: ch
* @LastEditTime: 2022-04-29 00:08:56
* @LastEditTime: 2022-04-29 14:03:56
* @Description: 项目接口请求统一处理器返回一个需要token和不需要token的请求封装方法
*/
@ -28,7 +28,7 @@ const successIntercept = (response, option) =>{
if(result.code === 'SUCCESS'){
return result.data;
}
if(result.code === 'TOKEN_EXPIRE'){
if(result.code === 'TOKEN_FAIL'){
uni.navigateTo({url : '/login'})
return result;
}

@ -2,105 +2,134 @@
* @Author: ch
* @Date: 2022-04-25 14:39:19
* @LastEditors: ch
* @LastEditTime: 2022-04-26 16:28:01
* @LastEditTime: 2022-04-28 23:33:33
* @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 :
sessionListFlag = true;
// 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框
ctx.sessionVOS.forEach(item => {
item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii')
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
let historyData = store.state.sessionData;
let hisIndex = historyData.findIndex(i => i.id === item.id);
if(hisIndex >= 0){
historyData[hisIndex].lastMessage = item.lastMessage;
historyData[hisIndex].unreadCount++;
store.commit('SET_SESSION_DATA', historyData);
}else{
item.messageList = [];
store.commit('SET_SESSION_DATA',[...historyData, item]);
}
});
// 消息总数
store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount || 10);
break;
// 历史消息
case 2 :
let newData = store.state.sessionData;
const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId);
ctx.forEach(item => {
item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii')
item.payload = JSON.parse(item.payload)
})
newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList);
store.commit('SET_SESSION_DATA', newData);
console.log(newData);
break;
// 会话消息
case 7 :
// 没接到会话列表时接到的消息不做处理
if(!sessionListFlag){
break;
}
// ctx.forEach(item => {
ctx.payload = JSON.parse(ctx.payload || {});
let historyData = store.state.sessionData;
const hisIndex = historyData.findIndex(i => i.id === ctx.sessionId);
store.commit('SET_SESSION_MSG_COUNT', store.state.sessionMsgCount + 1);
if(hisIndex >= 0){
// 存在会话往现有会话增加一条消息
const curHisData = historyData[hisIndex];
curHisData.messageList = [ ...(curHisData.messageList || []), ctx]
curHisData.unreadCount++;
store.commit('SET_SESSION_DATA', historyData);
}else{
// 会话列表不存在,则创建一个会话
store.commit('SET_SESSION_DATA',[...historyData, {
fromAvatar : ctx.fromAvatar,
fromId : ctx.fromId,
fromNickname : ctx.fromNickname,
id : ctx.id,
lastMessage : ctx,
messageList : [ctx],
unreadCount : 1
}]);
}
// });
break;
default :
break;
const sessionList = (ctx) => {
sessionListFlag = true;
// 来新消息先查会话列表是否一存在,存在则加消息数。不存在则向会话列表加一个会话框
ctx.sessionVOS.forEach(item => {
item.lastMessage.createTimeStamp = formatDate(item.lastMessage.createTimeStamp, 'mm-dd hh:ii')
item.lastMessage.payload = JSON.parse(item.lastMessage.payload || {});
let historyData = store.state.sessionData;
let hisIndex = historyData.findIndex(i => i.id === item.id);
if(hisIndex >= 0){
historyData[hisIndex].lastMessage = item.lastMessage;
historyData[hisIndex].unreadCount++;
store.commit('SET_SESSION_DATA', historyData);
}else{
item.messageList = [];
store.commit('SET_SESSION_DATA',[...historyData, item]);
}
});
// 消息总数
store.commit('SET_SESSION_MSG_COUNT', ctx.totalUnreadCount);
}
const historyMsg = (ctx) => {
if(!ctx.length){
return false;
}
})
ctx.reverse();
let newData = store.state.sessionData;
const hisIdx = newData.findIndex(i => i.id === ctx[0].sessionId);
ctx.forEach(item => {
item.createTimeStamp = formatDate(item.createTimeStamp, 'mm-dd hh:ii')
item.payload = JSON.parse(item.payload)
})
newData[hisIdx].messageList = ctx.concat(newData[hisIdx].messageList);
store.commit('SET_SESSION_DATA', newData);
}
/**
* 收到某个会话的信息
* @param {*} ctx
*/
const sessionMsg = (ctx)=>{
// 没接到会话列表时接到的消息不做处理
if(!sessionListFlag){
return
}
ctx.payload = JSON.parse(ctx.payload || {});
ctx.createTimeStamp = formatDate(ctx.createTimeStamp, 'mm-dd hh:ii')
let historyData = store.state.sessionData;
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 = [ ctx, ...(curHisData.messageList || [])]
// 不在当前会话框则会话框消息加1
if(ctx.sessionId !== store.state.sessionMsgId){
curHisData.unreadCount++;
}
store.commit('SET_SESSION_DATA', historyData);
}else{
// 会话列表不存在,则创建一个会话
store.commit('SET_SESSION_DATA',[...historyData, {
fromAvatar : ctx.fromAvatar,
fromId : ctx.fromId,
fromNickname : ctx.fromNickname,
id : ctx.id,
lastMessage : ctx,
messageList : [ctx],
unreadCount : 1
}]);
}
}
export let MsbWebSkt = null;
MsbWebSkt.onError(()=>{
console.log('WebSocket连接错误')
})
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;
}
}
})
setInterval(()=>{
MsbWebSkt.send({
data : JSON.stringify({
traceId : createUUID(),
traceType : "0",
content: { text : "ping"}
})
})
}, 5000);
resolve();
})
})
}

@ -2,7 +2,11 @@
* @Author: ch
* @Date: 2021-07-26 23:22:16
* @LastEditors: ch
* @LastEditTime: 2022-04-28 17:15:23
<<<<<<< HEAD
* @LastEditTime: 2022-04-29 14:02:40
=======
* @LastEditTime: 2022-04-27 10:12:54
>>>>>>> feature/1.0.0_im_ch
* @Description: file content
*/
import Vue from 'vue';
@ -15,28 +19,21 @@ import Confirm from '@/components/mount/index';
import route from 'uview-ui/libs/util/route';
import {toSearchJson} from '@/common/utils';
import {ApiGetOpenId, ApiGetAuthUrl} from '@/common/api/wx';
// import {MsbWebSkt, createUUID} from '@/common/utils';
import {MsbWebSkt, MsbWebSktInit, createUUID} from '@/common/utils';
if(store.state.token){
// 进入应用则向IM发起心跳包 以及获取IM会话数据
// MsbWebSkt.onOpen(()=>{
// MsbWebSkt.send({
// data : JSON.stringify({
// traceId : createUUID(),
// traceType : "1",
// content: { sysId : "1"}
// })
// });
// setInterval(()=>{
// MsbWebSkt.send({
// data : JSON.stringify({
// traceId : createUUID(),
// traceType : "0",
// content: { text : "ping"}
// })
// })
// }, 5000)
// });
MsbWebSktInit().then(()=>{
MsbWebSkt.send({
data : JSON.stringify({
traceId : createUUID(),
traceType : "1",
content: { sysId : "1"}
})
});
})
}
// 微信打开需要授权

@ -2,7 +2,7 @@
* @Author: ch
* @Date: 2022-03-22 16:13:00
* @LastEditors: ch
* @LastEditTime: 2022-04-25 19:34:58
* @LastEditTime: 2022-04-28 23:02:54
* @Description: file content
-->
<template>
@ -10,7 +10,7 @@
<BsEmpty tips="暂时还没有消息哦~" v-if="!$store.state.sessionData.length"
:icon="require('@/static/message/empty.png')" />
<view class="msgItem" v-for="item in $store.state.sessionData" :key="item.id"
@click="$Router.push(`/messageList?id=${item.id}`)">
@click="openMsg(item)">
<image class="msgItem--headImg" src="@/static/account/tx.png"></image>
<view class="msgItem--con">
<view>
@ -33,6 +33,12 @@ export default {
return {
sessionData : this.$store.state.sessionData
}
},
methods:{
openMsg(item){
this.$store.commit('SET_SESSION_MSG_ID',item.id);
this.$Router.push(`/messageList`);
}
}
}
</script>

@ -2,14 +2,14 @@
* @Author: ch
* @Date: 2022-03-26 14:32:03
* @LastEditors: ch
* @LastEditTime: 2022-04-26 17:52:30
* @LastEditTime: 2022-04-28 23:43:35
* @Description: file content
-->
<template>
<view>
<view class="msgItem" v-for="item in msgData" :key="item.id">
<view class="msgItem--title">
<text>{{item.payload.title}}</text>
<text>{{item.payload.title }} {{item.id}}</text>
<text class="msgItem--time">{{item.createTimeStamp}}</text>
</view>
<view class="msgItem--con">
@ -17,42 +17,73 @@
<view class="msgItem--desc">{{item.payload.content}}</view>
</view>
</view>
<u-loadmore status="loading" v-if="!msgData.length"/>
</view>
</template>
<script>
// import {MsbWebSkt, createUUID} from '@/common/utils';
import {MsbWebSkt, createUUID} from '@/common/utils';
export default {
data(){
return {
}
},
computed:{
curSessionData(){
return this.$store.state.sessionData.find(i =>i.id == this.$store.state.sessionMsgId)
},
msgData (){
// console.log(this.$store.state.sessionData,'---',this.$Route.query.id);
const sessionData = this.$store.state.sessionData.find(i =>i.id == this.$Route.query.id)
return sessionData ? sessionData.messageList : [];
return this.curSessionData ? this.curSessionData.messageList : [];
}
},
onReachBottom(){
this.getHistoryMsg();
},
onShow(){
if(!this.$store.state.sessionMsgId){
this.$Router.back();
return false
}
this.getHistoryMsg();
this.readMsg();
},
methods:{
/**
* 获取历史消息
*/
getHistoryMsg(){
// MsbWebSkt.onOpen(()=>{
// MsbWebSkt.send({
// data : JSON.stringify({
// traceId : createUUID(),
// traceType : "2",
// content: { sessionId : this.$Route.query.id}
// }),
// success(){
// console.log('suxx');
// },
// fail(e){
// console.log('fxx',e);
// }
// })
// })
this.loading = true;
const lastMsg = this.msgData[this.msgData.length - 1] || {};
MsbWebSkt.send({
data : JSON.stringify({
traceId : createUUID(),
traceType : "2",
content: {
sessionId : this.$store.state.sessionMsgId,
topMessageId : lastMsg.id || null
}
})
})
},
xx(){
console.log(this.$store.state.sessionData);
/**
* 已读消息
*/
readMsg(){
MsbWebSkt.send({
data : JSON.stringify({
traceId : createUUID(),
traceType : "6",
content: {
sessionId : this.$store.state.sessionMsgId
}
})
});
//
const count = this.$store.state.sessionMsgCount - this.curSessionData.unreadCount;
//
this.curSessionData.unreadCount = 0;
this.$store.commit('SET_SESSION_MSG_COUNT', count)
}
}
}

@ -2,7 +2,7 @@
* @Author: ch
* @Date: 2022-03-21 18:08:07
* @LastEditors: ch
* @LastEditTime: 2022-04-25 10:43:11
* @LastEditTime: 2022-04-27 10:47:46
* @Description: file content
-->
<template>
@ -42,7 +42,6 @@
</view>
</view>
<u-loadmore :status="loadingStatus" color="#FF8384" iconColor="#FF8384" nomoreText="我也是有底线的啦~"/>
</view>
</template>
<script>

Loading…
Cancel
Save