server.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import TUICore, { TUIConstants } from '@tencentcloud/tui-core';
  2. import {
  3. TUITranslateService,
  4. TUIConversationService,
  5. TUIStore,
  6. StoreName,
  7. } from '@tencentcloud/chat-uikit-engine';
  8. import { TUIGlobal } from '@tencentcloud/universal-api';
  9. import { CONV_CREATE_TYPE } from '../../constant';
  10. import { isUniFrameWork } from '../../utils/env';
  11. import createGroupIcon from '../../assets/icon/start-group.svg';
  12. import createC2CIcon from '../../assets/icon/icon-c2c.svg';
  13. import { enableSampleTaskStatus } from '../../utils/enableSampleTaskStatus';
  14. export default class TUIConversationServer {
  15. static instance: TUIConversationServer;
  16. private onCallParamsMap: Map<string, any>;
  17. private onCallCallbackMap: Map<string, () => void>;
  18. public constants: typeof TUIConstants;
  19. constructor() {
  20. TUICore.registerService(TUIConstants.TUIConversation.SERVICE.NAME, this);
  21. TUICore.registerExtension(TUIConstants.TUISearch.EXTENSION.SEARCH_MORE.EXT_ID, this);
  22. this.onCallParamsMap = new Map();
  23. this.onCallCallbackMap = new Map();
  24. this.constants = TUIConstants;
  25. }
  26. static getInstance(): TUIConversationServer {
  27. if (!TUIConversationServer.instance) {
  28. TUIConversationServer.instance = new TUIConversationServer();
  29. }
  30. return TUIConversationServer.instance;
  31. }
  32. public getOnCallParams(method: string): any {
  33. return this.onCallParamsMap.get(method);
  34. }
  35. public getOnCallCallback(method: string): (() => void) | undefined {
  36. return this.onCallCallbackMap.get(method);
  37. }
  38. public onCall(method: string, params: Record<string, any>, callback: () => void): void {
  39. this.onCallParamsMap.set(method, params);
  40. this.onCallCallbackMap.set(method, callback);
  41. switch (method) {
  42. case TUIConstants.TUIConversation.SERVICE.METHOD.CREATE_CONVERSATION:
  43. this.createConversation(params);
  44. break;
  45. case TUIConstants.TUIConversation.SERVICE.METHOD.HIDE_CONVERSATION_HEADER:
  46. this.hideConversationHeader();
  47. break;
  48. }
  49. }
  50. public onGetExtension(extensionID: string) {
  51. if (extensionID === TUIConstants.TUISearch.EXTENSION.SEARCH_MORE.EXT_ID) {
  52. const list = [
  53. {
  54. weight: 100,
  55. icon: createC2CIcon,
  56. text: TUITranslateService.t('TUIConversation.发起单聊'),
  57. data: {
  58. name: CONV_CREATE_TYPE.TYPEC2C,
  59. },
  60. listener: {
  61. onClicked: this.createConversation.bind(this),
  62. },
  63. },
  64. {
  65. weight: 100,
  66. icon: createGroupIcon,
  67. text: TUITranslateService.t('TUIConversation.发起群聊'),
  68. data: {
  69. name: CONV_CREATE_TYPE.TYPEGROUP,
  70. },
  71. listener: {
  72. onClicked: this.createConversation.bind(this),
  73. },
  74. },
  75. ];
  76. return list;
  77. }
  78. }
  79. private createConversation(item: any) {
  80. // Tell TUIContact to call the SelectFriend component to select a friend
  81. TUICore.callService({
  82. serviceName: TUIConstants.TUIContact.SERVICE.NAME,
  83. method: TUIConstants.TUIContact.SERVICE.METHOD.SELECT_FRIEND,
  84. params: {
  85. title: item.text,
  86. isRadio: item.data.name !== CONV_CREATE_TYPE.TYPEGROUP,
  87. isNeedSearch: !TUIStore.getData(StoreName.APP, 'isOfficial'),
  88. },
  89. callback: async (memberList: any[]) => {
  90. if (!memberList || memberList.length === 0) {
  91. // Return to the previous page
  92. return this.routerForward(null);
  93. }
  94. if (item.data.name === CONV_CREATE_TYPE.TYPEGROUP) {
  95. // After selecting members, if you want to create a group chat, you need to create a group
  96. this.createGroup(memberList);
  97. } else {
  98. const { userID } = memberList[0];
  99. // Generate Conversation
  100. await this.generateConversation(`C2C${userID}`);
  101. this.routerForward(`C2C${userID}`);
  102. }
  103. },
  104. });
  105. }
  106. private createGroup(memberList: any[]) {
  107. TUICore.callService({
  108. serviceName: TUIConstants.TUIGroup.SERVICE.NAME,
  109. method: TUIConstants.TUIGroup.SERVICE.METHOD.CREATE_GROUP,
  110. params: {
  111. title: TUITranslateService.t('TUIConversation.发起群聊'),
  112. memberList,
  113. },
  114. callback: async (group: any) => {
  115. let conversationID = null;
  116. if (group) {
  117. const { groupID } = group;
  118. await this.generateConversation(`GROUP${groupID}`);
  119. conversationID = `GROUP${groupID}`;
  120. }
  121. this.routerForward(conversationID);
  122. },
  123. });
  124. }
  125. private async routerForward(conversationID: string | null): Promise<void> {
  126. if (isUniFrameWork) {
  127. await TUIGlobal?.reLaunch({
  128. url: '/TUIKit/components/TUIConversation/index',
  129. });
  130. if (conversationID) {
  131. TUIGlobal?.navigateTo({
  132. url: '/TUIKit/components/TUIChat/index',
  133. });
  134. }
  135. }
  136. }
  137. private generateConversation(conversationID: string) {
  138. TUIConversationService.switchConversation(conversationID)
  139. .then(() => {
  140. if (conversationID.startsWith('GROUP')) {
  141. enableSampleTaskStatus('groupChat');
  142. }
  143. console.warn('打开会话成功');
  144. })
  145. .catch((err: any) => {
  146. console.warn('打开会话失败', err.code, err.msg);
  147. });
  148. }
  149. private hideConversationHeader = () => {
  150. TUIStore.update(StoreName.CUSTOM, 'isShowConversationHeader', false);
  151. };
  152. }