server.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import TUICore, { TUILogin, TUIConstants } from '@tencentcloud/tui-core';
  2. import TUIChatEngine, { TUITranslateService } from '@tencentcloud/chat-uikit-engine';
  3. import { TUIGlobal } from '@tencentcloud/universal-api';
  4. import { ITUIComponents, ITUIPlugins } from './interface';
  5. import { isFunction, isObject } from './utils';
  6. import { isApp } from './utils/env';
  7. import CallkitPluginServer from './plugins/extension-server/callkit';
  8. // #ifndef MP-WEIXIN
  9. import TUILocales from './locales';
  10. // #endif
  11. export default class TUIChatKit {
  12. static isInitialized: boolean;
  13. public chat: any;
  14. public SDKAppID: number;
  15. public TUICore: any;
  16. public TUIChatEngine: any;
  17. public TUIGlobal: any;
  18. public TUIComponents: ITUIComponents = {};
  19. public TUIPlugins: ITUIPlugins = {};
  20. constructor() {
  21. this.TUICore = TUICore;
  22. this.TUIChatEngine = TUIChatEngine;
  23. this.TUIGlobal = TUIGlobal;
  24. this.SDKAppID = 0;
  25. this.TUIGlobal._isTIMCallKit = true;
  26. TUICore.registerEvent(TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED, TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS, this);
  27. TUICore.registerEvent(TUIConstants.TUITranslate.EVENT.LANGUAGE_CHANGED, TUIConstants.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS, this);
  28. // register translate and voiceToText service event tracking
  29. TUICore.registerService(TUIConstants.TUITranslatePlugin.SERVICE.NAME, 1);
  30. TUICore.registerService(TUIConstants.TUIVoiceToTextPlugin.SERVICE.NAME, 1);
  31. }
  32. /**
  33. * Listen for the success notification of TUILogin.login
  34. */
  35. public onNotifyEvent(eventName: string, subKey: string, params?: Record<string, any>) {
  36. if (eventName === TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED) {
  37. switch (subKey) {
  38. case TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS:
  39. this.login();
  40. break;
  41. }
  42. }
  43. if (eventName === TUIConstants.TUITranslate.EVENT.LANGUAGE_CHANGED) {
  44. switch (subKey) {
  45. case TUIConstants.TUITranslate.EVENT_SUB_KEY.CHANGE_SUCCESS:
  46. if (params?.language) {
  47. TUITranslateService.changeLanguage(params.language);
  48. }
  49. break;
  50. }
  51. }
  52. }
  53. /**
  54. * init
  55. */
  56. public init() {
  57. // Backward compatibility, the new version executes the init operation by default in index.ts
  58. if (TUIChatKit.isInitialized) {
  59. return;
  60. }
  61. TUIChatKit.isInitialized = true;
  62. // Execute call server when native plugin TUICallKit exists
  63. if (isApp) {
  64. new CallkitPluginServer();
  65. }
  66. // TUITranslateService init
  67. // #ifndef MP-WEIXIN
  68. TUITranslateService.provideLanguages({ ...TUILocales });
  69. TUITranslateService.useI18n();
  70. // #endif
  71. // TUIComponents global install
  72. TUIGlobal.TUIComponents = this.TUIComponents;
  73. // TUIPlugins global install
  74. TUIGlobal.TUIPlugins = this.TUIPlugins;
  75. console.warn('[TUIChatKit]: init success.');
  76. }
  77. /**
  78. * login
  79. */
  80. public login() {
  81. const { chat, SDKAppID, userID, userSig } = TUILogin.getContext();
  82. this.SDKAppID = SDKAppID;
  83. this.TUIChatEngine.login({
  84. chat,
  85. SDKAppID,
  86. userID,
  87. userSig,
  88. });
  89. this.chat = chat;
  90. return this.TUIChatEngine;
  91. }
  92. /**
  93. * Single component mounting
  94. *
  95. * @param {string} componentName
  96. * @param {any} component
  97. * @param {any=} env
  98. * @returns {TUICore} mounted instance
  99. */
  100. public component(componentName: string, component: any, env?: any) {
  101. if (this?.TUIComponents?.componentName) {
  102. console.warn(
  103. `[TUIChatKit]: ${this?.TUIComponents?.componentName} component has already been applied to target TUIChatEngine.`,
  104. );
  105. } else {
  106. this.TUIComponents[componentName] = component;
  107. env?.component(componentName, component);
  108. }
  109. if (env) {
  110. !TUIGlobal.Vue && (TUIGlobal.Vue = env);
  111. }
  112. return this.TUIChatEngine;
  113. }
  114. /**
  115. * Component list mount
  116. *
  117. * @param {object} components
  118. * @param {any=} env: Vue2/Vue3 environment
  119. * @returns {TUICore} mounted instance
  120. */
  121. public components(components: object, env?: any) {
  122. if (!components || !isObject(components)) {
  123. console.warn('[TUIChatKit]: components is empty or not object.');
  124. } else {
  125. Object?.keys(components)?.forEach((key: string) => {
  126. this.component(key, components[key as keyof typeof components], env);
  127. });
  128. }
  129. return this.TUIChatEngine;
  130. }
  131. /**
  132. * Plugin Injection
  133. *
  134. * @param {any} TUIPlugin
  135. * @param {any} options
  136. * @returns {TUICore} mounted instance
  137. */
  138. public use(TUIPluginName: string, TUIPlugin: any, options?: any) {
  139. if (!this.TUICore) {
  140. console.warn(
  141. `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} can't be used before init.`,
  142. );
  143. return;
  144. }
  145. if (this.TUIPlugins[TUIPluginName]) {
  146. console.warn(
  147. `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} has already been applied to target TUIChatEngine.`,
  148. );
  149. } else if (TUIPlugin && isFunction(TUIPlugin?.plugin)) {
  150. this.TUIPlugins[TUIPluginName] = TUIPlugin;
  151. TUIPlugin?.plugin(this, options);
  152. } else if (isFunction(TUIPlugin)) {
  153. this.TUIPlugins[TUIPluginName] = TUIPlugin;
  154. TUIPlugin(this, options);
  155. } else {
  156. console.warn(
  157. '[TUIChatKit]: A plugin must either be a function or an object with an "plugin" '
  158. + 'function.'
  159. + this.TUIPlugins[TUIPluginName]
  160. + 'does not comply with the above rules.',
  161. );
  162. }
  163. return this.TUIChatEngine;
  164. }
  165. }