server.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import TUICore, { TUIConstants } from '@tencentcloud/tui-core';
  2. import {
  3. isCustomerServiceMessage,
  4. isMessageInvisible,
  5. isMessageRating,
  6. } from './utils/index';
  7. import { IMessageModel } from './interface';
  8. export default class TUICustomerServer {
  9. static instance: TUICustomerServer;
  10. private customerServiceAccounts: Array<string>;
  11. constructor() {
  12. console.log('TUICustomerServer.init ok');
  13. TUICore.registerService(TUIConstants.TUICustomerServicePlugin.SERVICE.NAME, this);
  14. TUICore.registerExtension(TUIConstants.TUIContact.EXTENSION.CONTACT_LIST.EXT_ID, this);
  15. this.customerServiceAccounts = ['@im_agent#online_shopping_mall', '@im_agent#online_doctor'];
  16. }
  17. static getInstance(): TUICustomerServer {
  18. if (!TUICustomerServer.instance) {
  19. TUICustomerServer.instance = new TUICustomerServer();
  20. }
  21. return TUICustomerServer.instance;
  22. }
  23. // 设置 客服号
  24. public setCustomerServiceAccounts(accounts: Array<string>) {
  25. this.customerServiceAccounts = accounts;
  26. }
  27. // 获取 客服号
  28. public getCustomerServiceAccounts() {
  29. return this.customerServiceAccounts;
  30. }
  31. // 判断当前会话是不是客服会话
  32. private isCustomerConversation(conversationID: string) {
  33. const userID = (conversationID && conversationID.slice(3)) || '';
  34. return this.customerServiceAccounts.indexOf(userID) > -1;
  35. }
  36. // 判断当前消息是不是客服消息
  37. public isCustomerServicePluginMessage(message: IMessageModel) {
  38. if (!message || !this.isCustomerConversation(message.conversationID)) {
  39. return false;
  40. }
  41. return isCustomerServiceMessage(message) || isMessageRating(message) || isMessageInvisible(message);
  42. }
  43. public onGetExtension(extensionID: string) {
  44. if (extensionID === TUIConstants.TUIContact.EXTENSION.CONTACT_LIST.EXT_ID) {
  45. return [
  46. {
  47. weight: 0,
  48. icon: '',
  49. text: '客服号',
  50. data: {
  51. name: 'customer',
  52. accountList: this.customerServiceAccounts,
  53. },
  54. },
  55. ];
  56. }
  57. }
  58. public onCall(method: string, params: any) {
  59. switch (method) {
  60. case TUIConstants.TUICustomerServicePlugin.SERVICE.METHOD.ACTIVE_CONVERSATION:
  61. if (this.isCustomerConversation(params.conversationID)) {
  62. TUICore.callService({
  63. serviceName: TUIConstants.TUIChat.SERVICE.NAME,
  64. method: TUIConstants.TUIChat.SERVICE.METHOD.SET_CHAT_TYPE,
  65. params: { chatType: 'customerService' },
  66. });
  67. TUICore.callService({
  68. serviceName: TUIConstants.TUIChat.SERVICE.NAME,
  69. method: TUIConstants.TUIChat.SERVICE.METHOD.SEND_CUSTOM_MESSAGE,
  70. params: {
  71. to: params.conversationID.slice(3),
  72. conversationType: 'C2C',
  73. payload: {
  74. data: JSON.stringify({ src: '7' }),
  75. },
  76. },
  77. });
  78. }
  79. break;
  80. }
  81. }
  82. }