UNPKG

@tencentcloud/chat-uikit-uniapp

Version:

TUIKit 是基于 IM SDK 实现的一套 UI 组件,其包含会话、聊天、群组、个人资料等功能,基于这些精心设计的 UI 组件,您可以快速构建优雅的、可靠的、可扩展的 Chat 应用。

142 lines (136 loc) 4.45 kB
import TUICore, { TUIConstants, TUILogin } from '@tencentcloud/tui-core-lite'; import { TUIGlobal } from '@tencentcloud/universal-api'; export default class CallkitPluginServer { constructor() { // Listen for successful login TUICore.registerEvent(TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED, TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS, this); // Native plugin callkit registers call service TUICore.registerService(TUIConstants.TUICalling.SERVICE.NAME, this); // Native plugin callkit registration extension TUICore.registerExtension(TUIConstants.TUIChat.EXTENSION.INPUT_MORE.EXT_ID, this); } /** * Listen for the successful notification of TUILogin.login and then log in with callkit */ public onNotifyEvent(eventName: string, subKey: string) { if (eventName === TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED) { let SDKAppID, userID, userSig, context; switch (subKey) { case TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS: context = TUILogin.getContext(); SDKAppID = context.SDKAppID; userID = context.userID; userSig = context.userSig; TUIGlobal.$TUICallKit && TUIGlobal.$TUICallKit.login({ SDKAppID, userID, userSig, }, (res: any) => { if (res.code === 0) { console.log('TUICallkit login success!'); // Floating window function TUIGlobal.$TUICallKit.enableFloatWindow(true); } else { console.error(`TUICallkit login failed,${res.msg}`); } }); break; } } } /** * Native plugin callkit implements onGetExtension method */ public onGetExtension(extensionID: string, params: Record<string, any>) { if (!TUIGlobal.$TUICallKit) { console.warn('请检查原生插件 TencentCloud-TUICallKit 是否已集成'); return []; } if (extensionID === TUIConstants.TUIChat.EXTENSION.INPUT_MORE.EXT_ID) { const list = []; const voiceCallExtension = { weight: 1000, text: '语音通话', icon: 'https://web.sdk.qcloud.com/component/TUIKit/assets/uni-app/voice-call.svg', data: { name: 'voiceCall', }, listener: { onClicked: (options: any) => { this.setCallExtension(options); }, }, }; const videoCallExtension = { weight: 900, text: '视频通话', icon: 'https://web.sdk.qcloud.com/component/TUIKit/assets/uni-app/video-call.svg', data: { name: 'videoCall', }, listener: { onClicked: (options: any) => { this.setCallExtension(options); }, }, }; if (!(params as any)?.filterVoice) { list.push(voiceCallExtension); } if (!(params as any)?.filterVideo) { list.push(videoCallExtension); } return list; } } /** * Native plugin callkit implements onCall method */ public onCall(method: string, params: any) { if (!TUIGlobal.$TUICallKit) { console.warn('请检查原生插件 TencentCloud-TUICallKit 是否已集成'); return; } if (method === TUIConstants.TUICalling.SERVICE.METHOD.START_CALL) { const { groupID = undefined, userIDList = [], type, callParams } = params; TUIGlobal.$TUICallKit.calls({ userIDList, callMediaType: type, callParams: { ...callParams, chatGroupId: groupID, }, }, (res: any) => { if (res.code === 0) { console.log('TUICallkit calls success'); } else { console.error(`TUICallkit calls failed,${res.msg}`); } }); } } public setCallExtension(options: any) { const { groupID = undefined, userIDList = [], type, callParams } = options; try { TUIGlobal.$TUICallKit.calls({ userIDList, callMediaType: type, callParams: { ...callParams, chatGroupId: groupID, }, }, (res: any) => { if (res.code === 0) { console.log('TUICallkit calls success'); } else { console.log(`TUICallkit calls failed,${res.msg}`); } }); } catch (error: any) { TUIGlobal.showToast({ title: '拨打失败!', icon: 'error', }); } } }