singleImage.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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">
  9. <div class="image-preview-wrapper" v-show="imageUrl.length>1">
  10. <img :src="imageUrl+'?imageView2/1/w/200/h/200'">
  11. <div class="image-preview-action">
  12. <i @click="rmImage" class="el-icon-delete"></i>
  13. </div>
  14. </div>
  15. </div>
  16. </div>
  17. </template>
  18. <script>
  19. // 预览效果见付费文章
  20. import { getToken } from '@/api/qiniu'
  21. export default {
  22. name: 'singleImageUpload',
  23. props: {
  24. value: String
  25. },
  26. computed: {
  27. imageUrl() {
  28. return this.value
  29. }
  30. },
  31. data() {
  32. return {
  33. tempUrl: '',
  34. dataObj: { token: '', key: '' }
  35. }
  36. },
  37. methods: {
  38. rmImage() {
  39. this.emitInput('')
  40. },
  41. emitInput(val) {
  42. this.$emit('input', val)
  43. },
  44. handleImageScucess() {
  45. this.emitInput(this.tempUrl)
  46. },
  47. beforeUpload() {
  48. const _self = this
  49. return new Promise((resolve, reject) => {
  50. getToken().then(response => {
  51. const key = response.data.qiniu_key
  52. const token = response.data.qiniu_token
  53. _self._data.dataObj.token = token
  54. _self._data.dataObj.key = key
  55. this.tempUrl = response.data.qiniu_url
  56. resolve(true)
  57. }).catch(err => {
  58. console.log(err)
  59. reject(false)
  60. })
  61. })
  62. }
  63. }
  64. }
  65. </script>
  66. <style rel="stylesheet/scss" lang="scss" scoped>
  67. @import "src/styles/mixin.scss";
  68. .upload-container {
  69. width: 100%;
  70. position: relative;
  71. @include clearfix;
  72. .image-uploader {
  73. width: 60%;
  74. float: left;
  75. }
  76. .image-preview {
  77. width: 200px;
  78. height: 200px;
  79. position: relative;
  80. border: 1px dashed #d9d9d9;
  81. float: left;
  82. margin-left: 50px;
  83. .image-preview-wrapper {
  84. position: relative;
  85. width: 100%;
  86. height: 100%;
  87. img {
  88. width: 100%;
  89. height: 100%;
  90. }
  91. }
  92. .image-preview-action {
  93. position: absolute;
  94. width: 100%;
  95. height: 100%;
  96. left: 0;
  97. top: 0;
  98. cursor: default;
  99. text-align: center;
  100. color: #fff;
  101. opacity: 0;
  102. font-size: 20px;
  103. background-color: rgba(0, 0, 0, .5);
  104. transition: opacity .3s;
  105. cursor: pointer;
  106. text-align: center;
  107. line-height: 200px;
  108. .el-icon-delete {
  109. font-size: 36px;
  110. }
  111. }
  112. &:hover {
  113. .image-preview-action {
  114. opacity: 1;
  115. }
  116. }
  117. }
  118. }
  119. </style>