index.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <div class="basic-file">
  3. <cip-upload action="uploadUrl"
  4. style="margin-bottom: 10px"
  5. :uploadFile="uploadFile"
  6. :show-file-list="false"
  7. :file-list="fileList"
  8. :before-upload="beforeUpload"
  9. :multiple="multiple"
  10. :accept="accept"
  11. :disabled="uploadDisabled">
  12. <cip-button buttonType="upload" :disabled="uploadDisabled"></cip-button>
  13. <cip-button v-if="formwork.length>0" @click="downloadFormwork">下载模板</cip-button>
  14. <template #tip>
  15. <div class="basic-file__tip">{{limitTip}}</div>
  16. </template>
  17. </cip-upload>
  18. <template v-for="(file,index) in fileList" :key="index">
  19. <file-row :file="file"
  20. :config="securityConfig"
  21. :depend-on-values="dependOnValues"
  22. :remove-code="securityConfig.removeCode"
  23. :removable="securityConfig.removable ?? true"
  24. @after-remove="removeFile"></file-row>
  25. </template>
  26. </div>
  27. </template>
  28. <script>
  29. import CipUpload from '@cip/components/cip-upload'
  30. import CipButton from '@cip/components/cip-button'
  31. import FileRow from './file-row'
  32. import { formInputProps, fromInputEmits } from '../../form-input-props'
  33. import { useFormInput } from '@cip/components/hooks/form-input'
  34. import { uploadProps, download, fileTypeList } from './upload'
  35. import { computed } from 'vue'
  36. import CipMessage from '@cip/components/cip-message'
  37. export default {
  38. props: formInputProps,
  39. emits: [...fromInputEmits],
  40. components: { FileRow, CipUpload, CipButton },
  41. setup (props, context) {
  42. const { securityConfig } = useFormInput(props, context)
  43. const { fileList, mainMessage, countLimit, typeLimit, sizeLimit, ...uploadUtils } = uploadProps(props, context)
  44. const multiple = computed(() => {
  45. return securityConfig.value.multiple ?? true
  46. })
  47. // 限制提示语
  48. const limitTip = computed(() => {
  49. const { fileType, size } = securityConfig.value
  50. if (!!fileType && !!size) return `只能上传${fileType}文件,且不超过${size}MB`
  51. if (!!fileType && !size) return `只能上传${fileType}文件`
  52. if (!fileType && !!size) return `只能上传不超过${size}MB的文件`
  53. return ''
  54. })
  55. // 验证文件是否可以上传
  56. const beforeUpload = (file) => {
  57. const errorMessage = securityConfig.value.customLimit && securityConfig.value.customLimit(file)
  58. if (errorMessage) {
  59. CipMessage.error(errorMessage)
  60. return false
  61. }
  62. if (mainMessage(typeLimit(file), '文件上传类型超出限制') ||
  63. mainMessage(countLimit(file), '文件上传数量超出限制') ||
  64. mainMessage(sizeLimit(file), '文件上传大小超出限制')
  65. ) {
  66. return false
  67. }
  68. }
  69. // 根据数量控制新增按钮
  70. const isAppend = computed(() => {
  71. return (securityConfig.value.limit ?? Infinity) > fileList.value.length
  72. })
  73. const formwork = computed(() => {
  74. return securityConfig.value.formwork ?? []
  75. })
  76. const accept = computed(() => {
  77. if (securityConfig.value.ignoreFileType) {
  78. return '*/*'
  79. }
  80. const fileType = securityConfig.value.fileType || fileTypeList.join(',')
  81. return '.' + fileType.replace(/,\s?/g, ',.')
  82. })
  83. const downloadFormwork = (e) => {
  84. e.stopPropagation()
  85. download(formwork.value[0])
  86. }
  87. return { ...uploadUtils, accept, fileList, beforeUpload, multiple, isAppend, limitTip, formwork, downloadFormwork, securityConfig }
  88. }
  89. }
  90. </script>
  91. <style lang="less">
  92. // 不加width 100%会导致宽度坍塌
  93. .basic-file{
  94. width: 100%;
  95. &__tip{
  96. word-break: break-all;
  97. line-height: 20px;
  98. }
  99. }
  100. </style>