1ea2634be4269417a7f774a1aa5f7cfc23997df1.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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">
  18. <div v-show="imageUrl.length>1" class="image-preview-wrapper">
  19. <img :src="imageUrl+'?imageView2/1/w/200/h/200'">
  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: 'SingleImageUpload',
  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(err => {
  69. console.log(err)
  70. reject(false)
  71. })
  72. })
  73. }
  74. }
  75. }
  76. </script>
  77. <style lang="scss" scoped>
  78. @import "~@/styles/mixin.scss";
  79. .upload-container {
  80. width: 100%;
  81. position: relative;
  82. @include clearfix;
  83. .image-uploader {
  84. width: 60%;
  85. float: left;
  86. }
  87. .image-preview {
  88. width: 200px;
  89. height: 200px;
  90. position: relative;
  91. border: 1px dashed #d9d9d9;
  92. float: left;
  93. margin-left: 50px;
  94. .image-preview-wrapper {
  95. position: relative;
  96. width: 100%;
  97. height: 100%;
  98. img {
  99. width: 100%;
  100. height: 100%;
  101. }
  102. }
  103. .image-preview-action {
  104. position: absolute;
  105. width: 100%;
  106. height: 100%;
  107. left: 0;
  108. top: 0;
  109. cursor: default;
  110. text-align: center;
  111. color: #fff;
  112. opacity: 0;
  113. font-size: 20px;
  114. background-color: rgba(0, 0, 0, .5);
  115. transition: opacity .3s;
  116. cursor: pointer;
  117. text-align: center;
  118. line-height: 200px;
  119. .el-icon-delete {
  120. font-size: 36px;
  121. }
  122. }
  123. &:hover {
  124. .image-preview-action {
  125. opacity: 1;
  126. }
  127. }
  128. }
  129. }
  130. </style>