upload.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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">
  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) {
  116. return uni.previewImage({
  117. urls: list,
  118. current: findIndex,
  119. showmenu: true,
  120. fail: () => {
  121. _self.toast('预览图片失败');
  122. },
  123. });
  124. } else if (item.isVideo) {
  125. return this.$refs.update.previewVideo(item);
  126. }
  127. uni.showActionSheet({
  128. itemList: ['预览', '下载'],
  129. success: node => {
  130. if (node.tapIndex === 0) {
  131. this.openFile(item)
  132. } else if (node.tapIndex === 1) {
  133. this.download(item);
  134. }
  135. }
  136. });
  137. },
  138. download(item) {
  139. let _self = this;
  140. uni.showLoading({
  141. title: '正在下载'
  142. })
  143. uni.downloadFile({
  144. url: config.baseUrl + '/file/filenode/' + item.id + '/type/' + item.name,
  145. success: downloadfile => {
  146. uni.hideLoading();
  147. let file = !uni.getStorageSync('downliadFile') ? [] : JSON.parse(uni.getStorageSync(
  148. 'downliadFile'));
  149. file.unshift({
  150. wxUrl: downloadfile.tempFilePath,
  151. userId: _self.$store.getters.user.userId,
  152. type: item.type,
  153. size: downloadfile.dataLength,
  154. projectId: _self.$store.getters.project.id || 0,
  155. organizationId: _self.$store.getters.project.id || 0,
  156. fileName: item.name,
  157. date: _self.$dayjs().format('YYYY-MM-DD HH:mm:ss')
  158. });
  159. uni.setStorageSync('downliadFile', JSON.stringify(file));
  160. _self.$toast('下载成功');
  161. },
  162. fail: error => {
  163. uni.hideLoading();
  164. _self.$toast('下载失败')
  165. }
  166. });
  167. },
  168. openFile(item) {
  169. let _self = this;
  170. uni.showLoading({
  171. title: '正在打开'
  172. })
  173. uni.downloadFile({
  174. url: config.baseUrl + '/file/filenode/' + item.id + '/type/' + item.name,
  175. success: downloadfile => {
  176. uni.hideLoading();
  177. uni.openDocument({
  178. filePath: downloadfile.tempFilePath,
  179. fileType: item.type,
  180. success: (res) => {},
  181. fail: (error) => {
  182. _self.$toast('打开失败')
  183. }
  184. });
  185. },
  186. fail: error => {
  187. uni.hideLoading();
  188. _self.$toast('打开失败')
  189. }
  190. });
  191. }
  192. }
  193. }
  194. </script>
  195. <style>
  196. </style>