index.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <div>
  3. <div style="margin-bottom: 10px">
  4. <!-- <a-button type="primary" @click="chooseFile" :disabled="coverFileList.length > 0 && isedit">选择图片</a-button> -->
  5. <a-button
  6. type="primary"
  7. @click="chooseFile"
  8. :disabled="isedit ? coverFileList.length > 0 && isedit : coverFileList.length > 0"
  9. >选择图片</a-button
  10. >
  11. <span style="margin-left: 10px">仅支持上传jpg/png格式文件,单个文件不能超过500kb</span>
  12. </div>
  13. <a-upload
  14. ref="upload"
  15. :before-upload="beforeUploadCover"
  16. :file-list="coverFileList"
  17. :remove="handleRemoveCover"
  18. :headers="headers"
  19. :action="action"
  20. :on-change="handleChangeCover"
  21. list-type="picture-card"
  22. >
  23. <div v-if="coverFileList.length < 1">
  24. <plus-outlined />
  25. <div class="ant-upload-text">上传图片</div>
  26. </div>
  27. </a-upload>
  28. <!-- 文件上传状态展示 -->
  29. <ul v-if="fileList.length">
  30. <li v-for="(file, index) in fileList" :key="index" class="file-item">
  31. <span>{{ file.name }}</span>
  32. <a-progress :percent="file.percent || 0" />
  33. <span v-if="file.status === 'done'">已完成</span>
  34. <span v-else-if="file.status === 'error'">上传失败</span>
  35. <span v-else-if="file.status === 'uploading'">上传中...</span>
  36. </li>
  37. </ul>
  38. </div>
  39. </template>
  40. <script setup>
  41. import { ref, reactive, toRefs } from 'vue'
  42. import tool from '@/utils/tool'
  43. const emit = defineEmits(['handleRemoveCover', 'handleChangeCover'])
  44. const headers = ref({
  45. token: tool.data.get('TOKEN')
  46. })
  47. const props = defineProps({
  48. coverImageId: {
  49. type: Number,
  50. required: true,
  51. default: null
  52. },
  53. isedit: {
  54. type: Boolean,
  55. required: true,
  56. default: false
  57. }
  58. })
  59. const action = ref('/api/webapp/dev/file/uploadMinioReturnId')
  60. // 封面文件列表
  61. const coverFileList = ref([])
  62. const fileList = ref([])
  63. const coverImageId = ref(null)
  64. // 上传封面前的钩子函数
  65. const beforeUploadCover = (file) => {
  66. const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png'
  67. if (!isJpgOrPng) {
  68. Modal.error({ content: '仅支持上传jpg/png格式文件!' })
  69. }
  70. const isLt500K = file.size / 1024 < 500
  71. if (!isLt500K) {
  72. Modal.error({ content: '单个文件不能超过500kb!' })
  73. }
  74. return isJpgOrPng && isLt500K
  75. }
  76. // 移除封面文件
  77. const handleRemoveCover = (file) => {
  78. const index = coverFileList.value.indexOf(file)
  79. const newFileList = coverFileList.value.slice()
  80. newFileList.splice(index, 1)
  81. coverFileList.value = newFileList
  82. // 如果移除的是当前封面文件,则清空coverImageId
  83. if (coverImageId.value === file.id) {
  84. coverImageId.value = null
  85. emit('handleRemoveCover')
  86. }
  87. }
  88. // 封面文件状态改变时的处理函数
  89. const handleChangeCover = ({ file, fileList: newFileList }) => {
  90. if (file.status === 'done') {
  91. // 上传成功,获取文件ID
  92. const fileId = file.response?.data || file.id
  93. console.log('上传成功,获取文件ID', fileId)
  94. if (fileId) {
  95. coverImageId.value = fileId
  96. emit('handleChangeCover', fileId)
  97. }
  98. }
  99. if (file.status === 'error') {
  100. // 延迟移除,确保用户能看到错误提示
  101. setTimeout(() => {
  102. coverFileList.value = coverFileList.value.filter((f) => f.uid !== file.uid)
  103. }, 1500)
  104. }
  105. coverFileList.value = newFileList
  106. }
  107. // 选择文件按钮点击事件
  108. const chooseFile = () => {
  109. document.querySelector('.ant-upload input').click()
  110. }
  111. </script>
  112. <style lang="scss"></style>