server.ts 2.8 KB

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