singleImage2.vue 2.6KB

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