From 1d807ab18f39493871704b8784837b0f62e60312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=91=E6=96=87=E5=8F=AF?= <1041367524@qq.com> Date: Thu, 12 May 2022 15:55:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B6=88=E6=81=AF=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E5=AF=B9=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/modules/chat/chat.js | 99 +++++++++++++++++++------ src/views/chat/index.vue | 132 ++++++++------------------------- src/views/chat/message.vue | 47 ++++++++++-- 3 files changed, 143 insertions(+), 135 deletions(-) diff --git a/src/store/modules/chat/chat.js b/src/store/modules/chat/chat.js index 0b409e2..6fc84d7 100644 --- a/src/store/modules/chat/chat.js +++ b/src/store/modules/chat/chat.js @@ -7,25 +7,37 @@ const state = () => ({ queue: [], task: [], sessionData: {}, + currentSession: null, + messageList: [], + messageType: ['', 'text'], }); -const getters = {}; +const getters = { + parseTime: () => { + return (timestamp) => { + dayjs(new Date(timestamp)).format('MM-DD HH:mm:ss'); + }; + }, + parseContent: () => { + return (payload) => { + payload = JSON.parse(payload); + if ('text' in payload) { + payload = payload.text; + } else { + payload = '[未知消息]'; + } + return payload; + }; + }, +}; const mutations = { setSocket: (state, data) => (state.socket = data), setHeart: (state, data) => (state.heart = data), setTask: (state, data) => (state.task = data), addTask: (state, data) => state.task.push(data), delTask: (state, data) => state.task.splice(data, 1), - receive: (state, { code, traceType, content }) => { - if (code === 200) { - switch (traceType) { - case 27: - state.sessionData = content; - break; - default: - break; - } - } - }, + setCurrentSession: (state, data) => (state.currentSession = data), + setSessionData: (state, data) => (state.sessionData = data), + setMessageList: (state, data) => (state.messageList = data), }; const actions = { /** @@ -36,17 +48,7 @@ const actions = { if (window.WebSocket) { const socket = new WebSocket('ws://192.168.10.93:8090/ws?client=' + token); socket.onmessage = ({ data }) => { - data = JSON.parse(data); - if (data.traceType !== 0) { - let index = state.task.findIndex((item) => item.traceId === data.traceId); - if (index !== -1) { - commit('delTask', index); - commit('receive', data); - console.info('[chat] msg', data); - } else { - console.info('[chat] deprecated', data); - } - } + dispatch('receive', data); }; socket.onopen = () => { commit( @@ -110,7 +112,47 @@ const actions = { } }, /** - * 查询回话列表 + * 接收数据 + */ + receive: ({ state, commit, dispatch }, data) => { + data = JSON.parse(data); + if (data.traceType !== 0) { + let index = state.task.findIndex((item) => item.traceId === data.traceId); + if (index !== -1) { + commit('delTask', index); + dispatch('handle', data); + console.info('[chat] msg', data); + } else { + console.info('[chat] deprecated', data); + } + } + }, + handle: ({ state, commit, dispatch }, { code, traceType, content, session }) => { + if (code === 200) { + switch (traceType) { + // 收到消息 + case 25: + if (session.id === state.currentSession) { + commit('setMessageList', [...state.messageList, content.content]); + } else { + dispatch('querySession'); + } + break; + // 会话列表 + case 27: + commit('setSessionData', content); + break; + // 消息列表 + case 28: + commit('setMessageList', content); + break; + default: + break; + } + } + }, + /** + * 查询会话列表 */ querySession: ({ dispatch }) => { dispatch('invoke', { @@ -118,6 +160,15 @@ const actions = { content: { storeId: 1 }, }); }, + /** + * 查询会话消息列表 + */ + querySessionMessage: ({ state, dispatch }) => { + dispatch('invoke', { + traceType: 28, + content: { sessionId: state.currentSession, size: 100, topMessageId: null }, + }); + }, }; export default { state, diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index 611f942..9c869b3 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -1,6 +1,5 @@ @@ -125,106 +134,18 @@ }); // 会话 + const currentSessionId = computed(() => store.state.chat.currentSession); const state = reactive({ - currentIndex: 0, message: '', }); const sessionList = computed(() => { return store.state.chat.sessionData?.sessionVOS || []; }); - const currentSession = computed(() => { - return sessionList[state.currentIndex]; - }); - // 格式化时间戳 - const parseTime = (timestamp) => dayjs(new Date(timestamp)).format('MM-DD HH:mm:ss'); - - const parseContent = (payload) => { - payload = JSON.parse(payload); - if ('text' in payload) { - payload = payload.text; - } else { - payload = '[未知消息]'; - } - return payload; - }; - const sessionMessageList = reactive([ - { - type: 'text', - content: '你好,请问这个能有优惠吗?', - time: '2019-12-12', - from: 'customer', - avatar: 'https://placem.at/people', - name: '小可爱', - }, - { - type: 'notify', - content: '小马 将该会话转移给 小艾,并留言:发货问题', - time: '2019-12-12', - from: 'system', - }, - { - type: 'text', - content: '没有', - time: '2019-12-12', - from: 'employee', - avatar: 'https://placem.at/people', - name: '小爱', - }, - { - type: 'product', - content: `{ - "id": 18, - "name": "柏战K20青轴机械键盘 混光机械键盘青轴电竞网吧游戏键盘电脑键盘", - "categoryId": 51, - "startingPrice": 79.0, - "totalStock": 1200, - "mainPicture": "https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN011nFEoP1DrheUNhNgU_!!3435280270-0-cib.jpg", - "remoteAreaPostage": 10.0, - "singleBuyLimit": 0, - "isEnable": true, - "remark": "柏战K20青轴机械键盘 混光机械键盘青轴电竞网吧游戏键盘电脑键盘" - }`, - time: '2019-12-12', - from: 'customer', - avatar: 'https://placem.at/people', - name: '小可爱', - }, - { - type: 'order', - content: `{ - "orderId": 392016, - "orderNo": "6391f99d063e", - "userId": 513, - "userPhone": "17683712911", - "payAmount": 859.0, - "orderStatus": 2, - "orderStatusDesc": "已关闭", - "payType": 1, - "payTypeDesc": "未支付", - "orderSource": 2, - "orderSourceDesc": "安卓端APP", - "submitTime": "2022-05-10 20:36:59", - "product": { - "id": 18, - "name": "柏战K20青轴机械键盘 混光机械键盘青轴电竞网吧游戏键盘电脑键盘", - "categoryId": 51, - "startingPrice": 79.0, - "totalStock": 1200, - "mainPicture": "https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/mall-product/productO1CN011nFEoP1DrheUNhNgU_!!3435280270-0-cib.jpg", - "remoteAreaPostage": 10.0, - "singleBuyLimit": 0, - "isEnable": true, - "remark": "柏战K20青轴机械键盘 混光机械键盘青轴电竞网吧游戏键盘电脑键盘" - } - }`, - time: '2019-12-12', - from: 'customer', - avatar: 'https://placem.at/people', - name: '小可爱', - }, - ]); - const handleChangeSession = (index) => { - state.currentIndex = index; + const currentSession = computed(() => sessionList.value.find((item) => item.id === currentSessionId.value)); + const sessionMessageList = computed(() => store.state.chat.messageList); + const handleChangeSession = (id) => { + store.commit('chat/setCurrentSession', id); + store.dispatch('chat/querySessionMessage'); }; // 表情 @@ -384,6 +305,11 @@ flex: 1; display: flex; flex-direction: column; + &.empty { + justify-content: center; + align-items: center; + color: #999; + } .content-header { height: 60px; display: flex; diff --git a/src/views/chat/message.vue b/src/views/chat/message.vue index 0d12e53..150281e 100644 --- a/src/views/chat/message.vue +++ b/src/views/chat/message.vue @@ -2,16 +2,16 @@
-
- +
+
- {{ props.message.name }} + {{ props.session.fromNickname }}