ccdc2a9c98ab334b381e3e87313ef252d6499653.svn-base 3.5 KB

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