upload-files.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <view class="upload-wrapper">
  3. <uni-section :title="uploadTitle">
  4. <view class="example-body">
  5. <uni-file-picker ref="upload" limit="999" file-mediatype="all" @select="select" :auto-upload="true"
  6. :disabled="disabled"></uni-file-picker>
  7. </view>
  8. <view v-show="file_info_list.length > 0" class="file-wrapper">
  9. <view v-for="(item, index) in file_info_list" :key="index" class="file-item">
  10. <view class="file-name" @click="handlePreview(item.file_url)">
  11. <svg fill="#000000">
  12. <use xlink:href="#icon-file"></use>
  13. </svg>
  14. <text>{{ item.file_name }}</text>
  15. </view>
  16. <svg icon-class="delete" @click="deleteFile(item.file_id,index)" :fill="disabled?'#DFDFDF':'#4E5969'">
  17. <use xlink:href="#icon-deletefile"></use>
  18. </svg>
  19. </view>
  20. </view>
  21. </uni-section>
  22. </view>
  23. </template>
  24. <script>
  25. import {
  26. fileUpload,
  27. GetFileStoreInfo
  28. } from '@/api/api.js';
  29. const Base64 = require('js-base64').Base64;
  30. import {
  31. getConfig
  32. } from '@/utils/auth';
  33. export default {
  34. name: 'upload-files',
  35. props: {
  36. fileIdList: {
  37. type: Array,
  38. default: () => [],
  39. },
  40. filleNumber: {
  41. type: Number,
  42. default: 1,
  43. },
  44. fileTypeName: {
  45. type: String,
  46. default: '文件',
  47. },
  48. uploadType: {
  49. type: String,
  50. default: '*',
  51. },
  52. uploadTitle: {
  53. type: String,
  54. default: '',
  55. },
  56. disabled: {
  57. type: Boolean,
  58. default: false,
  59. },
  60. },
  61. data() {
  62. return {
  63. file_info_list: [],
  64. };
  65. },
  66. watch: {
  67. fileIdList: {
  68. handler(idlst) {
  69. this.file_info_list = [];
  70. if (!idlst || idlst.length === 0) return;
  71. var that = this;
  72. idlst.forEach(p => {
  73. GetFileStoreInfo({
  74. file_id: p
  75. }).then((res) => {
  76. that.file_info_list.push(res);
  77. });
  78. })
  79. },
  80. immediate: true,
  81. },
  82. },
  83. methods: {
  84. select(e) {
  85. var files = e.tempFiles.map(p => {
  86. p.filename = p.name;
  87. //限制文件大小p.size
  88. return p;
  89. })
  90. this.getFileInfo(files);
  91. },
  92. //获取文件
  93. async getFileInfo(files) {
  94. var that = this;
  95. await fileUpload('Mid', files).then((res) => {
  96. if (res.status === 1) {
  97. res.file_info_list.forEach(p => {
  98. that.file_info_list.push(p);
  99. this.$emit('upload', p.file_id);
  100. })
  101. }
  102. });
  103. let list_box = document.getElementsByClassName('uni-file-picker__lists');
  104. if (list_box && list_box.length > 0)
  105. list_box[0].style.display = "none";
  106. },
  107. //预览文件
  108. handlePreview(url) {
  109. uni.navigateTo({
  110. url: '/pages/common/WebViewPreview?path=' + `${Base64.encode(url)}`
  111. })
  112. },
  113. //删除文件
  114. deleteFile(id, i) {
  115. if (this.disabled) return;
  116. var that = this;
  117. uni.showModal({
  118. title: '提示',
  119. content: '是否删除当前文件?',
  120. success: function(res) {
  121. if (res.confirm) {
  122. that.$emit('deleteFile', id);
  123. that.file_info_list.splice(i, 1);
  124. }
  125. }
  126. });
  127. },
  128. }
  129. };
  130. </script>
  131. <style lang="scss" scoped>
  132. .upload-wrapper {
  133. margin-top: 32rpx;
  134. .file-wrapper {
  135. display: flex;
  136. flex-direction: column;
  137. .file-item {
  138. display: flex;
  139. align-items: center;
  140. margin-top: 16rpx;
  141. }
  142. .file-name {
  143. display: flex;
  144. align-items: center;
  145. width: 360px;
  146. padding: 24rpx 24rpx;
  147. margin-right: 12px;
  148. cursor: pointer;
  149. background-color: $uni-bg-color-grey;
  150. border-radius: 2px;
  151. svg {
  152. flex-shrink: 0;
  153. margin-right: 12px;
  154. }
  155. }
  156. svg {
  157. width: 32rpx;
  158. height: 32rpx;
  159. color: #000000;
  160. }
  161. }
  162. }
  163. /deep/ .uni-section {
  164. .uni-section-header {
  165. padding: 0;
  166. }
  167. .files-button {
  168. uni-button[type=primary] {
  169. background-color: $uni-color-main !important;
  170. border-radius: 0;
  171. padding: 10rpx 32rpx;
  172. }
  173. }
  174. }
  175. </style>