SingleImage.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="upload-container">
  3. <el-upload
  4. v-if="imageUrl.length <= 0"
  5. :data="dataObj"
  6. :multiple="false"
  7. :show-file-list="false"
  8. :headers="uploadHeaders"
  9. :on-success="handleImageSuccess"
  10. class="image-uploader"
  11. drag
  12. :action="uploadHost"
  13. :style="{ width: uploaderWidth}"
  14. >
  15. <i class="el-icon-upload" />
  16. <div class="el-upload__text">
  17. 将文件拖到此处,或<em>点击上传</em>
  18. </div>
  19. </el-upload>
  20. <div v-show="imageUrl.length>1 && showPreview" class="image-preview">
  21. <div class="image-preview-wrapper">
  22. <img :src="imageUrl">
  23. <div class="image-preview-action">
  24. <i class="el-icon-delete" @click="rmImage" />
  25. </div>
  26. </div>
  27. </div>
  28. </div>
  29. </template>
  30. <script>
  31. import store from '@/store'
  32. export default {
  33. name: 'SingleImageUpload',
  34. props: {
  35. value: {
  36. type: String,
  37. default: ''
  38. },
  39. showPreview: {
  40. type: Boolean,
  41. default: true
  42. },
  43. uploaderWidth: {
  44. type: String,
  45. default: '100%'
  46. },
  47. imageType: {
  48. type: String,
  49. default: ''
  50. }
  51. },
  52. data() {
  53. return {
  54. uploadHost: process.env.VUE_APP_BASE_API + '/api/files/upload',
  55. dataObj: { token: '', key: '' }
  56. }
  57. },
  58. computed: {
  59. imageUrl() {
  60. return this.value
  61. },
  62. uploadHeaders() {
  63. return {
  64. 'Authorization': store.getters.token
  65. }
  66. }
  67. },
  68. methods: {
  69. rmImage() {
  70. this.emitInput('')
  71. this.$emit('success', { fileUrl: '', imageType: this.imageType })
  72. },
  73. emitInput(val) {
  74. this.$emit('input', val)
  75. },
  76. handleImageSuccess(response) {
  77. this.emitInput(response.url)
  78. this.$emit('success', { fileUrl: response.url, imageType: this.imageType })
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. @import "src/styles/mixin";
  85. .upload-container {
  86. width: 100%;
  87. position: relative;
  88. @include clearfix;
  89. .image-uploader {
  90. width: 20%;
  91. float: left;
  92. }
  93. .image-preview {
  94. width: 300px;
  95. height: 200px;
  96. position: relative;
  97. border: 1px dashed #d9d9d9;
  98. float: left;
  99. margin-left: 50px;
  100. .image-preview-wrapper {
  101. position: relative;
  102. width: 100%;
  103. height: 100%;
  104. img {
  105. width: 100%;
  106. height: 100%;
  107. }
  108. }
  109. .image-preview-action {
  110. position: absolute;
  111. width: 100%;
  112. height: 100%;
  113. left: 0;
  114. top: 0;
  115. cursor: default;
  116. text-align: center;
  117. color: #fff;
  118. opacity: 0;
  119. font-size: 20px;
  120. background-color: rgba(0, 0, 0, .5);
  121. transition: opacity .3s;
  122. cursor: pointer;
  123. text-align: center;
  124. line-height: 200px;
  125. .el-icon-delete {
  126. font-size: 36px;
  127. }
  128. }
  129. &:hover {
  130. .image-preview-action {
  131. opacity: 1;
  132. }
  133. }
  134. }
  135. }
  136. </style>