entry-chat-only.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { TUILogin } from '@tencentcloud/tui-core';
  2. import { TUIConversationService } from '@tencentcloud/chat-uikit-engine';
  3. // #ifdef MP-WEIXIN
  4. import { TUIChatKit } from '../../index.ts';
  5. // #endif
  6. export const initChat = (options: Record<string, string>) => {
  7. // #ifdef MP-WEIXIN
  8. // uni-app packages the mini program.
  9. // If you call TUIChatKit.init() directly during import, an error will be reported.
  10. // You need to init during the page onLoad.
  11. TUIChatKit.init();
  12. // #endif
  13. // When opening TUIChat, the options and options.conversationID parameters carried in the url,
  14. // determine whether to enter the Chat from the [Conversation List] or [Online Communication].
  15. const { chat } = TUILogin.getContext();
  16. if (options && options.conversationID && chat?.isReady()) {
  17. const { conversationID } = options;
  18. // verify conversationID
  19. if (!conversationID.startsWith('C2C') && !conversationID.startsWith('GROUP')) {
  20. console.warn('conversationID from options is invalid.');
  21. return;
  22. }
  23. // open chat
  24. TUIConversationService.switchConversation(conversationID);
  25. }
  26. };
  27. export const logout = (flag: boolean) => {
  28. if (flag) {
  29. return TUILogin.logout();
  30. }
  31. return Promise.resolve();
  32. };