callkit.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import TUICore, { TUIConstants, TUILogin } from '@tencentcloud/tui-core';
  2. import { TUIGlobal } from '@tencentcloud/universal-api';
  3. export default class CallkitPluginServer {
  4. constructor() {
  5. // Listen for successful login
  6. TUICore.registerEvent(TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED, TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS, this);
  7. // Native plugin callkit registers call service
  8. TUICore.registerService(TUIConstants.TUICalling.SERVICE.NAME, this);
  9. // Native plugin callkit registration extension
  10. TUICore.registerExtension(TUIConstants.TUIChat.EXTENSION.INPUT_MORE.EXT_ID, this);
  11. }
  12. /**
  13. * Listen for the successful notification of TUILogin.login and then log in with callkit
  14. */
  15. public onNotifyEvent(eventName: string, subKey: string) {
  16. if (eventName === TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED) {
  17. let SDKAppID, userID, userSig, context;
  18. switch (subKey) {
  19. case TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS:
  20. context = TUILogin.getContext();
  21. SDKAppID = context.SDKAppID;
  22. userID = context.userID;
  23. userSig = context.userSig;
  24. TUIGlobal.$TUICallKit && TUIGlobal.$TUICallKit.login({
  25. SDKAppID,
  26. userID,
  27. userSig,
  28. }, (res: any) => {
  29. if (res.code === 0) {
  30. console.log('TUICallkit login success!');
  31. // Floating window function
  32. TUIGlobal.$TUICallKit.enableFloatWindow(true);
  33. } else {
  34. console.error(`TUICallkit login failed,${res.msg}`);
  35. }
  36. });
  37. break;
  38. }
  39. }
  40. }
  41. /**
  42. * Native plugin callkit implements onGetExtension method
  43. */
  44. public onGetExtension(extensionID: string, params: Record<string, any>) {
  45. if (!TUIGlobal.$TUICallKit) {
  46. console.warn('请检查原生插件 TencentCloud-TUICallKit 是否已集成');
  47. return [];
  48. }
  49. if (extensionID === TUIConstants.TUIChat.EXTENSION.INPUT_MORE.EXT_ID) {
  50. const list = [];
  51. const voiceCallExtension = {
  52. weight: 1000,
  53. text: '语音通话',
  54. icon: 'https://web.sdk.qcloud.com/component/TUIKit/assets/call.png',
  55. data: {
  56. name: 'voiceCall',
  57. },
  58. listener: {
  59. onClicked: (options: any) => {
  60. this.setCallExtension(options);
  61. },
  62. },
  63. };
  64. const videoCallExtension = {
  65. weight: 900,
  66. text: '视频通话',
  67. icon: 'https://web.sdk.qcloud.com/component/TUIKit/assets/call-video-reverse.svg',
  68. data: {
  69. name: 'videoCall',
  70. },
  71. listener: {
  72. onClicked: (options: any) => {
  73. this.setCallExtension(options);
  74. },
  75. },
  76. };
  77. if (!(params as any)?.filterVoice) {
  78. list.push(voiceCallExtension);
  79. }
  80. if (!(params as any)?.filterVideo) {
  81. list.push(videoCallExtension);
  82. }
  83. return list;
  84. }
  85. }
  86. /**
  87. * Native plugin callkit implements onCall method
  88. */
  89. public onCall(method: string, params: any) {
  90. if (!TUIGlobal.$TUICallKit) {
  91. console.warn('请检查原生插件 TencentCloud-TUICallKit 是否已集成');
  92. return;
  93. }
  94. if (method === TUIConstants.TUICalling.SERVICE.METHOD.START_CALL) {
  95. const { groupID = undefined, userIDList = [], type, callParams } = params;
  96. if (groupID) {
  97. TUIGlobal.$TUICallKit.groupCall({
  98. groupID,
  99. userIDList,
  100. callMediaType: type,
  101. callParams,
  102. }, (res: any) => {
  103. if (res.code === 0) {
  104. console.log('TUICallkit groupCall success');
  105. } else {
  106. console.error(`TUICallkit groupCall failed,${res.msg}`);
  107. }
  108. });
  109. } else if (userIDList.length === 1) {
  110. TUIGlobal.$TUICallKit.call(
  111. {
  112. userID: userIDList[0],
  113. callMediaType: type,
  114. callParams,
  115. },
  116. (res: any) => {
  117. if (res.code === 0) {
  118. console.log('TUICallkit call success');
  119. } else {
  120. console.log(`TUICallkit call failed,${res.msg}`);
  121. }
  122. });
  123. }
  124. }
  125. }
  126. public setCallExtension(options: any) {
  127. const { groupID = undefined, userIDList = [], type, callParams } = options;
  128. try {
  129. if (groupID) {
  130. // group call
  131. TUIGlobal.$TUICallKit.groupCall({
  132. groupID,
  133. userIDList,
  134. callMediaType: type,
  135. callParams,
  136. }, (res: any) => {
  137. if (res.code === 0) {
  138. console.log('TUICallkit groupCall success');
  139. } else {
  140. console.log(`TUICallkit groupCall failed,${res.msg}`);
  141. }
  142. });
  143. } else if (userIDList.length === 1) {
  144. // 1v1 call
  145. TUIGlobal.$TUICallKit.call(
  146. {
  147. userID: userIDList[0],
  148. callMediaType: type,
  149. callParams,
  150. },
  151. (res: any) => {
  152. if (res.code === 0) {
  153. console.log('TUICallkit call success');
  154. } else {
  155. console.log(`TUICallkit call failed,${res.msg}`);
  156. }
  157. },
  158. );
  159. }
  160. } catch (error: any) {
  161. TUIGlobal.showToast({
  162. title: '拨打失败!',
  163. icon: 'error',
  164. });
  165. }
  166. }
  167. }