upload.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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', 'HASPDF'],
  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. mounted() {
  50. this.qualifications.url = this.url || ''
  51. },
  52. methods: {
  53. update (e) {
  54. let file = e.target.files[0]
  55. if (!this.NopassThree) {
  56. // console.log(file.size)
  57. if (file.size > (this.maxSize || 3) * 1024 * 1024) {
  58. if (this.isMobile) {
  59. this.$emit('remindAction', `上传文件不得超过${this.maxSize || 3}M`)
  60. } else {
  61. this.$message.error(`上传文件不得超过${this.maxSize || 3}M`)
  62. }
  63. // this.baseUtils.setMessage(this, '上传文件不得超过3M', this.isMobile, false)
  64. return false
  65. }
  66. }
  67. let param = new FormData()
  68. param.append('file', file, file.name)
  69. param.append('chunk', '0')
  70. if (this.HASPDF === false && file.type === 'application/pdf') {
  71. this.$emit('remindAction', `请选择有效图片进行上传`)
  72. }
  73. if (file.type !== 'application/pdf') {
  74. let config = {
  75. headers: {'Content-Type': 'multipart/form-data'}
  76. }
  77. this.$http.post('/api/images', param, config)
  78. .then(response => {
  79. this.isPdf = false
  80. this.qualifications.url = response.data[0].path
  81. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  82. this.$emit('uploadAction', this.qualifications)
  83. })
  84. } else {
  85. let config = {
  86. headers: {'Content-Type': file.type}
  87. }
  88. this.$http.post('/file', param, config)
  89. .then(response => {
  90. this.isPdf = true
  91. this.qualifications.url = response.data[0].path
  92. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  93. this.$emit('uploadAction', this.qualifications)
  94. }, err => {
  95. console.log(err)
  96. })
  97. }
  98. },
  99. showImg (imgUrl) {
  100. if (!this.isPdf) {
  101. this.qualifications.url = imgUrl
  102. this.isShow = true
  103. } else {
  104. window.open(imgUrl)
  105. }
  106. },
  107. deleteImg () {
  108. this.qualifications.url = ''
  109. this.qualifications.type = this.typeData
  110. this.$emit('uploadAction', this.qualifications)
  111. }
  112. }
  113. }
  114. </script>
  115. <style scoped>
  116. .mobile-previewImage {
  117. width: .5rem;
  118. height: .5rem ;
  119. display: block;
  120. margin: .3rem auto 0;
  121. }
  122. .com-input{
  123. width: 100%;
  124. height: 100%;
  125. text-align: center;
  126. position: absolute;
  127. bottom: 0;
  128. left: 0;
  129. opacity: 0;
  130. display: inline-block !important;
  131. }
  132. </style>