FileMessageRender.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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" class="file_icon" alt="" srcset="" />
  9. <image v-if="!localPath" class="mask"
  10. src="@/pages_im/static/images/file_message/file_mask.png" alt="" srcset="" />
  11. <image class="mask download" v-if="currentFileIsDownloading" src="@/pages_im/static/images/file_message/file_suspend.png" alt="" srcset="" />
  12. <image v-if="!currentFileIsDownloading && !localPath" class="download"
  13. src="@/pages_im/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. getFileIcon(fileName){
  91. const fileType = getFileType(fileName);
  92. const wordType = ["doc", "docx", "docm", "dot"];
  93. const pdfType = ["pdf"];
  94. const pptType = ["pptx", "pptm", "ppt"];
  95. const excelType = ["xlsx", "xlsm", "xlsb", "xltx"];
  96. const zipType = ["zip", "rar", "tar", "gz"];
  97. if (wordType.includes(fileType)) {
  98. return word;
  99. }
  100. if (pdfType.includes(fileType)) {
  101. return pdf;
  102. }
  103. if (pptType.includes(fileType)) {
  104. return ppt;
  105. }
  106. if (excelType.includes(fileType)) {
  107. return excel;
  108. }
  109. if (zipType.includes(fileType)) {
  110. return zip;
  111. }
  112. return unknown;
  113. },
  114. checkFileIsExist({ key, path }){
  115. return new Promise((resolve) => {
  116. if (!path) {
  117. resolve("");
  118. return;
  119. }
  120. plus.io.resolveLocalFileSystemURL(
  121. path,
  122. (res) => {
  123. resolve(path);
  124. },
  125. (err) => {
  126. console.log(err);
  127. getStore().dispatch("user/deleteCacheData", key);
  128. resolve("");
  129. },
  130. );
  131. });
  132. }
  133. },
  134. };
  135. </script>
  136. <style lang="scss" scoped>
  137. .file_message_container {
  138. display: flex;
  139. flex-direction: row;
  140. align-items: center;
  141. padding: 24rpx 32rpx;
  142. background-color: #fff;
  143. justify-content: space-between;
  144. width: 380rpx;
  145. box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
  146. border-radius: 12rpx;
  147. border-width: 1px;
  148. border-style: solid;
  149. border-color: #ececec;
  150. }
  151. .file_info {
  152. display: flex;
  153. flex-direction: column;
  154. justify-content: space-between;
  155. height: 44px;
  156. margin-right: 24rpx;
  157. max-width: 280rpx;
  158. }
  159. .file_name {
  160. lines: 1;
  161. text-overflow: ellipsis;
  162. overflow: hidden;
  163. font-size: 14px;
  164. color: #333;
  165. }
  166. .file_size {
  167. color: #999;
  168. font-size: 12px;
  169. }
  170. .file_icon {
  171. width: 38px;
  172. height: 44px;
  173. }
  174. .mask {
  175. top: 0;
  176. left: 0;
  177. position: absolute;
  178. width: 38px;
  179. height: 44px;
  180. }
  181. .download {
  182. width: 44rpx;
  183. height: 44rpx;
  184. position: absolute;
  185. top: 50%;
  186. left: 50%;
  187. margin-top: -22rpx;
  188. margin-left: -22rpx;
  189. }
  190. </style>