ImageUpload.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <script setup lang="ts">
  2. import { MessagePlugin, type UploadProps } from 'tdesign-vue-next'
  3. import { ref, watch } from 'vue'
  4. import { useAppStore } from '@/stores/app'
  5. const imageViewerProps = ref<UploadProps['imageViewerProps']>({
  6. closeOnEscKeydown: false,
  7. });
  8. const sizeLimit = ref<UploadProps['sizeLimit']>({
  9. size: 2,
  10. unit: 'MB',
  11. });
  12. const file1 = ref<UploadProps['value']>([]);
  13. const disabled = ref<boolean>(false);
  14. const autoUpload = ref<boolean>(true);
  15. const showImageFileName = ref<boolean>(true);
  16. const uploadAllFilesInOneRequest = ref<boolean>(false);
  17. const handleFail: UploadProps['onFail'] = ({ file }) => {
  18. MessagePlugin.error(`文件 ${file.name} 上传失败`);
  19. };
  20. const handleSuccess: UploadProps['onSuccess'] = ({ file }) => {
  21. if(file?.url){
  22. console.log(file1.value,'file1')
  23. emits('on-success',file.url)
  24. }
  25. }
  26. const appStore = useAppStore()
  27. const uploadHeaders = ref<UploadProps['headers']>({
  28. Authorization: `Bearer ${appStore.token}`
  29. })
  30. const props = defineProps({
  31. imageUrl:{
  32. type:Object,
  33. default:() => {
  34. return { url: '' }
  35. }
  36. }
  37. })
  38. const emits = defineEmits(['on-success'])
  39. watch(() => props.imageUrl,(newVal) => {
  40. if(newVal){
  41. if(newVal.url !== ''){
  42. file1.value = [{ ...newVal }]
  43. }else{
  44. file1.value = []
  45. }
  46. }
  47. },{
  48. deep:true,
  49. immediate:true
  50. })
  51. </script>
  52. <template>
  53. <div>
  54. <t-upload
  55. ref="uploadRef1"
  56. v-model="file1"
  57. :image-viewer-props="imageViewerProps"
  58. :size-limit="sizeLimit"
  59. action="/api/files/upload"
  60. :headers="uploadHeaders"
  61. theme="image"
  62. tips="图片上传限制为2M"
  63. accept="image/*"
  64. :disabled="disabled"
  65. :auto-upload="autoUpload"
  66. :show-image-file-name="showImageFileName"
  67. :upload-all-files-in-one-request="uploadAllFilesInOneRequest"
  68. :locale="{
  69. triggerUploadText: {
  70. image: '请选择图片',
  71. },
  72. }"
  73. @fail="handleFail"
  74. @success="handleSuccess"
  75. >
  76. <!-- custom UI -->
  77. <!-- <template #fileListDisplay="{ files }">
  78. <div>{{ JSON.stringify(files) }}</div>
  79. </template> -->
  80. </t-upload>
  81. </div>
  82. </template>
  83. <style scoped>
  84. </style>