SingleImage.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <div class="upload-container">
  3. <el-upload :data="dataObj" :multiple="false" :show-file-list="false" :on-success="handleImageSuccess" class="image-uploader" drag action="https://httpbin.org/post">
  4. <i class="el-icon-upload" />
  5. <div class="el-upload__text">
  6. 将文件拖到此处,或<em>点击上传</em>
  7. </div>
  8. </el-upload>
  9. <div class="image-preview">
  10. <div v-show="imageUrl.length>1" class="image-preview-wrapper">
  11. <img :src="imageUrl+'?imageView2/1/w/200/h/200'">
  12. <div class="image-preview-action">
  13. <i class="el-icon-delete" @click="rmImage" />
  14. </div>
  15. </div>
  16. </div>
  17. </div>
  18. </template>
  19. <script>
  20. // import { getToken } from '@/api/qiniu'
  21. export default {
  22. name: 'SingleImageUpload',
  23. props: {
  24. value: {
  25. type: String,
  26. default: ''
  27. }
  28. },
  29. data() {
  30. return {
  31. tempUrl: '',
  32. dataObj: { token: '', key: '' }
  33. }
  34. },
  35. computed: {
  36. imageUrl() {
  37. return this.value
  38. }
  39. },
  40. methods: {
  41. rmImage() {
  42. this.emitInput('')
  43. },
  44. emitInput(val) {
  45. this.$emit('input', val)
  46. },
  47. handleImageSuccess() {
  48. this.emitInput(this.tempUrl)
  49. },
  50. beforeUpload() {
  51. const _self = this
  52. return new Promise((resolve, reject) => {
  53. // eslint-disable-next-line no-undef
  54. getToken()
  55. .then(response => {
  56. const key = response.data.qiniu_key
  57. const token = response.data.qiniu_token
  58. _self._data.dataObj.token = token
  59. _self._data.dataObj.key = key
  60. this.tempUrl = response.data.qiniu_url
  61. resolve(true)
  62. })
  63. .catch(err => {
  64. console.log(err)
  65. reject(false)
  66. })
  67. })
  68. }
  69. }
  70. }
  71. </script>
  72. <style lang="scss" scoped>
  73. @import '~@/styles/mixin.scss';
  74. .upload-container {
  75. width: 100%;
  76. position: relative;
  77. @include clearfix;
  78. .image-uploader {
  79. width: 60%;
  80. float: left;
  81. }
  82. .image-preview {
  83. width: 200px;
  84. height: 200px;
  85. position: relative;
  86. border: 1px dashed #d9d9d9;
  87. float: left;
  88. margin-left: 50px;
  89. .image-preview-wrapper {
  90. position: relative;
  91. width: 100%;
  92. height: 100%;
  93. img {
  94. width: 100%;
  95. height: 100%;
  96. }
  97. }
  98. .image-preview-action {
  99. position: absolute;
  100. width: 100%;
  101. height: 100%;
  102. left: 0;
  103. top: 0;
  104. cursor: default;
  105. text-align: center;
  106. color: #fff;
  107. opacity: 0;
  108. font-size: 20px;
  109. background-color: rgba(0, 0, 0, 0.5);
  110. transition: opacity 0.3s;
  111. cursor: pointer;
  112. text-align: center;
  113. line-height: 200px;
  114. .el-icon-delete {
  115. font-size: 36px;
  116. }
  117. }
  118. &:hover {
  119. .image-preview-action {
  120. opacity: 1;
  121. }
  122. }
  123. }
  124. }
  125. </style>