server.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 TUILocales from './locales';
  6. import { isFunction, isObject } from './utils';
  7. import { isApp } from './utils/env';
  8. import CallkitPluginServer from './plugins/extension-server/callkit';
  9. export default class TUIChatKit {
  10. static isInitialized: boolean;
  11. public chat: any;
  12. public SDKAppID: number;
  13. public TUICore: any;
  14. public TUIChatEngine: any;
  15. public TUIGlobal: any;
  16. public TUIComponents: ITUIComponents = {};
  17. public TUIPlugins: ITUIPlugins = {};
  18. constructor() {
  19. this.TUICore = TUICore;
  20. this.TUIChatEngine = TUIChatEngine;
  21. this.TUIGlobal = TUIGlobal;
  22. this.SDKAppID = 0;
  23. this.TUIGlobal._isTIMCallKit = true;
  24. TUICore.registerEvent(
  25. TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED,
  26. TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS,
  27. this,
  28. );
  29. // register translate and voiceToText service event tracking
  30. TUICore.registerService(TUIConstants.TUITranslatePlugin.SERVICE.NAME, 1);
  31. TUICore.registerService(TUIConstants.TUIVoiceToTextPlugin.SERVICE.NAME, 1);
  32. }
  33. /**
  34. * 监听 TUILogin.login 的成功通知
  35. * @param { TUIInitParam } params 初始化参数
  36. */
  37. public onNotifyEvent(eventName: string, subKey: string) {
  38. if (eventName === TUIConstants.TUILogin.EVENT.LOGIN_STATE_CHANGED) {
  39. switch (subKey) {
  40. case TUIConstants.TUILogin.EVENT_SUB_KEY.USER_LOGIN_SUCCESS:
  41. this.login();
  42. break;
  43. }
  44. }
  45. }
  46. /**
  47. * init 初始化
  48. */
  49. public init() {
  50. // 向下兼容,新版本在 index.ts 中默认执行 init 操作
  51. if (TUIChatKit.isInitialized) {
  52. return;
  53. }
  54. TUIChatKit.isInitialized = true;
  55. // 原生插件 TUICallKit 存在时执行 call server
  56. if (isApp) {
  57. new CallkitPluginServer();
  58. }
  59. // TUITranslateService init
  60. TUITranslateService.provideLanguages({ ...TUILocales });
  61. TUITranslateService.useI18n();
  62. // TUIComponents global install
  63. TUIGlobal.TUIComponents = this.TUIComponents;
  64. // TUIPlugins global install
  65. TUIGlobal.TUIPlugins = this.TUIPlugins;
  66. console.warn('[TUIChatKit]: init success.');
  67. }
  68. /**
  69. * login 登录
  70. */
  71. public login() {
  72. const { chat, SDKAppID, userID, userSig } = TUILogin.getContext();
  73. this.SDKAppID = SDKAppID;
  74. this.TUIChatEngine.login({
  75. chat,
  76. SDKAppID,
  77. userID,
  78. userSig,
  79. });
  80. this.chat = chat;
  81. return this.TUIChatEngine;
  82. }
  83. /**
  84. * 单个组件挂载
  85. *
  86. * @param {string} componentName 挂载的组件名
  87. * @param {any} component 挂载的组件
  88. * @param {any=} env Vue2/Vue3环境
  89. * @returns {TUICore} 挂载后的实例
  90. */
  91. public component(componentName: string, component: any, env?: any) {
  92. if (this?.TUIComponents?.componentName) {
  93. console.warn(
  94. `[TUIChatKit]: ${this?.TUIComponents?.componentName} component has already been applied to target TUIChatEngine.`,
  95. );
  96. } else {
  97. this.TUIComponents[componentName] = component;
  98. env?.component(componentName, component);
  99. }
  100. if (env) {
  101. !TUIGlobal.Vue && (TUIGlobal.Vue = env);
  102. }
  103. return this.TUIChatEngine;
  104. }
  105. /**
  106. * 组件列表挂载
  107. *
  108. * @param {object} components 挂载的组件列表
  109. * @param {any=} env Vue2/Vue3环境
  110. * @returns {TUICore} 挂载后的实例
  111. */
  112. public components(components: object, env?: any) {
  113. if (!components || !isObject(components)) {
  114. console.warn('[TUIChatKit]: components is empty or not object.');
  115. } else {
  116. Object?.keys(components)?.forEach((key: string) => {
  117. this.component(key, components[key as keyof typeof components], env);
  118. });
  119. }
  120. return this.TUIChatEngine;
  121. }
  122. /**
  123. * 插件注入
  124. *
  125. * @param {any} TUIPlugin 需要挂载模块的服务
  126. * @param {any} options 其他参数
  127. * @returns {TUICore} 挂载后的实例
  128. */
  129. public use(TUIPluginName: string, TUIPlugin: any, options?: any) {
  130. if (!this.TUICore) {
  131. console.warn(
  132. `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} can't be used before init.`,
  133. );
  134. return;
  135. }
  136. if (this.TUIPlugins[TUIPluginName]) {
  137. console.warn(
  138. `[TUIChatKit]: Plugin ${this.TUIPlugins[TUIPluginName]} has already been applied to target TUIChatEngine.`,
  139. );
  140. } else if (TUIPlugin && isFunction(TUIPlugin?.plugin)) {
  141. this.TUIPlugins[TUIPluginName] = TUIPlugin;
  142. TUIPlugin?.plugin(this, options);
  143. } else if (isFunction(TUIPlugin)) {
  144. this.TUIPlugins[TUIPluginName] = TUIPlugin;
  145. TUIPlugin(this, options);
  146. } else {
  147. console.warn(
  148. '[TUIChatKit]: A plugin must either be a function or an object with an "plugin" '
  149. + 'function.'
  150. + this.TUIPlugins[TUIPluginName]
  151. + 'does not comply with the above rules.',
  152. );
  153. }
  154. return this.TUIChatEngine;
  155. }
  156. }