upload.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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">
  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'],
  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. this.$message.error('上传文件不得超过3M')
  54. return false
  55. }
  56. }
  57. let param = new FormData()
  58. param.append('file', file, file.name)
  59. param.append('chunk', '0')
  60. if (file.type !== 'application/pdf') {
  61. let config = {
  62. headers: {'Content-Type': 'multipart/form-data'}
  63. }
  64. this.$http.post('/api/images', param, config)
  65. .then(response => {
  66. this.isPdf = false
  67. this.qualifications.url = response.data[0].path
  68. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  69. this.$emit('uploadAction', this.qualifications)
  70. })
  71. } else {
  72. let config = {
  73. headers: {'Content-Type': file.type}
  74. }
  75. this.$http.post('/file', param, config)
  76. .then(response => {
  77. this.isPdf = true
  78. this.qualifications.url = response.data[0].path
  79. this.qualifications.type = typeof this.typeData === 'undefined' ? '' : this.typeData
  80. this.$emit('uploadAction', this.qualifications)
  81. }, err => {
  82. console.log(err)
  83. })
  84. }
  85. },
  86. showImg (imgUrl) {
  87. if (!this.isPdf) {
  88. this.qualifications.url = imgUrl
  89. this.isShow = true
  90. } else {
  91. window.open(imgUrl)
  92. }
  93. },
  94. deleteImg () {
  95. this.qualifications.url = ''
  96. this.qualifications.type = this.typeData
  97. this.$emit('uploadAction', this.qualifications)
  98. }
  99. }
  100. }
  101. </script>
  102. <style scoped>
  103. .mobile-previewImage {
  104. width: .5rem !important;
  105. height: .5rem !important;
  106. display: block;
  107. margin: .3rem auto 0 !important;
  108. }
  109. </style>