perf: IM交互优化

fix/0524_ch
向文可 3 years ago
parent 8df116968e
commit 78f371960a

@ -1,3 +1,4 @@
VITE_BASE_URL=/api VITE_BASE_URL=/api
VITE_SOCKET_URL=wss://k8s-horse-gateway.mashibing.cn/ws #VITE_SOCKET_URL=wss://k8s-horse-gateway.mashibing.cn/ws
VITE_SOCKET_URL=ws://192.168.10.93:8090/ws
VITE_REQUEST_TIMEOUT=5000 VITE_REQUEST_TIMEOUT=5000

@ -22,8 +22,8 @@
import LayoutMain from './components/main.vue'; import LayoutMain from './components/main.vue';
import LayoutMenu from './components/menu.vue'; import LayoutMenu from './components/menu.vue';
import LayoutTabs from './components/tabs.vue'; import LayoutTabs from './components/tabs.vue';
// const store = useStore(); const store = useStore();
// store.dispatch('chat/connect'); store.dispatch('chat/connect');
</script> </script>
<style lang="less" scoped> <style lang="less" scoped>

@ -144,9 +144,12 @@ const actions = {
if (data.traceType !== 0) { if (data.traceType !== 0) {
let index = state.task.findIndex((item) => item.traceId === data.traceId); let index = state.task.findIndex((item) => item.traceId === data.traceId);
if (index !== -1) { if (index !== -1) {
console.info('[chat] data', data);
commit('delTask', index); commit('delTask', index);
dispatch('handle', data); dispatch('handle', data);
} else if (data.traceType === 25) {
console.info('[chat] msg', data); console.info('[chat] msg', data);
dispatch('handle', data);
} else { } else {
console.info('[chat] deprecated', data); console.info('[chat] deprecated', data);
} }
@ -155,12 +158,12 @@ const actions = {
/** /**
* 处理数据 * 处理数据
*/ */
handle: ({ state, commit, dispatch }, { code, traceType, content, session }) => { handle: ({ state, commit, dispatch }, { code, traceType, content }) => {
if (code === 200) { if (code === 200) {
switch (traceType) { switch (traceType) {
case 25: // 收到消息 case 25: // 收到消息
if (session.id === state.currentSession) { if (content.sessionId === state.currentSession) {
commit('setMessageList', [...state.messageList, content.content]); commit('setMessageList', [...state.messageList, content]);
dispatch('submitRead'); dispatch('submitRead');
} else { } else {
dispatch('querySession'); dispatch('querySession');
@ -173,7 +176,7 @@ const actions = {
commit('setSessionData', content); commit('setSessionData', content);
break; break;
case 28: // 消息列表 case 28: // 消息列表
commit('setMessageList', content); commit('setMessageList', [...content, ...state.messageList]);
break; break;
case 29: // 客服列表 case 29: // 客服列表
commit('setCustomerServiceList', content); commit('setCustomerServiceList', content);
@ -195,10 +198,10 @@ const actions = {
/** /**
* 查询会话消息列表 * 查询会话消息列表
*/ */
querySessionMessage: ({ state, dispatch }) => { querySessionMessage: ({ state, dispatch }, data) => {
dispatch('invoke', { dispatch('invoke', {
traceType: 28, traceType: 28,
content: { sessionId: state.currentSession, size: 100, topMessageId: null }, content: { sessionId: state.currentSession, size: 10, topMessageId: null, ...data },
}); });
}, },
/** /**

@ -68,6 +68,9 @@
</div> </div>
</div> </div>
<el-scrollbar ref="refsMessageList" class="message-list"> <el-scrollbar ref="refsMessageList" class="message-list">
<el-button v-if="sessionMessageList.length" class="load" type="text" @click="handleLoadMore">
加载更多
</el-button>
<message-item <message-item
v-for="(item, index) in sessionMessageList" v-for="(item, index) in sessionMessageList"
:key="index" :key="index"
@ -156,6 +159,7 @@
const currentSession = computed(() => sessionList.value.find((item) => item.id === currentSessionId.value)); const currentSession = computed(() => sessionList.value.find((item) => item.id === currentSessionId.value));
const handleChangeSession = (id) => { const handleChangeSession = (id) => {
store.commit('chat/setCurrentSession', id); store.commit('chat/setCurrentSession', id);
store.commit('chat/setMessageList', []);
store.dispatch('chat/querySessionMessage'); store.dispatch('chat/querySessionMessage');
store.dispatch('chat/submitRead'); store.dispatch('chat/submitRead');
}; };
@ -163,11 +167,21 @@
// //
const sessionMessageList = computed(() => store.state.chat.messageList); const sessionMessageList = computed(() => store.state.chat.messageList);
const refsMessageList = ref(null); const refsMessageList = ref(null);
watch(sessionMessageList, () => { watch(sessionMessageList, (value, old) => {
setTimeout(() => { let offset = refsMessageList.value
refsMessageList.value.setScrollTop(refsMessageList.value.scrollbar$.scrollHeight); ? refsMessageList.value.resize$.scrollHeight - refsMessageList.value.resize$.scrollTop
}, 1000); : 0;
nextTick(() => {
if (!old?.length || value.indexOf(old[0]) === 0) {
refsMessageList.value.setScrollTop(refsMessageList.value.resize$.scrollHeight);
} else {
refsMessageList.value.setScrollTop(refsMessageList.value.resize$.scrollHeight - offset);
}
});
}); });
const handleLoadMore = () => {
store.dispatch('chat/querySessionMessage', { topMessageId: unref(sessionMessageList)[0].id });
};
const handleSendMessage = (data) => { const handleSendMessage = (data) => {
if (!data) { if (!data) {
store.dispatch('chat/submitMessage', { text: state.message }); store.dispatch('chat/submitMessage', { text: state.message });
@ -404,6 +418,9 @@
.message-list { .message-list {
flex: 1; flex: 1;
background-color: #f1f1f1; background-color: #f1f1f1;
.load {
width: 100%;
}
} }
.operation-bar { .operation-bar {
display: flex; display: flex;

@ -23,8 +23,8 @@ export default (configEnv) => {
// target: 'http://192.168.10.109:8090/', // 显雨 // target: 'http://192.168.10.109:8090/', // 显雨
// target: 'http://192.168.10.5:4500', // 高玉 // target: 'http://192.168.10.5:4500', // 高玉
// target: 'http://192.168.10.67:8090', // 罗战 // target: 'http://192.168.10.67:8090', // 罗战
// target: 'http://192.168.10.93:8090', // 周渺 target: 'http://192.168.10.93:8090', // 周渺
target: 'https://k8s-horse-gateway.mashibing.cn/', // 测试地址 // target: 'https://k8s-horse-gateway.mashibing.cn/', // 测试地址
// target: 'https://you-gateway.mashibing.com', // 生产环境 // target: 'https://you-gateway.mashibing.com', // 生产环境
changeOrigin: true, changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, ''), rewrite: (path) => path.replace(/^\/api/, ''),

Loading…
Cancel
Save