index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <template>
  2. <div class="component-upload-image" :class="{ 'is-disabled': disabled }">
  3. <MaterialSingle
  4. v-if="imageLimit <= 1"
  5. :value="singleUrl"
  6. type="image"
  7. :num="1"
  8. :width="width"
  9. :height="height"
  10. :allow-upload="allowUpload"
  11. @input="handleSingleInput"
  12. />
  13. <MaterialMulti
  14. v-else
  15. :value="urlArray"
  16. type="image"
  17. :num="imageLimit"
  18. :width="width"
  19. :height="height"
  20. :allow-upload="allowUpload"
  21. @input="handleMultiInput"
  22. />
  23. </div>
  24. </template>
  25. <script>
  26. import MaterialSingle from '@/components/Material/single'
  27. import MaterialMulti from '@/components/Material/index'
  28. export default {
  29. name: 'ImageUpload',
  30. components: { MaterialSingle, MaterialMulti },
  31. props: {
  32. value: [String, Object, Array],
  33. /** 最多选取张数;兼容旧 prop num */
  34. limit: {
  35. type: Number,
  36. default: 10
  37. },
  38. num: {
  39. type: Number,
  40. default: undefined
  41. },
  42. width: {
  43. type: Number,
  44. default: 148
  45. },
  46. height: {
  47. type: Number,
  48. default: 148
  49. },
  50. disabled: {
  51. type: Boolean,
  52. default: false
  53. },
  54. /** 素材库弹窗内是否允许上传新素材 */
  55. allowUpload: {
  56. type: Boolean,
  57. default: true
  58. },
  59. // 以下保留兼容,不再使用
  60. fileSize: Number,
  61. fileType: Array,
  62. isShowTip: Boolean,
  63. type: String
  64. },
  65. computed: {
  66. imageLimit() {
  67. return this.num != null ? this.num : this.limit
  68. },
  69. singleUrl() {
  70. if (!this.value) return ''
  71. if (Array.isArray(this.value)) return this.value[0] || ''
  72. return String(this.value).split(',').filter(Boolean)[0] || ''
  73. },
  74. urlArray() {
  75. if (!this.value) return []
  76. if (Array.isArray(this.value)) return this.value.filter(Boolean)
  77. return String(this.value).split(',').filter(Boolean)
  78. }
  79. },
  80. methods: {
  81. handleSingleInput(url) {
  82. this.$emit('input', url || '')
  83. },
  84. handleMultiInput(arr) {
  85. const list = Array.isArray(arr) ? arr.filter(Boolean) : []
  86. this.$emit('input', list.join(','))
  87. }
  88. }
  89. }
  90. </script>
  91. <style scoped lang="scss">
  92. .is-disabled {
  93. pointer-events: none;
  94. opacity: 0.6;
  95. }
  96. </style>