index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 {
  24. TUIChatService,
  25. TUIStore,
  26. StoreName,
  27. IConversationModel,
  28. SendMessageParams,
  29. } from '@tencentcloud/chat-uikit-engine';
  30. import { ref } from '../../../../adapter-vue';
  31. import ToolbarItemContainer from '../toolbar-item-container/index.vue';
  32. import fileIcon from '../../../../assets/icon/files.png';
  33. import { isPC, isUniFrameWork } from '../../../../utils/env';
  34. import { isEnabledMessageReadReceiptGlobal } from '../../utils/utils';
  35. const inputRef = ref();
  36. const currentConversation = ref<IConversationModel>();
  37. TUIStore.watch(StoreName.CONV, {
  38. currentConversation: (conversation: IConversationModel) => {
  39. currentConversation.value = conversation;
  40. },
  41. });
  42. const onIconClick = () => {
  43. if (isUniFrameWork) {
  44. // uniapp app 不支持选择文件发送
  45. return;
  46. } else {
  47. inputRef?.value?.click && inputRef?.value?.click();
  48. }
  49. };
  50. const sendFileMessage = (e: any) => {
  51. if (e?.target?.files?.length <= 0) {
  52. return;
  53. }
  54. const options = {
  55. to:
  56. currentConversation?.value?.groupProfile?.groupID
  57. || currentConversation?.value?.userProfile?.userID,
  58. conversationType: currentConversation?.value?.type,
  59. payload: {
  60. file: e?.target,
  61. },
  62. needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  63. } as SendMessageParams;
  64. TUIChatService.sendFileMessage(options);
  65. e.target.value = '';
  66. };
  67. </script>
  68. <style lang="scss" scoped>
  69. @import "../../../../assets/styles/common";
  70. </style>