index.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <ToolbarItemContainer
  3. :iconFile="imageToolbarForShow.icon"
  4. :title="imageToolbarForShow.title"
  5. iconWidth="30px"
  6. iconHeight="30px"
  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 {
  27. TUIChatService,
  28. TUIStore,
  29. StoreName,
  30. IConversationModel,
  31. SendMessageParams,
  32. } from "@tencentcloud/chat-uikit-engine";
  33. import { TUIGlobal } from "@tencentcloud/universal-api";
  34. import { ref, computed } from "../../../../adapter-vue";
  35. import { isPC, isWeChat, isUniFrameWork } from "../../../../utils/env";
  36. import ToolbarItemContainer from "../toolbar-item-container/index.vue";
  37. import { isEnabledMessageReadReceiptGlobal } from "../../utils/utils";
  38. const props = defineProps({
  39. // 图片源, 仅uniapp版本有效, web版本仅支持从相册中选择图片
  40. // album: 从相册中选择
  41. // camera: 使用相机拍摄
  42. imageSourceType: {
  43. type: String,
  44. default: "album",
  45. },
  46. });
  47. const inputRef = ref();
  48. const currentConversation = ref<IConversationModel>();
  49. const IMAGE_TOOLBAR_SHOW_MAP = {
  50. web_album: {
  51. icon: "https://qianzhiy-applet.oss-rg-china-mainland.aliyuncs.com/h5/images/homemaking/user/tool-icon1.png",
  52. title: "图片",
  53. },
  54. uni_album: {
  55. icon:"https://qianzhiy-applet.oss-rg-china-mainland.aliyuncs.com/h5/images/homemaking/user/tool-icon1.png",
  56. title: "图片",
  57. },
  58. uni_camera: {
  59. icon: "https://qianzhiy-applet.oss-rg-china-mainland.aliyuncs.com/h5/images/homemaking/user/tool-icon2.png",
  60. title: "拍照",
  61. },
  62. };
  63. TUIStore.watch(StoreName.CONV, {
  64. currentConversation: (conversation: IConversationModel) => {
  65. currentConversation.value = conversation;
  66. },
  67. });
  68. const imageToolbarForShow = computed((): { icon: string; title: string } => {
  69. if (isUniFrameWork) {
  70. return props.imageSourceType === "camera"
  71. ? IMAGE_TOOLBAR_SHOW_MAP["uni_camera"]
  72. : IMAGE_TOOLBAR_SHOW_MAP["uni_album"];
  73. } else {
  74. return IMAGE_TOOLBAR_SHOW_MAP["web_album"];
  75. }
  76. });
  77. const onIconClick = () => {
  78. // uniapp 环境 发送图片
  79. if (isUniFrameWork) {
  80. // 增加 TUIGlobal.chooseMedia 条件限制,防御 uni 打包其他平台小程序时由于打包问题导致 isWeChat 为 true 出现运行时报错
  81. if (isWeChat && TUIGlobal?.chooseMedia) {
  82. // 微信小程序从基础库 2.21.0 开始, wx.chooseImage 停止维护,请使用 uni.chooseMedia 代替
  83. TUIGlobal?.chooseMedia({
  84. count: 1,
  85. mediaType: ["image"], // 图片
  86. sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
  87. sourceType: [props.imageSourceType], // 从相册选择或使用相机拍摄
  88. success: function (res: any) {
  89. sendImageMessage(res);
  90. },
  91. });
  92. } else {
  93. // uniapp h5/app 发送图片
  94. TUIGlobal?.chooseImage({
  95. count: 1,
  96. sourceType: [props.imageSourceType], // 从相册选择或使用相机拍摄
  97. success: function (res) {
  98. sendImageMessage(res);
  99. },
  100. });
  101. }
  102. } else {
  103. if (inputRef.value?.click) {
  104. inputRef.value.click();
  105. }
  106. }
  107. };
  108. const sendImageInWeb = (e: any) => {
  109. if (e?.target?.files?.length <= 0) {
  110. return;
  111. }
  112. sendImageMessage(e?.target);
  113. e.target.value = "";
  114. };
  115. const sendImageMessage = (files: any) => {
  116. if (!files) {
  117. return;
  118. }
  119. const options = {
  120. to:
  121. currentConversation?.value?.groupProfile?.groupID ||
  122. currentConversation?.value?.userProfile?.userID,
  123. conversationType: currentConversation?.value?.type,
  124. payload: {
  125. file: files,
  126. },
  127. needReadReceipt: isEnabledMessageReadReceiptGlobal(),
  128. } as SendMessageParams;
  129. // todo: 需要处理uniapp文件没有宽高的变形问题,需要linda看看
  130. TUIChatService.sendImageMessage(options);
  131. };
  132. </script>
  133. <style lang="scss" scoped>
  134. @import "../../../../assets/styles/common";
  135. </style>