Uploader.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <template>
  2. <el-upload
  3. :id="uploaderId"
  4. drag
  5. multiple
  6. :action="actionUrl"
  7. list-type="picture-card"
  8. :on-preview="preview"
  9. :on-remove="remove"
  10. :data="params"
  11. :on-success="uploadSuccess"
  12. :file-list="fileList"
  13. :disabled="disabled"
  14. :on-exceed="onExceed"
  15. :limit="limit"
  16. >
  17. <div :class="['upload-plus-icon']">+</div>
  18. <div :class="['upload-text']">{{ uploadText }}</div>
  19. </el-upload>
  20. <el-dialog v-model="dialogVisible" title="图片预览" width="30%">
  21. <el-image
  22. :src="dialogImageUrl"
  23. style="height: 100%; width: 100%"
  24. ></el-image>
  25. </el-dialog>
  26. </template>
  27. <script>
  28. import { defineComponent, computed, ref, onMounted, watch } from "vue";
  29. import { ElMessage, ElNotification } from "element-plus";
  30. import store from "../store/index";
  31. export default defineComponent({
  32. props: {
  33. uploaderId: {
  34. type: String,
  35. default: "uploader",
  36. },
  37. limit: {
  38. type: Number,
  39. default: 100,
  40. },
  41. params: Object,
  42. actionUrl: {
  43. type: String,
  44. default: store.state.uploadUrl,
  45. },
  46. disabled: {
  47. type: Boolean,
  48. default: false,
  49. },
  50. fileList: Array,
  51. uploadText: {
  52. type: String,
  53. default: "拖拽或点击上传",
  54. },
  55. },
  56. emits: ["onPreview", "onSendFileList"],
  57. setup(props, { emit }) {
  58. let dialogVisible = ref(false);
  59. let dialogImageUrl = ref("");
  60. function preview(file) {
  61. dialogVisible.value = true;
  62. dialogImageUrl.value = file.url;
  63. }
  64. function remove(file, list) {
  65. emit("onSendFileList", list);
  66. }
  67. function uploadSuccess(res, file, list) {
  68. emit("onSendFileList", list);
  69. }
  70. function onExceed(files, fileList) {
  71. ElMessage({
  72. message: `超出文件数量限制 (最大数量:${props.limit})`,
  73. type: "warning",
  74. });
  75. }
  76. watch(
  77. () => props.disabled,
  78. (a, b) => {
  79. changeDragVisable(!a);
  80. }
  81. );
  82. function changeDragVisable(boo) {
  83. let d = document.getElementById(props.uploaderId);
  84. d.childNodes[1].style.display = boo ? "inline-block" : "none";
  85. return;
  86. let a = document.getElementsByClassName("el-upload-dragger");
  87. let b = document.getElementsByClassName("el-upload--picture-card");
  88. for (let i of a) {
  89. i.style.display = boo ? "inline-block" : "none";
  90. }
  91. for (let i of b) {
  92. i.style.display = boo ? "inline-block" : "none";
  93. }
  94. }
  95. onMounted(() => {
  96. changeDragVisable(!props.disabled);
  97. });
  98. return {
  99. preview,
  100. remove,
  101. uploadSuccess,
  102. dialogVisible,
  103. dialogImageUrl,
  104. preview,
  105. onExceed,
  106. };
  107. },
  108. });
  109. </script>
  110. <style scoped>
  111. .upload-plus-icon {
  112. height: 15%;
  113. color: rgb(139, 147, 156);
  114. line-height: 100px;
  115. font-size: 40px;
  116. font-weight: 200;
  117. }
  118. .upload-text {
  119. height: 25%;
  120. color: rgb(139, 147, 156);
  121. }
  122. .dn {
  123. display: none;
  124. }
  125. :deep().el-upload-dragger {
  126. width: 100%;
  127. height: 148px !important;
  128. }
  129. </style>