FileMessageRender.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <view class="file_message_container" @click="openFile">
  3. <view class="file_info">
  4. <text class="file_name">{{ message.fileElem.fileName }}</text>
  5. <text class="file_size">{{ getSizeStr }}</text>
  6. </view>
  7. <view style="position: relative">
  8. <image :src="getIcon" alt="" srcset="" />
  9. <image v-if="!localPath" class="mask" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/file_message/file_mask.png" alt="" srcset="" />
  10. <image class="mask download" v-if="currentFileIsDownloading" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/file_message/file_suspend.png" alt="" srcset="" />
  11. <image v-if="!currentFileIsDownloading && !localPath" class="download" src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/file_message/file_download.png" alt="" srcset="" />
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import { mapGetters } from 'vuex';
  17. import { bytesToSize } from '../../../../../util/imCommon';
  18. import { getFileIcon, checkFileIsExist } from '../../../../../util/common';
  19. export default {
  20. name: '',
  21. props: {
  22. message: Object
  23. },
  24. data() {
  25. return {
  26. localPath: ''
  27. };
  28. },
  29. computed: {
  30. ...mapGetters(['storeCacheMap']),
  31. getSizeStr() {
  32. return bytesToSize(this.message.fileElem.fileSize);
  33. },
  34. getIcon() {
  35. return getFileIcon(this.message.fileElem.fileName);
  36. },
  37. currentFileIsDownloading() {
  38. return this.storeCacheMap[this.message.clientMsgID]?.state === 'pending';
  39. }
  40. },
  41. watch: {
  42. storeCacheMap: {
  43. async handler(newVal) {
  44. const path = newVal[this.message.clientMsgID]?.path;
  45. this.localPath = await checkFileIsExist({
  46. key: this.message.clientMsgID,
  47. path
  48. });
  49. }
  50. }
  51. },
  52. mounted() {
  53. checkFileIsExist({
  54. key: this.message.clientMsgID,
  55. path: this.storeCacheMap[this.message.clientMsgID]?.path
  56. }).then((path) => (this.localPath = path));
  57. },
  58. methods: {
  59. download() {
  60. if (!this.message.fileElem.sourceUrl) {
  61. this.$u.toast('无效的链接');
  62. return;
  63. }
  64. if (this.currentFileIsDownloading) return;
  65. this.$store.dispatch('user/addCacheTask', {
  66. key: this.message.clientMsgID,
  67. url: this.message.fileElem.sourceUrl
  68. });
  69. },
  70. openFile() {
  71. if (!this.localPath) {
  72. this.download();
  73. return;
  74. }
  75. uni.openDocument({
  76. filePath: this.localPath,
  77. fail: () => {
  78. uni.showToast({
  79. title: '文件打开失败,请前往文件管理器打开',
  80. icon: 'none'
  81. });
  82. }
  83. });
  84. }
  85. }
  86. };
  87. </script>
  88. <style lang="scss" scoped>
  89. .file_message_container {
  90. @include vCenterBox();
  91. padding: 24rpx 32rpx;
  92. background: #fff;
  93. justify-content: space-between;
  94. width: 380rpx;
  95. box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
  96. border-radius: 12rpx;
  97. border: 1px solid #ececec;
  98. .file_info {
  99. @include colBox(true);
  100. height: 44px;
  101. margin-right: 24rpx;
  102. max-width: 280rpx;
  103. .file_name {
  104. @include nomalEllipsis();
  105. }
  106. .file_size {
  107. color: #999;
  108. font-size: 24rpx;
  109. }
  110. }
  111. image {
  112. width: 38px;
  113. height: 44px;
  114. }
  115. .mask {
  116. top: 0;
  117. left: 0;
  118. position: absolute;
  119. }
  120. .download {
  121. width: 44rpx;
  122. height: 44rpx;
  123. position: absolute;
  124. top: 50%;
  125. left: 50%;
  126. transform: translate(-50%, -50%);
  127. }
  128. }
  129. </style>