index.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view>
  3. <lsj-upload
  4. ref="lsjUpload"
  5. childId="upload1"
  6. :option="option"
  7. :size="size"
  8. :formats="formats"
  9. :debug="debug"
  10. :instantly="instantly"
  11. :width="width"
  12. :height="height"
  13. @uploadEnd="onUploadEnd"
  14. @onProgress="onProgress"
  15. >
  16. <view>
  17. <slot></slot>
  18. </view>
  19. </lsj-upload>
  20. </view>
  21. </template>
  22. <script>
  23. /* EaseIM */
  24. import { EMClient } from '@/EaseIM';
  25. import { MESSAGE_TYPE } from '@/EaseIM/constant';
  26. import { emMessages } from '@/EaseIM/emApis';
  27. const { sendDisplayMessages } = emMessages();
  28. const apiUrl = EMClient.apiUrl;
  29. const orgName = EMClient.orgName;
  30. const appName = EMClient.appName;
  31. const token = EMClient.token;
  32. const uploadTargetUrl = `${apiUrl}/${orgName}/${appName}/chatfiles`;
  33. export default {
  34. data() {
  35. return {
  36. option: {
  37. url: uploadTargetUrl,
  38. name: 'file',
  39. header: {
  40. Authorization: `Bearer ${token}`,
  41. },
  42. },
  43. // 选择文件后是否立即自动上传,true=选择后立即上传
  44. instantly: true,
  45. // 必传宽高且宽高应与slot宽高保持一致
  46. // width: '64px',
  47. // height: '62px',
  48. width: '30px',
  49. height: '27px',
  50. // 限制允许选择的格式,空串=不限制,默认为空
  51. formats: '',
  52. // 文件上传大小限制
  53. size: 10,
  54. // 是否打印日志
  55. debug: true,
  56. };
  57. },
  58. computed: {
  59. chattingId() {
  60. return this.$store.getters.chattingId;
  61. },
  62. chattingChatType() {
  63. return this.$store.getters.chattingChatType;
  64. },
  65. },
  66. methods: {
  67. //发送附件消息
  68. async sendFileMessage(payload) {
  69. console.log('>>>>>sendFileMessage payload', payload);
  70. const { dataObj, filename, file_length } = payload;
  71. let params = {
  72. // 消息类型。
  73. type: MESSAGE_TYPE.FILE,
  74. // 单聊、群聊和聊天室分别为 `singleChat`、`groupChat` 和 `chatRoom`。
  75. chatType: this.chattingChatType,
  76. // 消息接收方:单聊为对方用户 ID,群聊和聊天室分别为群组 ID 和聊天室 ID。
  77. to: this.chattingId,
  78. body: {
  79. // 文件 URL。
  80. url: dataObj.uri + '/' + dataObj.entities[0].uuid,
  81. // 文件类型。
  82. type: 'filetype',
  83. // 文件名称。
  84. filename: filename,
  85. // 文件大小。
  86. file_length: file_length,
  87. },
  88. ext: {},
  89. };
  90. // 发送消息。
  91. console.log('>>>>>要发送的消息params', params);
  92. try {
  93. await sendDisplayMessages(params);
  94. console.log('>>>>>文件发送成功');
  95. } catch (error) {
  96. console.log('>>>>>文件消息发送失败', error);
  97. uni.showToast({
  98. title: '消息发送失败',
  99. icon: 'none',
  100. });
  101. } finally {
  102. uni.hideLoading();
  103. this.$emit('onCloseAllShowContainer');
  104. }
  105. },
  106. onUploadEnd(res) {
  107. const dataObj = JSON.parse(res.responseText); // 接收上传文件对象
  108. const payload = {
  109. dataObj,
  110. filename: res.name,
  111. file_length: res.size,
  112. };
  113. this.sendFileMessage(payload);
  114. uni.hideLoading();
  115. },
  116. //onProgress
  117. onProgress(evt) {
  118. uni.showLoading({ title: '文件上传中,请稍后' });
  119. },
  120. },
  121. };
  122. </script>
  123. <style></style>