From 19c29050dd0d09acc5bb31c8c5a29c5e45ccc292 Mon Sep 17 00:00:00 2001 From: ch Date: Mon, 13 Jun 2022 14:20:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9im?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/chat/index.js | 12 +++++++++++- src/store/modules/im/im.js | 14 ++++++++++++-- src/utils/im.js | 21 +++++---------------- src/views/chat/index.vue | 24 ++++++++++++++++++++---- 4 files changed, 48 insertions(+), 23 deletions(-) diff --git a/src/api/chat/index.js b/src/api/chat/index.js index ff501e6..47484c2 100644 --- a/src/api/chat/index.js +++ b/src/api/chat/index.js @@ -2,7 +2,7 @@ * @Author: xwk * @Date: 2022-05-24 17:00:26 * @LastEditors: ch - * @LastEditTime: 2022-06-09 10:10:47 + * @LastEditTime: 2022-06-11 18:41:23 * @Description: file content */ import request from '@/utils/request.js'; @@ -52,6 +52,16 @@ export const getCustomeServiceTicket = () => { }, }); }; + +/** + * 获取当前客服 + */ +export const getCustomerService = () => { + return request({ + url: '/mall/im/admin/waiterUser/getWaiterByUserId', + method: 'get', + }); +}; /** * 获取可转移客服列表 * @param {*} params diff --git a/src/store/modules/im/im.js b/src/store/modules/im/im.js index 10c64aa..760fe51 100644 --- a/src/store/modules/im/im.js +++ b/src/store/modules/im/im.js @@ -2,12 +2,13 @@ * @Author: ch * @Date: 2022-06-07 15:41:05 * @LastEditors: ch - * @LastEditTime: 2022-06-09 10:25:49 + * @LastEditTime: 2022-06-13 14:03:18 * @Description: file content */ import * as api from '@/api/chat'; import dayjs from 'dayjs'; const state = { + curCustomerService: {}, sessionData: [], customerServiceList: [], messageType: { 1: 'text', 2: 'audio', 3: 'image', 4: 'video', 5: 'revoke', 6: 'custom', 7: 'notify' }, @@ -105,6 +106,9 @@ const getters = { }, }; const mutations = { + SET_CUR_SERVICE(state, data) { + state.curCustomerService = data; + }, SET_SESSION_DATA(state, data) { state.sessionData = data; }, @@ -113,6 +117,12 @@ const mutations = { }, }; const actions = { + queryCurCustomerService: ({ commit }) => { + api.getCustomerService().then((res) => { + commit('SET_CUR_SERVICE', res); + }); + }, + /** * 查询可转移客服列表 */ @@ -127,7 +137,7 @@ const actions = { /** * 提交转移会话 */ - submitTransferSession: ({ dispatch }, data) => { + submitTransferSession: ({}, data) => { api.transferCustomerService({ storeId: 1, ...data, diff --git a/src/utils/im.js b/src/utils/im.js index 2ad55fa..bbe97df 100644 --- a/src/utils/im.js +++ b/src/utils/im.js @@ -2,7 +2,7 @@ * @Author: ch * @Date: 2022-06-07 15:52:37 * @LastEditors: ch - * @LastEditTime: 2022-06-09 10:59:15 + * @LastEditTime: 2022-06-13 14:14:26 * @Description: file content */ import * as api from '@/api/chat'; @@ -13,27 +13,16 @@ import { FormatJsonSearch, ToAsyncAwait } from '@/utils/utils'; const Im = new MsbIm({ reconnect: true, }); -const ImInit = () => { +const ImInit = (waiterId) => { return new Promise((reslove, reject) => { - const storeUc = $store.state.auth.userInfo; - if (!storeUc) { - ImInit(); - return false; - } + console.log(waiterId, 'waiterId'); api.getCustomeServiceTicket().then(async (res) => { + console.log(res); const par = FormatJsonSearch({ client: res.client, ticket: res.ticket, - user: storeUc.userId, - // nickname: storeUc.employeeName, - // avatar: storeUc.avatar, - // 1普通用户 2客服链接 + user: waiterId, connect: 2, - // user: 2, - // client: 'yan_xuan', - // ticket: '9kpEgiLzVG14znSTvElLOJE5MEMa/EGdexhab4CbDmLzDGnE+UXmVOvUs4ixyPeQ', - // nickname: '周渺', - // avatar: 'https://msb-edu-dev.oss-cn-beijing.aliyuncs.com/uc/account-avatar/桌面水果.jpg', }); const { error, result } = await ToAsyncAwait( Im.init({ diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index 01a6c3f..5e369ec 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -149,10 +149,24 @@ const { proxy } = getCurrentInstance(); const router = useRouter(); const store = useStore(); - - ImInit().then(() => { - Im.getSessionList(); + store.dispatch('im/queryCurCustomerService'); + const socketInit = () => { + console.log('----', store.state.im.curCustomerService); + if (typeof store.state.im.curCustomerService !== 'string') { + setTimeout(() => { + socketInit(); + }, 1000); + return false; + } + console.log('++++', typeof store.state.im.curCustomerService); + ImInit(store.state.im.curCustomerService).then(() => { + Im.getSessionList(); + }); + }; + onMounted(() => { + socketInit(); }); + const opts = computed(() => store.state.chat.opts); // 统计 @@ -208,12 +222,14 @@ Im.getHistoryMsg(); }; const handleSendMessage = (e) => { + const curService = store.state.im.curCustomerService; if (e && e.shiftKey) { state.message += '\n'; } else { if (state.message) { Im.sendMsg({ - fromId: userInfo.value.userId, + fromId: curService.waiterId, + fromAvatar: curService.waiterAvatar, content: { toSessionId: currentSessionId.value, payload: { text: state.message },