CUploadImg.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="clearfix">
  3. <a-upload :action="action" multiple :fileList="fileList" list-type="picture-card" @preview="handlePreview" @change="handleChange" :customRequest="customRequest" :remove="handleRemove" :beforeUpload="beforeUpload">
  4. <div v-if="fileList.length < maxSize">
  5. <a-icon type="upload" style="margin-left: 15px" />
  6. <div class="ant-upload-text">上传图片</div>
  7. </div>
  8. </a-upload>
  9. <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel" :width="1200">
  10. <img style="width: 100%" :src="previewImage" />
  11. </a-modal>
  12. </div>
  13. </template>
  14. <script>
  15. import { uploadFile } from '@/api/upms/file'
  16. export default {
  17. name: 'UploadImg',
  18. data() {
  19. return {
  20. previewVisible: false,
  21. previewImage: '',
  22. fileList: [],
  23. action: process.env.VUE_APP_API_BASE_URL + '/upms/files/upload',
  24. }
  25. },
  26. props: {
  27. maxSize: {
  28. type: Number,
  29. default: 1,
  30. },
  31. value: {
  32. type: Array,
  33. },
  34. },
  35. watch: {
  36. fileList: {
  37. handler(newV, oldV) {
  38. this.fileList = newV
  39. this.$emit('change', newV)
  40. },
  41. deep: true,
  42. immediate: true,
  43. },
  44. value: {
  45. deep: true,
  46. immediate: true,
  47. handler: function (newV) {
  48. console.log(newV)
  49. // 数据为空的三种情况
  50. if (newV === null || newV === '' || newV === undefined) {
  51. this.fileList = []
  52. this.isWatch = true
  53. return
  54. }
  55. if (this.isWatch) {
  56. this.isWatch = false
  57. if (newV.length > 0) {
  58. this.fileList = this.BaseTool.UPLOAD.transImg(newV)
  59. } else {
  60. this.isWatch = true
  61. }
  62. }
  63. },
  64. },
  65. },
  66. methods: {
  67. handleCancel() {
  68. this.previewVisible = false
  69. },
  70. handlePreview(file) {
  71. this.previewImage = file.url || file.thumbUrl
  72. this.previewVisible = true
  73. },
  74. handleChange({ fileList }) {
  75. // this.fileList = fileList
  76. // this.$emit('catchFile', fileList)
  77. },
  78. async customRequest(data) {
  79. const formData = new FormData()
  80. formData.append('file', data.file)
  81. data.onProgress()
  82. this.$loading.show()
  83. uploadFile(formData).then((res) => {
  84. this.$loading.hide()
  85. data.onSuccess()
  86. const reg = new RegExp('[;,。!?;,]')
  87. if (reg.test(res.data.fileName)) {
  88. this.$message.error(`文件名包含非法字符`)
  89. } else {
  90. this.fileList.push({
  91. uid: '-1',
  92. name: res.data.name,
  93. status: 'done',
  94. url: this.BaseTool.Constant.FILE_URL + res.data.url,
  95. })
  96. }
  97. this.$emit('catchFile', this.fileList)
  98. })
  99. },
  100. handleRemove(file) {
  101. const index = this.fileList.indexOf(file)
  102. const newFileList = this.fileList.slice()
  103. newFileList.splice(index, 1)
  104. this.fileList = newFileList
  105. this.$emit('catchFile', this.fileList)
  106. return true
  107. },
  108. beforeUpload(file, fileList) {
  109. const reg = /\.(jpg|png|zip|png|jpg|gif|jpeg|webp)(\?.*)?$/
  110. // if (this.fileType === 1) {
  111. // reg = /\.(jpg|png|gif)(\?.*)?$/
  112. // }
  113. return new Promise((resolve, reject) => {
  114. if (reg.test(file.name.toLowerCase())) {
  115. resolve()
  116. } else {
  117. this.$message.error(`请上传正确的图片格式`)
  118. reject(new Error('请上传正确的图片格式'))
  119. }
  120. })
  121. },
  122. },
  123. }
  124. </script>
  125. <style>
  126. /* you can make up upload button and sample style by using stylesheets */
  127. .ant-upload-select-picture-card i {
  128. font-size: 32px;
  129. color: #999;
  130. }
  131. .ant-upload-select-picture-card .ant-upload-text {
  132. margin-top: 8px;
  133. color: #666;
  134. }
  135. .ant-upload-btn {
  136. margin-top: 20px;
  137. }
  138. </style>