index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <ToolbarItemContainer
  3. :iconFile="handleIcon()"
  4. :title="handleTitle()"
  5. :needDialog="false"
  6. :iconWidth="isUniFrameWork ? '32px' : '21px'"
  7. :iconHeight="
  8. isUniFrameWork
  9. ? props.videoSourceType === 'album'
  10. ? '20px'
  11. : '25px'
  12. : '18px'
  13. "
  14. @onIconClick="onIconClick"
  15. >
  16. <div :class="['video-upload', !isPC && 'video-upload-h5']">
  17. <input
  18. ref="inputRef"
  19. title="视频"
  20. type="file"
  21. data-type="video"
  22. accept="video/*"
  23. @change="sendVideoInWeb"
  24. >
  25. </div>
  26. </ToolbarItemContainer>
  27. </template>
  28. <script lang="ts" setup>
  29. import {
  30. TUIChatService,
  31. TUIStore,
  32. StoreName,
  33. IConversationModel,
  34. SendMessageParams,
  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. const props = defineProps({
  45. // 视频源, 仅uniapp版本有效, web版本仅支持从文件中选择视频
  46. // album: 从文件中选择
  47. // camera: 使用相机拍摄
  48. videoSourceType: {
  49. type: String,
  50. default: 'album',
  51. },
  52. });
  53. const inputRef = ref();
  54. const currentConversation = ref<IConversationModel>();
  55. TUIStore.watch(StoreName.CONV, {
  56. currentConversation: (conversation: IConversationModel) => {
  57. currentConversation.value = conversation;
  58. },
  59. });
  60. const handleIcon = (): string => {
  61. if (isUniFrameWork) {
  62. switch (props.videoSourceType) {
  63. case 'album':
  64. return videoUniIcon;
  65. case 'camera':
  66. return cameraUniIcon;
  67. default:
  68. return videoUniIcon;
  69. }
  70. } else {
  71. return videoIcon;
  72. }
  73. };
  74. const handleTitle = (): string => {
  75. if (isUniFrameWork && props.videoSourceType === 'camera') {
  76. return '录制';
  77. } else {
  78. return '视频';
  79. }
  80. };
  81. const onIconClick = () => {
  82. // uniapp环境发送视频
  83. if (isUniFrameWork) {
  84. // 增加 TUIGlobal.chooseMedia 条件限制,防御 uni 打包其他平台小程序时由于打包问题导致 isWeChat 为 true 出现运行时报错
  85. if (isWeChat && TUIGlobal?.chooseMedia) {
  86. // 微信小程序从基础库 2.21.0 开始, wx.chooseVideo 停止维护,请使用 uni.chooseMedia 代替
  87. TUIGlobal?.chooseMedia({
  88. mediaType: ['video'], // 视频
  89. count: 1,
  90. sourceType: [props.videoSourceType], // album 从相册选视频,camera 使用相机拍摄
  91. maxDuration: 60, // 设置最长时间60s
  92. success: function (res: any) {
  93. sendVideoMessage(res);
  94. },
  95. });
  96. } else {
  97. // uniapp h5/app 发送图片
  98. TUIGlobal?.chooseVideo({
  99. count: 1,
  100. sourceType: [props.videoSourceType], // 从相册选择或使用相机拍摄
  101. success: function (res: any) {
  102. sendVideoMessage(res);
  103. },
  104. });
  105. }
  106. } else {
  107. inputRef?.value?.click && inputRef?.value?.click();
  108. }
  109. };
  110. const sendVideoInWeb = (e: any) => {
  111. if (e?.target?.files?.length <= 0) {
  112. return;
  113. }
  114. sendVideoMessage(e?.target);
  115. e.target.value = '';
  116. };
  117. const sendVideoMessage = (file: any) => {
  118. if (!file) {
  119. return;
  120. }
  121. const options = {
  122. to:
  123. currentConversation?.value?.groupProfile?.groupID
  124. || currentConversation?.value?.userProfile?.userID,
  125. conversationType: currentConversation?.value?.type,
  126. payload: {
  127. file,
  128. },
  129. needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  130. } as SendMessageParams;
  131. TUIChatService.sendVideoMessage(options);
  132. };
  133. </script>
  134. <style lang="scss" scoped>
  135. @import "../../../../assets/styles/common";
  136. </style>