upload-file.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <uv-upload
  3. :fileList="list"
  4. name="1"
  5. multiple
  6. :maxCount="10"
  7. @afterRead="afterRead"
  8. @delete="handleDeletePic"
  9. ></uv-upload>
  10. </template>
  11. <script setup>
  12. import { VUE_APP_UPLOAD_URL } from '@/config';
  13. import { ref } from 'vue';
  14. const props = defineProps(['modelValue'])
  15. console.log("--> % modelValue:\n", props.modelValue)
  16. const emit = defineEmits(['update:modelValue'])
  17. const list = ref(props.modelValue)
  18. const handleDeletePic = (event) => {
  19. list.value.splice(event.index, 1)
  20. emit('update:modelValue', list.value)
  21. }
  22. const afterRead = async (event) => {
  23. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  24. let lists = [].concat(event.file)
  25. let fileListLen = list.value.length
  26. lists.map((item) => {
  27. list.value.push({
  28. ...item,
  29. status: 'uploading',
  30. message: '上传中'
  31. })
  32. })
  33. for (let i = 0; i < lists.length; i++) {
  34. const result = await uploadFilePromise(lists[i].url)
  35. console.log("gxs --> % afterRead % result:\n", result)
  36. let item = list.value[fileListLen]
  37. list.value.splice(fileListLen, 1, Object.assign(item, {
  38. status: 'success',
  39. message: '',
  40. url: result
  41. }))
  42. fileListLen++
  43. }
  44. emit('update:modelValue', list.value)
  45. }
  46. const uploadFilePromise = (url) => {
  47. return new Promise((resolve, reject) => {
  48. let a = uni.uploadFile({
  49. url: VUE_APP_UPLOAD_URL, // 仅为示例,非真实的接口地址
  50. filePath: url,
  51. name: 'file',
  52. formData: {
  53. user: 'test'
  54. },
  55. success: (res) => {
  56. console.log("gxs --> % returnnewPromise % res:\n", res)
  57. setTimeout(() => {
  58. resolve(res.data.data)
  59. }, 10)
  60. }
  61. });
  62. })
  63. }
  64. </script>
  65. <style lang="less">
  66. .activity {
  67. &-header {
  68. display: flex;
  69. justify-content: space-between;
  70. align-items: center;
  71. margin-bottom: 20rpx;
  72. &-info {
  73. display: flex;
  74. align-items: flex-end;
  75. }
  76. &-title {
  77. line-height: 45rpx;
  78. font-size: 32rpx;
  79. color: #333333;
  80. }
  81. &-subtitle {
  82. margin-left: 10rpx;
  83. line-height: 33rpx;
  84. font-size: 24rpx;
  85. color: #EE6D46;
  86. }
  87. &-more {
  88. display: flex;
  89. align-items: center;
  90. &-info {
  91. line-height: 33rpx;
  92. font-size: 24rpx;
  93. color: #999999;
  94. }
  95. .image {
  96. margin-left: 10rpx;
  97. display: block;
  98. width: 20rpx;
  99. height: 20rpx;
  100. }
  101. }
  102. &-body {}
  103. }
  104. }
  105. </style>