upload.vue 4.4 KB

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