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. // 两种上屏情况
  34. // 1. 如果 call 消息 conversationID 为 currentConversation,
  35. // 需要借助 UPDATE_MESSAGE_LIST 更新 engine 中 TUIStore 的 messageList 进行上屏
  36. // (因为此时无法获得自己发送的消息)
  37. // 2. 如果 call 消息 conversationID 不是 currentConversation,
  38. // 下次切换到 call 消息所在会话时, getMessageList 可以获得 所有自己发送的 call 消息
  39. // 无需此处处理
  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. }