index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <template>
  2. <view class="preview_file_container">
  3. <custom-nav-bar title="文件" />
  4. <view class="main_area">
  5. <view class="file_info">
  6. <image src="https://bjyjb-1362704775.cos.ap-chongqing.myqcloud.com/shop/image/preview_file_icon.png" mode=""></image>
  7. <text class="file_name">{{ fileEl.fileName }}</text>
  8. <text class="file_size">文件大小:{{ getSize }}</text>
  9. </view>
  10. <view class="doload_action">
  11. <u-icon
  12. v-if="downloadUrl"
  13. @click="openFile"
  14. name="share-square"
  15. label="打开"
  16. size="32"
  17. ></u-icon>
  18. <u-icon
  19. v-if="!downloading && !downloadUrl"
  20. @click="download"
  21. name="download"
  22. label="下载"
  23. size="32"
  24. >
  25. </u-icon>
  26. <u-loading-icon
  27. v-if="downloading"
  28. mode="semicircle"
  29. text="下载中"
  30. ></u-loading-icon>
  31. </view>
  32. </view>
  33. <u-toast ref="uToast"></u-toast>
  34. </view>
  35. </template>
  36. <script>
  37. import CustomNavBar from "../../../components/CustomNavBar/index.vue";
  38. import { bytesToSize } from "../../../util/imCommon";
  39. import { getPurePath } from "../../../util/common";
  40. export default {
  41. name: "",
  42. components: {
  43. CustomNavBar,
  44. },
  45. data() {
  46. return {
  47. downloading: false,
  48. msgID: "",
  49. fileEl: {},
  50. downloaded: {},
  51. downloadUrl: "",
  52. };
  53. },
  54. computed: {
  55. loginUserID() {
  56. return this.$store.getters.storeCurrentUserID;
  57. },
  58. getSize() {
  59. return bytesToSize(this.fileEl.fileSize ?? 0);
  60. },
  61. },
  62. watch: {
  63. downloaded: {
  64. async handler(newVal) {
  65. let path = newVal[this.msgID];
  66. this.downloadUrl = await this.checkFile(path);
  67. },
  68. // deep: true,
  69. immediate: true,
  70. },
  71. },
  72. onLoad(options) {
  73. const { fileEl, msgID } = options;
  74. this.fileEl = JSON.parse(options.fileEl);
  75. this.msgID = msgID;
  76. this.checkDownloaded();
  77. },
  78. methods: {
  79. checkDownloaded() {
  80. this.downloaded =
  81. uni.getStorageSync(`${this.loginUserID}_downloaded`) || {};
  82. },
  83. back() {
  84. uni.navigateBack();
  85. },
  86. download() {
  87. if (!this.fileEl.sourceUrl) {
  88. this.$u.toast("无效的链接");
  89. return;
  90. }
  91. if (this.downloading) return;
  92. this.showToast("下载中", true);
  93. this.downloading = true;
  94. uni.downloadFile({
  95. url: this.fileEl.sourceUrl,
  96. success: (res) => {
  97. if (res.statusCode === 200) {
  98. uni.saveFile({
  99. tempFilePath: res.tempFilePath, //文件的临时路径
  100. success: ({ savedFilePath }) => {
  101. const tmpData = {
  102. ...this.downloaded,
  103. };
  104. console.log(savedFilePath);
  105. tmpData[this.msgID] = savedFilePath;
  106. uni.setStorage({
  107. key: `${this.loginUserID}_downloaded`,
  108. data: {
  109. ...tmpData,
  110. },
  111. });
  112. this.downloaded = {
  113. ...tmpData,
  114. };
  115. uni.$u.toast(`文件已保存到${getPurePath(savedFilePath)}`);
  116. // 打开文件
  117. uni.openDocument({
  118. filePath: escape(savedFilePath),
  119. success: function () {},
  120. fail: function () {},
  121. complete: () => {
  122. this.$refs.uToast.hide();
  123. this.downloading = false;
  124. },
  125. });
  126. },
  127. fail: function (err) {
  128. this.showToast("保存失败");
  129. this.downloading = false;
  130. },
  131. });
  132. } else {
  133. this.showToast("下载失败");
  134. this.downloading = false;
  135. }
  136. },
  137. fail: () => {
  138. this.showToast("下载失败");
  139. this.downloading = false;
  140. },
  141. });
  142. },
  143. openFile() {
  144. uni.openDocument({
  145. filePath: this.downloadUrl,
  146. fail: function () {
  147. uni.$u.toast(
  148. `文件打开失败,请前往文件管理器打开`,
  149. );
  150. },
  151. });
  152. },
  153. showToast(message, isLoading = false) {
  154. if (this.$refs.uToast.isShow) {
  155. this.$refs.uToast.hide();
  156. }
  157. this.$refs.uToast.show({
  158. message,
  159. position: "bottom",
  160. loading: isLoading,
  161. type: isLoading ? "loading" : "default",
  162. duration: isLoading ? 6000 : 2000,
  163. });
  164. },
  165. checkFile(path) {
  166. return new Promise((resolve) => {
  167. if (!path) {
  168. resolve("");
  169. return;
  170. }
  171. plus.io.resolveLocalFileSystemURL(
  172. path,
  173. (res) => {
  174. resolve(path);
  175. },
  176. (err) => {
  177. console.log(err);
  178. const tmpArr = [...this.downloaded];
  179. delete tmpArr[this.msgID];
  180. uni.setStorage({
  181. key: `${this.loginUserID}_downloaded`,
  182. data: {
  183. ...tmpArr,
  184. },
  185. });
  186. this.downloaded = [...tmpArr];
  187. resolve("");
  188. },
  189. );
  190. });
  191. },
  192. },
  193. };
  194. </script>
  195. <style lang="scss">
  196. .preview_file_container {
  197. @include colBox(false);
  198. height: 100vh;
  199. .main_area {
  200. display: flex;
  201. flex-direction: column;
  202. justify-content: space-evenly;
  203. height: 100%;
  204. }
  205. .file_info {
  206. display: flex;
  207. flex-direction: column;
  208. align-items: center;
  209. color: #0c1c33;
  210. .file_name {
  211. @include nomalEllipsis();
  212. font-weight: 500;
  213. max-width: 80vw;
  214. }
  215. .file_size {
  216. font-size: 28rpx;
  217. margin-top: 8rpx;
  218. }
  219. image {
  220. width: 56px;
  221. height: 56px;
  222. margin-bottom: 36rpx;
  223. }
  224. }
  225. .doload_action {
  226. display: flex;
  227. justify-content: center;
  228. }
  229. }
  230. </style>