fileList.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view class="" style="padding: 20rpx 0;">
  3. <view v-for='(item,index) in fileList' :key="index"
  4. class="jnpf-file-item u-type-primary u-flex u-line-1 u-m-t-10" @tap='downLoad(item)'>
  5. <view class="jnpf-file-item-txt u-line-1">{{item.name}}</view>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. const imgTypeList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  11. import {
  12. getDownloadUrl
  13. } from '@/api/common'
  14. import jnpf from '@/utils/jnpf'
  15. export default {
  16. props: {
  17. fileList: {
  18. type: Array,
  19. default: () => []
  20. }
  21. },
  22. data() {
  23. return {
  24. }
  25. },
  26. computed: {
  27. baseURL() {
  28. return this.define.baseURL
  29. }
  30. },
  31. methods: {
  32. downLoad(item) {
  33. if (item.fileExtension && imgTypeList.includes(item.fileExtension)) return this.previewImage(item)
  34. // #ifdef MP
  35. this.previewFile(item)
  36. // #endif
  37. // #ifndef MP
  38. getDownloadUrl('annex', item.fileId).then(res => {
  39. const fileUrl = this.baseURL + res.data.url + '&name=' + item.name;
  40. // #ifdef H5
  41. window.location.href = fileUrl;
  42. // #endif
  43. // #ifdef APP-PLUS
  44. this.downloadFile(res.data.url);
  45. // #endif
  46. })
  47. // #endif
  48. },
  49. previewFile(item) {
  50. let fileTypes = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx']
  51. let url = item.url
  52. let fileType = url.split('.')[1]
  53. if (fileTypes.includes(fileType)) {
  54. uni.downloadFile({
  55. url: this.baseURL + url,
  56. success: (res) => {
  57. var filePath = res.tempFilePath;
  58. uni.openDocument({
  59. filePath: encodeURI(filePath),
  60. showMenu: true,
  61. fileType: fileType,
  62. success: (res) => {
  63. console.log('打开文档成功');
  64. },
  65. fail(err) {
  66. console.log('小程序', err);
  67. }
  68. });
  69. }
  70. });
  71. } else {
  72. this.$u.toast(
  73. '该文件类型无法打开'
  74. )
  75. }
  76. },
  77. previewImage(item) {
  78. if (!item.url) return
  79. const url = jnpf.getAuthImgUrl(item.url)
  80. uni.previewImage({
  81. urls: [url],
  82. current: url,
  83. success: () => {},
  84. fail: () => {
  85. uni.showToast({
  86. title: '预览图片失败',
  87. icon: 'none'
  88. });
  89. }
  90. });
  91. },
  92. downloadFile(url) {
  93. uni.downloadFile({
  94. url: this.baseURL + url,
  95. success: res => {
  96. if (res.statusCode === 200) {
  97. uni.saveFile({
  98. tempFilePath: res.tempFilePath,
  99. success: red => {
  100. uni.showToast({
  101. icon: 'none',
  102. mask: true,
  103. title: '文件已保存:' + red.savedFilePath, //保存路径
  104. duration: 3000,
  105. });
  106. setTimeout(() => {
  107. uni.openDocument({
  108. filePath: red.savedFilePath,
  109. success: ress => {},
  110. fail(err) {}
  111. });
  112. }, 500)
  113. }
  114. });
  115. }
  116. }
  117. });
  118. },
  119. }
  120. }
  121. </script>
  122. <style>
  123. </style>