BasicUpload.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div>
  3. <a-button-group>
  4. <a-button type="primary" @click="openUploadModal" preIcon="ant-design:cloud-upload-outlined">
  5. {{ t('component.upload.upload') }}
  6. </a-button>
  7. <Tooltip placement="bottom" v-if="showPreview">
  8. <template #title>
  9. {{ t('component.upload.uploaded') }}
  10. <template v-if="fileListRef.length">{{ fileListRef.length }}</template>
  11. </template>
  12. <a-button @click="openPreviewModal">
  13. <Icon icon="ant-design:eye-outlined" />
  14. <template v-if="fileListRef.length && showPreviewNumber">
  15. {{ fileListRef.length }}
  16. </template>
  17. </a-button>
  18. </Tooltip>
  19. </a-button-group>
  20. <UploadModal v-bind="bindValue" @register="registerUploadModal" @change="handleChange" />
  21. <UploadPreviewModal
  22. :value="fileListRef"
  23. @register="registerPreviewModal"
  24. @list-change="handlePreviewChange"
  25. />
  26. </div>
  27. </template>
  28. <script lang="ts">
  29. import { defineComponent, ref, watch, unref, computed } from 'vue';
  30. import UploadModal from './UploadModal.vue';
  31. import UploadPreviewModal from './UploadPreviewModal.vue';
  32. import Icon from '/@/components/Icon';
  33. import { Tooltip } from 'ant-design-vue';
  34. import { useModal } from '/@/components/Modal';
  35. import { uploadContainerProps } from './props';
  36. import { omit } from 'lodash-es';
  37. import { useI18n } from '/@/hooks/web/useI18n';
  38. export default defineComponent({
  39. name: 'BasicUpload',
  40. components: { UploadModal, UploadPreviewModal, Icon, Tooltip },
  41. props: uploadContainerProps,
  42. setup(props, { emit, attrs }) {
  43. const { t } = useI18n();
  44. // 上传modal
  45. const [registerUploadModal, { openModal: openUploadModal }] = useModal();
  46. // 预览modal
  47. const [registerPreviewModal, { openModal: openPreviewModal }] = useModal();
  48. const fileListRef = ref<string[]>([]);
  49. const showPreview = computed(() => {
  50. const { emptyHidePreview } = props;
  51. if (!emptyHidePreview) return true;
  52. return emptyHidePreview ? fileListRef.value.length > 0 : true;
  53. });
  54. const bindValue = computed(() => {
  55. const value = { ...attrs, ...props };
  56. return omit(value, 'onChange');
  57. });
  58. watch(
  59. () => props.value,
  60. (value = []) => {
  61. fileListRef.value = value;
  62. },
  63. { immediate: true }
  64. );
  65. // 上传modal保存操作
  66. function handleChange(urls: string[]) {
  67. fileListRef.value = [...unref(fileListRef), ...(urls || [])];
  68. emit('change', fileListRef.value);
  69. }
  70. // 预览modal保存操作
  71. function handlePreviewChange(urls: string[]) {
  72. fileListRef.value = [...(urls || [])];
  73. emit('change', fileListRef.value);
  74. }
  75. return {
  76. registerUploadModal,
  77. openUploadModal,
  78. handleChange,
  79. handlePreviewChange,
  80. registerPreviewModal,
  81. openPreviewModal,
  82. fileListRef,
  83. showPreview,
  84. bindValue,
  85. t,
  86. };
  87. },
  88. });
  89. </script>