Uploader.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 {
  30. ElMessage,
  31. ElNotification,
  32. } from "_element-plus@1.1.0-beta.24@element-plus";
  33. import store from "../store/index";
  34. export default defineComponent({
  35. props: {
  36. uploaderId: {
  37. type: String,
  38. default: "uploader",
  39. },
  40. limit: {
  41. type: Number,
  42. default: 100,
  43. },
  44. params: Object,
  45. actionUrl: {
  46. type: String,
  47. default: store.state.uploadUrl,
  48. },
  49. disabled: {
  50. type: Boolean,
  51. default: false,
  52. },
  53. fileList: Array,
  54. uploadText: {
  55. type: String,
  56. default: "拖拽或点击上传",
  57. },
  58. },
  59. emits: ["onPreview", "onSendFileList"],
  60. setup(props, { emit }) {
  61. let dialogVisible = ref(false);
  62. let dialogImageUrl = ref("");
  63. function preview(file) {
  64. dialogVisible.value = true;
  65. dialogImageUrl.value = file.url;
  66. }
  67. function remove(file, list) {
  68. emit("onSendFileList", list);
  69. }
  70. function uploadSuccess(res, file, list) {
  71. emit("onSendFileList", list);
  72. }
  73. function onExceed(files, fileList) {
  74. ElMessage({
  75. message: `超出文件数量限制 (最大数量:${props.limit})`,
  76. type: "warning",
  77. });
  78. }
  79. watch(
  80. () => props.disabled,
  81. (a, b) => {
  82. changeDragVisable(!a);
  83. }
  84. );
  85. function changeDragVisable(boo) {
  86. let d = document.getElementById(props.uploaderId);
  87. d.childNodes[1].style.display = boo ? "inline-block" : "none";
  88. return;
  89. let a = document.getElementsByClassName("el-upload-dragger");
  90. let b = document.getElementsByClassName("el-upload--picture-card");
  91. for (let i of a) {
  92. i.style.display = boo ? "inline-block" : "none";
  93. }
  94. for (let i of b) {
  95. i.style.display = boo ? "inline-block" : "none";
  96. }
  97. }
  98. onMounted(() => {
  99. changeDragVisable(!props.disabled);
  100. });
  101. return {
  102. preview,
  103. remove,
  104. uploadSuccess,
  105. dialogVisible,
  106. dialogImageUrl,
  107. preview,
  108. onExceed,
  109. };
  110. },
  111. });
  112. </script>
  113. <style scoped>
  114. .upload-plus-icon {
  115. height: 15%;
  116. color: rgb(139, 147, 156);
  117. line-height: 100px;
  118. font-size: 40px;
  119. font-weight: 200;
  120. }
  121. .upload-text {
  122. height: 25%;
  123. color: rgb(139, 147, 156);
  124. }
  125. .dn {
  126. display: none;
  127. }
  128. :deep().el-upload-dragger {
  129. width: 100%;
  130. height: 148px !important;
  131. }
  132. </style>