index.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <script setup>
  2. import { REQUEST } from '~/utils/request'
  3. import { resolveFileString } from '~/utils/helper'
  4. const attrs = useAttrs()
  5. const props = defineProps({
  6. // part: String,
  7. // full: String,
  8. modelValue: String,
  9. })
  10. // const emits = defineEmits(['update:part', 'update:full'])
  11. const emits = defineEmits(['update:modelValue'])
  12. const fileList = $ref(resolveFileString(props.modelValue).map(_ => ({ ..._, res: _ })))
  13. // if (props.part) {
  14. // fileList = resolveFileString(props.part)
  15. // }
  16. // if (props.full) {
  17. // fileList = resolveFileString(props.full)
  18. // }
  19. const handleAfterRead = (fileProxy) => {
  20. fileProxy.status = 'uploading'
  21. fileProxy.message = '上传中...'
  22. const { file } = fileProxy
  23. REQUEST.upload({
  24. url: '/upload/main/file',
  25. data: { filedata: file },
  26. }).then((res) => {
  27. console.log('res :>> ', res)
  28. console.log('fileList :>> ', fileList)
  29. if (res.code === '1') {
  30. // fileList.value.push(res.data)
  31. fileProxy.url = `${window.GLOBAL_CONFIG.oss}/${res.data.url}`
  32. fileProxy.res = {
  33. name: res.data.file_name,
  34. url: fileProxy.url,
  35. origin: res.data.url,
  36. }
  37. fileProxy.status = 'done'
  38. fileProxy.message = ''
  39. // emits('update:part', fileList.map((item) => item.res.name + ',' + item.res.url).join(';'))
  40. // emits('update:part', fileList.map((item) => item.res.name + ',' + item.res.url).join(';'))
  41. // emits('update:modelValue', fileList.map(item => `${item.res.name},${item.res.origin}`).join(';'))
  42. emits('update:modelValue', fileList.map(item => `${item.res.origin}`).join(';'))
  43. }
  44. else {
  45. fileProxy.status = 'failed'
  46. fileProxy.message = '上传失败'
  47. }
  48. }).catch((err) => {
  49. console.error(err)
  50. })
  51. }
  52. </script>
  53. <template>
  54. <van-uploader v-model="fileList" :after-read="handleAfterRead" :=attrs ><slot></slot></van-uploader>
  55. </template>