upload.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <div class="preview">
  4. <img :src="qualifications.url==''? uploadImgTemp : isPdf?'/images/all/timg.png':qualifications.url" class="previewImage" :class="{'mobile-previewImage': isMobile}"
  5. :style="imgStyle"
  6. />
  7. <input type="file" v-if="!qualifications.url" class="com-input" @change="update" accept="image/jpeg,image/jpg,image/gif,image/bmp,image/png,.pdf" />
  8. </div>
  9. <div class="hover-show" v-if="qualifications.url && !noReview">
  10. <span class="delete" title="删除" @click="deleteImg(qualifications.url)"><i class="fa fa-trash"></i></span>
  11. <a @click="showImg(qualifications.url)"><i class="fa fa-search"></i>查看</a>
  12. </div>
  13. <div id="image-box" v-if="isShow">
  14. <div class="x-floating-wrap"></div>
  15. <div class="x-floating">
  16. <div id="item-content">
  17. <div class="x-close-wrap" @click="isShow = false"><a href="javascript:void(0);">&times;</a></div>
  18. <div class="img"><img :src="qualifications.url"/></div>
  19. </div>
  20. </div>
  21. </div>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. props: ['typeData', 'url', 'NopassThree', 'noReview', 'maxSize', 'imgStyle'],
  27. data () {
  28. return {
  29. qualifications: {
  30. url: '',
  31. type: ''
  32. },
  33. isShow: false,
  34. isPdf: false
  35. }
  36. },
  37. watch: {
  38. url: function (val, oldVal) {
  39. if (val && val !== '') {
  40. this.qualifications.url = val
  41. }
  42. }
  43. },
  44. computed: {
  45. uploadImgTemp () {
  46. return this.isMobile ? '/images/mobile/openStore/upload.png' : '/images/all/upload-apply.png'
  47. }
  48. },
  49. methods: {
  50. update (e) {
  51. let file = e.target.files[0]
  52. if (!this.NopassThree) {
  53. // console.log(file.size)
  54. if (file.size > (this.maxSize || 3) * 1024 * 1024) {
  55. if (this.isMobile) {
  56. this.$emit('remindAction', `上传文件不得超过${this.maxSize || 3}M`)
  57. } else {
  58. this.$message.error(`上传文件不得超过${this.maxSize || 3}M`)
  59. }
  60. // this.baseUtils.setMessage(this, '上传文件不得超过3M', this.isMobile, false)
  61. return false
  62. }
  63. }
  64. let param = new FormData()
  65. param.append('file', file, file.name)
  66. param.append('chunk', '0')
  67. if (file.type !== 'application/pdf') {
  68. let config = {
  69. headers: {'Content-Type': 'multipart/form-data'}
  70. }
  71. this.$http.post('/api/images', param, config)
  72. .then(response => {
  73. this.isPdf = false
  74. this.qualifications.url = response.data[0].path
  75. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  76. this.$emit('uploadAction', this.qualifications)
  77. })
  78. } else {
  79. let config = {
  80. headers: {'Content-Type': file.type}
  81. }
  82. this.$http.post('/file', param, config)
  83. .then(response => {
  84. this.isPdf = true
  85. this.qualifications.url = response.data[0].path
  86. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  87. this.$emit('uploadAction', this.qualifications)
  88. }, err => {
  89. console.log(err)
  90. })
  91. }
  92. },
  93. showImg (imgUrl) {
  94. if (!this.isPdf) {
  95. this.qualifications.url = imgUrl
  96. this.isShow = true
  97. } else {
  98. window.open(imgUrl)
  99. }
  100. },
  101. deleteImg () {
  102. this.qualifications.url = ''
  103. this.qualifications.type = this.typeData
  104. this.$emit('uploadAction', this.qualifications)
  105. }
  106. }
  107. }
  108. </script>
  109. <style scoped>
  110. .mobile-previewImage {
  111. width: .5rem;
  112. height: .5rem ;
  113. display: block;
  114. margin: .3rem auto 0;
  115. }
  116. .com-input{
  117. width: 100%;
  118. height: 100%;
  119. text-align: center;
  120. position: absolute;
  121. bottom: 0;
  122. left: 0;
  123. opacity: 0;
  124. display: inline-block !important;
  125. }
  126. </style>