index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <ToolbarItemContainer
  3. :iconFile="fileIcon"
  4. title="文件"
  5. :iconWidth="isUniFrameWork ? '32px' : '21px'"
  6. :iconHeight="isUniFrameWork ? '25px' : '18px'"
  7. :needDialog="false"
  8. @onIconClick="onIconClick"
  9. >
  10. <div :class="['file-upload', !isPC && 'file-upload-h5']">
  11. <input
  12. ref="inputRef"
  13. title="文件"
  14. type="file"
  15. data-type="file"
  16. accept="*"
  17. @change="sendFileMessage"
  18. >
  19. </div>
  20. </ToolbarItemContainer>
  21. </template>
  22. <script lang="ts" setup>
  23. import TUIChatEngine, {
  24. TUIChatService,
  25. TUIStore,
  26. StoreName,
  27. IConversationModel,
  28. SendMessageParams,
  29. SendMessageOptions,
  30. } from '@tencentcloud/chat-uikit-engine';
  31. import { ref } from '../../../../adapter-vue';
  32. import ToolbarItemContainer from '../toolbar-item-container/index.vue';
  33. import fileIcon from '../../../../assets/icon/files.png';
  34. import { isPC, isUniFrameWork } from '../../../../utils/env';
  35. import { isEnabledMessageReadReceiptGlobal } from '../../utils/utils';
  36. import OfflinePushInfoManager, { IOfflinePushInfoCreateParams } from '../../offlinePushInfoManager/index';
  37. const inputRef = ref();
  38. const currentConversation = ref<IConversationModel>();
  39. TUIStore.watch(StoreName.CONV, {
  40. currentConversation: (conversation: IConversationModel) => {
  41. currentConversation.value = conversation;
  42. },
  43. });
  44. const onIconClick = () => {
  45. if (isUniFrameWork) {
  46. return;
  47. } else {
  48. inputRef?.value?.click && inputRef?.value?.click();
  49. }
  50. };
  51. const sendFileMessage = (e: any) => {
  52. if (e?.target?.files?.length <= 0) {
  53. return;
  54. }
  55. const options = {
  56. to:
  57. currentConversation?.value?.groupProfile?.groupID
  58. || currentConversation?.value?.userProfile?.userID,
  59. conversationType: currentConversation?.value?.type,
  60. payload: {
  61. file: e?.target,
  62. },
  63. needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  64. } as SendMessageParams;
  65. const offlinePushInfoCreateParams: IOfflinePushInfoCreateParams = {
  66. conversation: currentConversation.value,
  67. payload: options.payload,
  68. messageType: TUIChatEngine.TYPES.MSG_FILE,
  69. };
  70. const sendMessageOptions: SendMessageOptions = {
  71. offlinePushInfo: OfflinePushInfoManager.create(offlinePushInfoCreateParams),
  72. };
  73. TUIChatService.sendFileMessage(options, sendMessageOptions);
  74. e.target.value = '';
  75. };
  76. </script>
  77. <style lang="scss" scoped>
  78. @import "../../../../assets/styles/common";
  79. </style>