callManager.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import TIM from 'tim-wx-sdk';
  2. import TUICallEngine, { EVENT } from 'tuicall-engine-wx';
  3. /**
  4. * @param {Number} sdkAppID 用户的sdkAppID 必传
  5. * @param {String} userID 用户的userID 必传
  6. * @param {String} userSig 用户的userSig 必传
  7. * @param {String} globalCallPagePath 跳转的路径 必传
  8. * @param {ChatSDK} tim tim实例 非必传
  9. */
  10. const PREFIX = 'callManager';
  11. export class CallManager {
  12. sdkAppID = 0;
  13. userID = '';
  14. userSig = '';
  15. tim = null;
  16. globalCallPagePath = '';
  17. currentListenStatus = false;
  18. async init(params) {
  19. if (wx.$globalCallSign) return;
  20. const { sdkAppID, SDKAppID, userID, tim, globalCallPagePath, userSig } = params;
  21. this.sdkAppID = sdkAppID || SDKAppID;
  22. this.userID = userID;
  23. this.userSig = userSig;
  24. this.globalCallPagePath = globalCallPagePath;
  25. this.tim = tim;
  26. // 挂载全局变量
  27. wx.$globalCallSign = true;
  28. // 设置标志位 用于移除监听
  29. wx.$CallManagerInstance = this;
  30. if (!this.tim) {
  31. this.tim = TIM.create({
  32. SDKAppID: this.sdkAppID,
  33. });
  34. }
  35. // 创建 TUICallEngine 实例
  36. wx.$TUICallEngine = TUICallEngine.createInstance({
  37. sdkAppID: this.sdkAppID,
  38. tim: this.tim,
  39. });
  40. // 调用 init 方法
  41. try {
  42. await wx.$TUICallEngine.init({
  43. userID: this.userID,
  44. userSig: this.userSig,
  45. });
  46. // 监听 TUICallEngine 内部的 TSignaling 事件
  47. this.addEngineInvite();
  48. } catch (error) {
  49. wx.$globalCallSign = false;
  50. wx.$CallManagerInstance = null;
  51. }
  52. console.log(`${PREFIX} init Ready!`);
  53. };
  54. addEngineInvite() {
  55. if (this.currentListenStatus) return;
  56. this.currentListenStatus = true;
  57. wx.$TUICallEngine.on(EVENT.INVITED, this.handleNewInvitationReceived, this);
  58. };
  59. addEngineCallEnd() {
  60. // 通话被取消
  61. wx.$TUICallEngine.on(EVENT.CALLING_CANCEL, this.handleCallEnd, this);
  62. // 通话结束
  63. wx.$TUICallEngine.on(EVENT.CALL_END, this.handleCallEnd, this);
  64. }
  65. removeEngineInvite() {
  66. this.currentListenStatus = false;
  67. wx.$TUICallEngine.off(EVENT.INVITED, this.handleNewInvitationReceived, this);
  68. this.removeEngineCallEnd();
  69. }
  70. removeEngineCallEnd() {
  71. // 若当前已在globalCall页面 则无需处理
  72. if (this.getRoute().route === this.globalCallPagePath) {
  73. return;
  74. }
  75. // 通话被取消
  76. wx.$TUICallEngine.off(EVENT.CALLING_CANCEL, this.handleCallEnd, this);
  77. // 通话结束
  78. wx.$TUICallEngine.off(EVENT.CALL_END, this.handleCallEnd, this);
  79. }
  80. handleNewInvitationReceived(event) {
  81. // 若当前已在globalCall页面 则无需处理
  82. if (this.getRoute().route === this.globalCallPagePath) {
  83. return;
  84. }
  85. // 监听 TUICallEngine 自身的通话结束事件
  86. this.addEngineCallEnd();
  87. const configData = {
  88. sdkAppID: this.sdkAppID,
  89. userID: this.userID,
  90. userSig: this.userSig,
  91. };
  92. wx.navigateTo({
  93. url: `/${this.globalCallPagePath}?data=${JSON.stringify(event)}&configData=${JSON.stringify(configData)}`,
  94. });
  95. };
  96. handleCallEnd() {
  97. wx.$TUICallEngine._resetTUICallEngine();
  98. wx.navigateBack({
  99. success: () => {
  100. },
  101. fail: () => {
  102. },
  103. complete: () => {
  104. wx.$TUICallEngine.off(EVENT.CALLING_CANCEL, this.handleCallEnd, this);
  105. wx.$TUICallEngine.off(EVENT.CALL_END, this.handleCallEnd, this);
  106. },
  107. });
  108. }
  109. // 获取当前的页面地址
  110. getRoute() {
  111. const pages = getCurrentPages();
  112. const currentPage = pages[pages.length - 1];
  113. return currentPage;
  114. }
  115. // 卸载 callManger
  116. async destroyed() {
  117. this.removeEngineInvite();
  118. this.reset();
  119. await wx.$TUICallEngine.destroyInstance();
  120. wx.$globalCallSign = false;
  121. wx.$TUICallEngine = null;
  122. }
  123. reset() {
  124. this.sdkAppID = 0;
  125. this.userID = '';
  126. this.userSig = '';
  127. this.tim = null;
  128. this.globalCallPagePath = '';
  129. }
  130. };