index.vue 4.0 KB

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