upload.vue 3.7 KB

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