singleImage3.vue 3.1 KB

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