upload.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <view class="upload-index">
  3. <uv-upload ref="update" :accept="accept" :fileList="fileList" name="1" multiple
  4. :maxCount="type === 'insert' ? 5 : list.length" :deletable="type === 'insert'" @afterRead="afterRead"
  5. @delete="deletePic" @clickPreview="clickFile" width="80" height="80">
  6. </uv-upload>
  7. </view>
  8. </template>
  9. <script>
  10. import config from "@/config";
  11. import {
  12. insertDownloadFile
  13. } from '@/request/api/common.js'
  14. export default {
  15. name: "preview",
  16. props: {
  17. list: {
  18. type: Array,
  19. default: () => {
  20. return []
  21. }
  22. },
  23. type: {
  24. type: String,
  25. default: 'preview'
  26. },
  27. accept: {
  28. type: String,
  29. default: 'image'
  30. }
  31. },
  32. data() {
  33. return {
  34. fileList: []
  35. };
  36. },
  37. created() {
  38. this.fileList = this.list.map(node => {
  39. if (node.type) {
  40. node['isImage'] = this.image(node.type.toLowerCase());
  41. } else {
  42. let name = node.name.split('.');
  43. node['isImage'] = this.image(name[name.length - 1].toLowerCase());
  44. }
  45. return node;
  46. });
  47. },
  48. methods: {
  49. image(value) {
  50. return 'jpeg|jpg|gif|png|svg|webp|jfif|bmp|dpg'.indexOf(value) > -1;
  51. },
  52. // 删除图片
  53. deletePic(event) {
  54. uni.showModal({
  55. title: '有极提示',
  56. content: '是否删除该附件',
  57. success: res => {
  58. if (res.confirm) {
  59. this.fileList.splice(event.index, 1)
  60. }
  61. }
  62. });
  63. },
  64. // 新增图片
  65. async afterRead(event) {
  66. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  67. let lists = [].concat(event.file)
  68. let fileListLen = this.fileList.length;
  69. lists.map((item) => {
  70. this.fileList.push({
  71. ...item,
  72. status: 'uploading',
  73. message: '上传中'
  74. })
  75. })
  76. for (let i = 0; i < lists.length; i++) {
  77. const result = await this.uploadFilePromise(lists[i].url)
  78. let item = this.fileList[fileListLen]
  79. this.fileList.splice(fileListLen, 1, Object.assign(item, {
  80. status: 'success',
  81. message: '',
  82. url: result.node.url,
  83. id: result.id,
  84. name: result.name
  85. }))
  86. fileListLen++
  87. }
  88. },
  89. uploadFilePromise(url) {
  90. return new Promise((resolve, reject) => {
  91. let a = uni.uploadFile({
  92. url: config.baseUrl + '/file/filenode/-1', // 仅为示例,非真实的接口地址
  93. filePath: url,
  94. name: 'uploadFile',
  95. success: (res) => {
  96. setTimeout(() => {
  97. resolve(JSON.parse(res.data).data)
  98. }, 1000)
  99. }
  100. });
  101. })
  102. },
  103. getFile() {
  104. return this.fileList.map(res => {
  105. return {
  106. id: res.id,
  107. name: res.name,
  108. url: res.url,
  109. type: res.type
  110. }
  111. })
  112. },
  113. clickFile(item, list, findIndex) {
  114. let _self = this;
  115. if (item.isImage || item.isVideo) return;
  116. uni.showActionSheet({
  117. itemList: ['预览', '下载'],
  118. success: node => {
  119. if (node.tapIndex === 0) {
  120. this.openFile(item)
  121. } else if (node.tapIndex === 1) {
  122. this.download(item);
  123. }
  124. }
  125. });
  126. },
  127. download(item) {
  128. let _self = this;
  129. uni.showLoading({
  130. title: '正在下载'
  131. })
  132. uni.downloadFile({
  133. url: config.baseUrl + '/file/filenode/' + item.id + '/type/' + item.name,
  134. success: downloadfile => {
  135. uni.hideLoading();
  136. let file = !uni.getStorageSync('downliadFile') ? [] : JSON.parse(uni.getStorageSync(
  137. 'downliadFile'));
  138. file.unshift({
  139. wxUrl: downloadfile.tempFilePath,
  140. userId: _self.$store.getters.user.userId,
  141. type: item.type,
  142. size: downloadfile.dataLength,
  143. projectId: _self.$store.getters.project.id || 0,
  144. organizationId: _self.$store.getters.project.id || 0,
  145. fileName: item.name,
  146. date: _self.$uv.timeFormat(new Date(),'yyyy-mm-dd hh:MM:ss')
  147. });
  148. uni.setStorageSync('downliadFile', JSON.stringify(file));
  149. _self.$toast('下载成功');
  150. },
  151. fail: error => {
  152. uni.hideLoading();
  153. _self.$toast('下载失败')
  154. }
  155. });
  156. },
  157. openFile(item) {
  158. let _self = this;
  159. uni.showLoading({
  160. title: '正在打开'
  161. })
  162. uni.downloadFile({
  163. url: config.baseUrl + '/file/filenode/' + item.id + '/type/' + item.name,
  164. success: downloadfile => {
  165. uni.hideLoading();
  166. uni.openDocument({
  167. filePath: downloadfile.tempFilePath,
  168. fileType: item.type,
  169. success: (res) => {},
  170. fail: (error) => {
  171. _self.$toast('打开失败')
  172. }
  173. });
  174. },
  175. fail: error => {
  176. uni.hideLoading();
  177. _self.$toast('打开失败')
  178. }
  179. });
  180. }
  181. }
  182. }
  183. </script>
  184. <style>
  185. </style>