index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <template>
  2. <ToolbarItemContainer
  3. :iconFile="imageToolbarForShow.icon"
  4. :title="imageToolbarForShow.title"
  5. :iconWidth="isUniFrameWork ? '32px' : '21px'"
  6. :iconHeight="isUniFrameWork ? '25px' : '18px'"
  7. :needDialog="false"
  8. @onIconClick="onIconClick"
  9. >
  10. <div
  11. v-if="!isUniFrameWork"
  12. :class="['image-upload', !isPC && 'image-upload-h5']"
  13. >
  14. <input
  15. ref="inputRef"
  16. title="图片"
  17. type="file"
  18. data-type="image"
  19. accept="image/gif,image/jpeg,image/jpg,image/png,image/bmp,image/webp"
  20. @change="sendImageInWeb"
  21. >
  22. </div>
  23. </ToolbarItemContainer>
  24. </template>
  25. <script lang="ts" setup>
  26. import TUIChatEngine, {
  27. TUIChatService,
  28. TUIStore,
  29. StoreName,
  30. IConversationModel,
  31. SendMessageParams,
  32. SendMessageOptions,
  33. } from '@tencentcloud/chat-uikit-engine';
  34. import { TUIGlobal } from '@tencentcloud/universal-api';
  35. import { ref, computed } from '../../../../adapter-vue';
  36. import { isPC, isWeChat, isUniFrameWork } from '../../../../utils/env';
  37. import ToolbarItemContainer from '../toolbar-item-container/index.vue';
  38. import imageIcon from '../../../../assets/icon/image.png';
  39. import imageUniIcon from '../../../../assets/icon/image-uni.png';
  40. import cameraUniIcon from '../../../../assets/icon/camera-uni.png';
  41. import { isEnabledMessageReadReceiptGlobal } from '../../utils/utils';
  42. import OfflinePushInfoManager, { IOfflinePushInfoCreateParams } from '../../offlinePushInfoManager/index';
  43. const props = defineProps({
  44. // Image source: only valid for uni-app version, web version only supports selecting images from the album.
  45. // album: Select from album
  46. // camera: Take a photo using the camera
  47. imageSourceType: {
  48. type: String,
  49. default: 'album',
  50. },
  51. });
  52. const inputRef = ref();
  53. const currentConversation = ref<IConversationModel>();
  54. const IMAGE_TOOLBAR_SHOW_MAP = {
  55. web_album: {
  56. icon: imageIcon,
  57. title: '图片',
  58. },
  59. uni_album: {
  60. icon: imageUniIcon,
  61. title: '图片',
  62. },
  63. uni_camera: {
  64. icon: cameraUniIcon,
  65. title: '拍照',
  66. },
  67. };
  68. TUIStore.watch(StoreName.CONV, {
  69. currentConversation: (conversation: IConversationModel) => {
  70. currentConversation.value = conversation;
  71. },
  72. });
  73. const imageToolbarForShow = computed((): { icon: string; title: string } => {
  74. if (isUniFrameWork) {
  75. return props.imageSourceType === 'camera'
  76. ? IMAGE_TOOLBAR_SHOW_MAP['uni_camera']
  77. : IMAGE_TOOLBAR_SHOW_MAP['uni_album'];
  78. } else {
  79. return IMAGE_TOOLBAR_SHOW_MAP['web_album'];
  80. }
  81. });
  82. const onIconClick = () => {
  83. // uni-app send image
  84. if (isUniFrameWork) {
  85. if (isWeChat && TUIGlobal?.chooseMedia) {
  86. TUIGlobal?.chooseMedia({
  87. count: 1,
  88. mediaType: ['image'],
  89. sizeType: ['original', 'compressed'],
  90. sourceType: [props.imageSourceType], // Use camera or select from album.
  91. success: function (res: any) {
  92. sendImageMessage(res);
  93. },
  94. });
  95. } else {
  96. // uni-app H5/App send image
  97. TUIGlobal?.chooseImage({
  98. count: 1,
  99. sourceType: [props.imageSourceType], // Use camera or select from album.
  100. success: function (res) {
  101. sendImageMessage(res);
  102. },
  103. });
  104. }
  105. } else {
  106. if (inputRef.value?.click) {
  107. inputRef.value.click();
  108. }
  109. }
  110. };
  111. const sendImageInWeb = (e: any) => {
  112. if (e?.target?.files?.length <= 0) {
  113. return;
  114. }
  115. sendImageMessage(e?.target);
  116. e.target.value = '';
  117. };
  118. const sendImageMessage = (files: any) => {
  119. if (!files) {
  120. return;
  121. }
  122. const options = {
  123. to:
  124. currentConversation?.value?.groupProfile?.groupID
  125. || currentConversation?.value?.userProfile?.userID,
  126. conversationType: currentConversation?.value?.type,
  127. payload: {
  128. file: files,
  129. },
  130. needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  131. } as SendMessageParams;
  132. const offlinePushInfoCreateParams: IOfflinePushInfoCreateParams = {
  133. conversation: currentConversation.value,
  134. payload: options.payload,
  135. messageType: TUIChatEngine.TYPES.MSG_IMAGE,
  136. };
  137. const sendMessageOptions: SendMessageOptions = {
  138. offlinePushInfo: OfflinePushInfoManager.create(offlinePushInfoCreateParams),
  139. };
  140. TUIChatService.sendImageMessage(options, sendMessageOptions);
  141. };
  142. </script>
  143. <style lang="scss" scoped>
  144. @import "../../../../assets/styles/common";
  145. </style>