5cf59fb8ee486ccc832215e5653bd2948806668b.svn-base 2.8 KB

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