index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <div class="upload">
  3. <a-upload :action="uploadUrl"
  4. v-model:file-list="uploadImgList"
  5. :before-upload="beforeUpload"
  6. @change="uploadAction">
  7. <a-button class="uploadBtn">上传</a-button>
  8. </a-upload>
  9. <div class="look"
  10. v-if="uploadImgList.length"
  11. @click="previewImg(uploadImgList[0])">查看附件</div>
  12. <a-modal :visible="previewVisible"
  13. :footer="null"
  14. @cancel="cancelImg">
  15. <img alt="预览附件"
  16. style="width: 100%"
  17. :src="previewImage" />
  18. </a-modal>
  19. </div>
  20. </template>
  21. <script lang="ts">
  22. import { defineComponent } from '@/common/export/table';
  23. import { handlePreviewImg, handleUplodImg } from '@/common/setup/upload';
  24. import { FileInfo, FileItem } from '@/common/setup/upload/interface';
  25. import { PropType, watchEffect } from '@vue/runtime-core';
  26. import { message } from 'ant-design-vue';
  27. export default defineComponent({
  28. name: 'upload-img',
  29. props: {
  30. visible: {
  31. type: Boolean,
  32. default: false,
  33. },
  34. imgList: {
  35. type: Array as PropType<FileItem[]>,
  36. default: [],
  37. },
  38. },
  39. setup(props, context) {
  40. // 上传附件
  41. const { uploadUrl, uploadImgList, uploadChange, getUploadUrl, getUploadResultUrl } = handleUplodImg();
  42. // 预览附件
  43. const { previewVisible, previewImage, cancelImg, previewImg } = handlePreviewImg();
  44. function uploadAction(obj: FileInfo) {
  45. uploadChange(obj).then(() => {
  46. const arr = getUploadResultUrl();
  47. context.emit('upload', arr);
  48. });
  49. }
  50. watchEffect(() => {
  51. if (props.visible) {
  52. getUploadUrl();
  53. uploadImgList.value = props.imgList;
  54. }
  55. });
  56. // 文件上传校验
  57. const beforeUpload = (file:any) => {
  58. const isLt2M = file.size / 1024 / 1024 <= 1;
  59. if (!isLt2M) {
  60. message.error(file.name + "图片大小超出1M限制,请修改后重新上传", 0.8);
  61. return isLt2M;
  62. }
  63. return isLt2M;
  64. };
  65. return {
  66. beforeUpload,
  67. uploadUrl,
  68. uploadAction,
  69. uploadChange,
  70. uploadImgList,
  71. previewVisible,
  72. previewImage,
  73. cancelImg,
  74. previewImg,
  75. };
  76. },
  77. });
  78. </script>
  79. <style lang="less">
  80. .upload {
  81. .flex;
  82. flex-direction: column;
  83. .ant-upload-list {
  84. max-width: calc(100% - 80px);
  85. }
  86. .look {
  87. white-space: nowrap;
  88. }
  89. }
  90. </style
  91. >;