FileMessageRender.vue 3.3 KB

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