You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-app/src/common/utils/im.js

57 lines
1.3 KiB

/*
* @Author: ch
* @Date: 2022-05-20 11:00:07
* @LastEditors: ch
* @LastEditTime: 2022-06-13 10:11:33
* @Description: file content
*/
import MsbIm from '@/common/plugins/msbIm' ;
import { ToAsyncAwait, FormatJsonSearch } from '@/common/utils';
import { ApiGetCurrentUser } from '@/common/api/account';
import { ApiGetSoketTicket } from '@/common/api/im';
import $store from '@/common/store';
import ENV from '@/common/config/env';
const Im = new MsbIm({
reconnect: true,
});
const ImInit = async () => {
const { error, result } = await ApiGetSoketTicket();
if (error) {
return false;
}
const par = FormatJsonSearch({
client: result.client,
ticket: result.ticket,
// 1普通用户 2客服链接
connect: 1,
user: $store.state.userInfo.id,
nickname: $store.state.userInfo.nickname,
avatar : $store.state.userInfo.avatar
})
await ToAsyncAwait(Im.init({
url: `${ENV.imUrl}/ws${par}`
}))
};
Im.interceptors.dataChangeAfter = () => {
$store.commit('SET_IM_DATA', Im.sessionData);
let msgCount = 0;
Im.sessionData.forEach(i => {
msgCount += i.unreadCount;
})
$store.commit('SET_IM_MSG_COUNT', msgCount);
}
Im.interceptors.onClose = () => {
Im.setSessionData([]);
Im.setCurSessionId(null);
$store.commit('SET_IM_DATA', []);
$store.commit('SET_IM_MSG_COUNT', 0);
}
export {
Im,
ImInit
}