UploadFileNew.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <view class="upBox">
  3. <uni-file-picker
  4. v-model="list"
  5. :limit="limit"
  6. :fileMediatype="fileMediatype"
  7. @select="picSelect"
  8. @success="picSuccess"
  9. @fail="picFail"
  10. @delete="picDelete"
  11. :mode="mode"
  12. :delIcon="delIcon"
  13. :imageStyles="imageStyles"
  14. >
  15. <slot></slot>
  16. </uni-file-picker>
  17. </view>
  18. </template>
  19. <script>
  20. export default {
  21. name: 'uploadFile',
  22. model: {
  23. prop: 'fileList',
  24. event: 'change'
  25. },
  26. props: {
  27. fileList: {
  28. type: Array,
  29. default: () => []
  30. },
  31. module: {
  32. type: String,
  33. default: ''
  34. },
  35. fileMediatype: {
  36. type: String,
  37. default: 'all'
  38. },
  39. limit: {
  40. type: String,
  41. default: ''
  42. },
  43. mode: {
  44. type: String,
  45. default: ''
  46. },
  47. imageStyles: {
  48. type: Object,
  49. default: () => ({})
  50. },
  51. delIcon: {
  52. type: Boolean,
  53. default: true
  54. }
  55. },
  56. data () {
  57. return {
  58. list: []
  59. }
  60. },
  61. watch: {},
  62. computed: {
  63. // list: {
  64. // get () {
  65. // return this.fileList.map(item => ({
  66. // extname: item.name.split('.').pop(),
  67. // url: window.location.origin + item.accessUrl,
  68. // ...item
  69. // }))
  70. // },
  71. // set (val) {
  72. // console.log(val, 'val')
  73. // this.$emit('change', val)
  74. // return val
  75. // }
  76. // }
  77. },
  78. methods: {
  79. //选择图片,上传
  80. picSelect (e) {
  81. const tempFilePaths = e.tempFilePaths
  82. console.log(tempFilePaths)
  83. uni.showLoading({
  84. title: '上传中'
  85. })
  86. uni.uploadFile({
  87. url: this.apiUrl + '/data/doc/add',
  88. filePath: tempFilePaths[0],
  89. name: 'file',
  90. formData: {
  91. module: this.module
  92. },
  93. success: res => {
  94. let info = JSON.parse(res.data)
  95. console.log(res.data, 'this.$attrs.limit=', this.$attrs.limit)
  96. uni.hideLoading()
  97. info.data.type = info.data.type?.id
  98. this.$emit(
  99. 'change',
  100. this.$attrs.limit == 1 ? [info.data] : [...this.fileList, info.data]
  101. )
  102. },
  103. fail: () => {
  104. uni.hideLoading()
  105. uni.showToast('上传失败!')
  106. }
  107. })
  108. },
  109. //上传成功
  110. picSuccess () {
  111. uni.showToast({
  112. icon: 'none',
  113. title: '上传成功',
  114. duration: 2000
  115. })
  116. },
  117. //上传失败
  118. picFail () {
  119. uni.showToast({
  120. icon: 'none',
  121. title: '上传失败,请重新上传!',
  122. duration: 2000
  123. })
  124. },
  125. //删除图片
  126. picDelete (e) {
  127. console.log(this.list)
  128. // this.$emit(
  129. // 'change',
  130. // this.$attrs.limit == 1 ? [] : [...this.fileList, info.data]
  131. // )
  132. console.log(e)
  133. }
  134. }
  135. }
  136. </script>
  137. <style lang="scss" scoped>
  138. .upBox {
  139. width: 100%;
  140. display: block;
  141. }
  142. </style>