UploadPreviewModal.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <template>
  2. <BasicModal
  3. width="800px"
  4. :title="t('component.upload.preview')"
  5. wrapClassName="upload-preview-modal"
  6. v-bind="$attrs"
  7. @register="register"
  8. :showOkBtn="false"
  9. >
  10. <FileList :dataSource="fileListRef" :columns="columns" :actionColumn="actionColumn" />
  11. </BasicModal>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent, watch, ref } from 'vue';
  15. // import { BasicTable, useTable } from '/@/components/Table';
  16. import FileList from './FileList.vue';
  17. import { BasicModal, useModalInner } from '/@/components/Modal';
  18. import { previewProps } from './props';
  19. import { PreviewFileItem } from './typing';
  20. import { downloadByUrl } from '/@/utils/file/download';
  21. import { createPreviewColumns, createPreviewActionColumn } from './data';
  22. import { useI18n } from '/@/hooks/web/useI18n';
  23. import { isArray } from '/@/utils/is';
  24. export default defineComponent({
  25. components: { BasicModal, FileList },
  26. props: previewProps,
  27. emits: ['list-change', 'register', 'delete'],
  28. setup(props, { emit }) {
  29. const [register, { closeModal }] = useModalInner();
  30. const { t } = useI18n();
  31. const fileListRef = ref<PreviewFileItem[]>([]);
  32. watch(
  33. () => props.value,
  34. (value) => {
  35. if (!isArray(value)) value = [];
  36. fileListRef.value = value
  37. .filter((item) => !!item)
  38. .map((item) => {
  39. return {
  40. url: item,
  41. type: item.split('.').pop() || '',
  42. name: item.split('/').pop() || '',
  43. };
  44. });
  45. },
  46. { immediate: true }
  47. );
  48. // 删除
  49. function handleRemove(record: PreviewFileItem) {
  50. const index = fileListRef.value.findIndex((item) => item.url === record.url);
  51. if (index !== -1) {
  52. const removed = fileListRef.value.splice(index, 1);
  53. emit('delete', removed[0].url);
  54. emit(
  55. 'list-change',
  56. fileListRef.value.map((item) => item.url)
  57. );
  58. }
  59. }
  60. // // 预览
  61. // function handlePreview(record: PreviewFileItem) {
  62. // const { url = '' } = record;
  63. // createImgPreview({
  64. // imageList: [url],
  65. // });
  66. // }
  67. // 下载
  68. function handleDownload(record: PreviewFileItem) {
  69. const { url = '' } = record;
  70. downloadByUrl({ url });
  71. }
  72. return {
  73. t,
  74. register,
  75. closeModal,
  76. fileListRef,
  77. columns: createPreviewColumns() as any[],
  78. actionColumn: createPreviewActionColumn({ handleRemove, handleDownload }) as any,
  79. };
  80. },
  81. });
  82. </script>
  83. <style lang="less">
  84. @ventSpace: zxm;
  85. .upload-preview-modal {
  86. .@{ventSpace}-upload-list {
  87. display: none;
  88. }
  89. .@{ventSpace}-table-wrapper .@{ventSpace}-spin-nested-loading {
  90. padding: 0;
  91. }
  92. }
  93. </style>