h-upload.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <view :style="wrapClass">
  3. <u-upload
  4. :accept="accept"
  5. :fileList="child"
  6. :maxCount="maxCount"
  7. :width="size"
  8. :height="size"
  9. :uploadIcon="uploadIcon"
  10. :uploadText="uploadText"
  11. @afterRead="afterRead"
  12. @delete="deleteClick"/>
  13. </view>
  14. </template>
  15. <script>
  16. import { uploadFile } from '@/network/module/common.api.js'
  17. export default {
  18. name:"h-upload",
  19. props: {
  20. value: {
  21. type: Array,
  22. default: () => []
  23. },
  24. accept: {
  25. type: String,
  26. default: 'image'
  27. },
  28. maxCount: {
  29. type: Number,
  30. default: 1
  31. },
  32. size: {
  33. type: String,
  34. default: '160rpx'
  35. },
  36. uploadIcon: {
  37. type: String,
  38. default: 'photo'
  39. },
  40. uploadText: {
  41. type: String,
  42. default: '上传照片'
  43. },
  44. validateField: {
  45. type: String,
  46. require: true,
  47. default: 'photoList'
  48. },
  49. display: {
  50. type: String,
  51. default: 'inline'
  52. }
  53. },
  54. model: {
  55. prop: 'value',
  56. event: 'change'
  57. },
  58. data() {
  59. return {
  60. child: this.formatFile(this.value)
  61. }
  62. },
  63. computed: {
  64. wrapClass(){
  65. if(this.display === 'block') {
  66. return {
  67. width: '100%',
  68. marginTop: '20rpx'
  69. }
  70. } else {
  71. return {
  72. width: 'auto',
  73. marginTop: '0rpx'
  74. }
  75. }
  76. }
  77. },
  78. watch: {
  79. value(newValue) {
  80. this.child = this.formatFile(newValue)
  81. }
  82. },
  83. methods: {
  84. formatFile(inputFile) {
  85. if(!inputFile || inputFile.length <= 0) {
  86. return []
  87. }
  88. return inputFile.map((item, index) => {
  89. const suffix = item.substr(item.lastIndexOf("."))
  90. return {
  91. message: '',
  92. name: `${index}${suffix}`,
  93. status: 'success',
  94. thumb: item,
  95. type: this.accept,
  96. url: item
  97. }
  98. })
  99. },
  100. // 删除
  101. deleteClick(event) {
  102. this.child.splice(event.index, 1)
  103. this.$emit('change', this.getChangeFormatChild(this.child) )
  104. if(this.validateField) {
  105. uni.$u.$parent.call(this).$refs.uForm.validateField(this.validateField)
  106. }
  107. },
  108. getChangeFormatChild(child) {
  109. if(!child || child.length <= 0) {
  110. return []
  111. }
  112. return child.map(item => {
  113. return item.url
  114. })
  115. },
  116. // 新增
  117. async afterRead(event) {
  118. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  119. let lists = [].concat(event.file)
  120. let fileListLen = this.child.length
  121. lists.map((item) => {
  122. this.child.push({
  123. ...item,
  124. status: 'uploading',
  125. message: '上传中'
  126. })
  127. })
  128. for (let i = 0; i < lists.length; i++) {
  129. const result = await uploadFile(lists[i].url)
  130. let item = this.child[fileListLen]
  131. this.child.splice(fileListLen, 1, Object.assign(item, {
  132. status: 'success',
  133. message: '',
  134. url: result.data
  135. }))
  136. fileListLen++
  137. }
  138. this.$emit('change', this.getChangeFormatChild(this.child))
  139. if(this.validateField) {
  140. const parent = uni.$u.$parent.call(this)
  141. parent.$refs.uForm.validateField(this.validateField)
  142. }
  143. }
  144. }
  145. }
  146. </script>
  147. <style>
  148. </style>