server.ts 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import TUICore, { TUIConstants } from '@tencentcloud/tui-core';
  2. import {
  3. IMessageModel,
  4. TUIStore,
  5. StoreName,
  6. TUIChatService,
  7. } from '@tencentcloud/chat-uikit-engine';
  8. import TUIChatConfig from './config';
  9. export default class TUIChatServer {
  10. public currentConversationID: string = '';
  11. public currentMessageList: IMessageModel[] = [];
  12. constructor() {
  13. // register service
  14. TUICore.registerService(TUIConstants.TUIChat.SERVICE.NAME, this);
  15. // watch current conversationID
  16. TUIStore.watch(StoreName.CONV, {
  17. currentConversationID: (id: string) => {
  18. this.currentConversationID = id;
  19. },
  20. });
  21. // watch current conversation message list
  22. TUIStore.watch(StoreName.CHAT, {
  23. messageList: (list: IMessageModel[]) => {
  24. this.currentMessageList = list;
  25. },
  26. });
  27. }
  28. public onCall(method: string, params: any, callback: any): any {
  29. let message;
  30. switch (method) {
  31. case TUIConstants.TUIChat.SERVICE.METHOD.UPDATE_MESSAGE_LIST:
  32. message = params.message;
  33. // Two screen-up situations
  34. // 1. If the call message conversationID is currentConversation,
  35. // You need to use UPDATE_MESSAGE_LIST to update the messageList of TUIStore in the engine to display it on the screen
  36. // (because you cannot get the messages you sent at this time)
  37. // 2. If the call message conversationID is not currentConversation,
  38. // The next time you switch to the conversation where the call message is located, getMessageList can get all the call messages you sent
  39. // No need to process here
  40. if (message?.conversationID === this.currentConversationID) {
  41. const messageList = [...this.currentMessageList, message];
  42. TUIStore.update(StoreName.CHAT, 'messageList', messageList);
  43. }
  44. break;
  45. case TUIConstants.TUIChat.SERVICE.METHOD.SEND_CUSTOM_MESSAGE:
  46. TUIChatService.sendCustomMessage(params).then((res: any) => {
  47. callback && callback(res);
  48. });
  49. break;
  50. case TUIConstants.TUIChat.SERVICE.METHOD.SEND_TEXT_MESSAGE:
  51. TUIChatService.sendTextMessage(params).then((res: any) => {
  52. callback && callback(res);
  53. });
  54. break;
  55. case TUIConstants.TUIChat.SERVICE.METHOD.SET_CHAT_TYPE:
  56. TUIChatConfig.setChatType(params?.chatType);
  57. break;
  58. case TUIConstants.TUIChat.SERVICE.METHOD.CLOSE_MESSAGE_POP_MENU:
  59. TUIStore.update(StoreName.CUSTOM, 'isShowMessagePopMenu', false);
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. }