SpuShowcase.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <div class="flex flex-wrap items-center gap-8px">
  3. <div v-for="(spu, index) in productSpus" :key="spu.id" class="select-box spu-pic">
  4. <el-tooltip :content="spu.name">
  5. <div class="relative h-full w-full">
  6. <el-image :src="spu.picUrl" class="h-full w-full" />
  7. <Icon
  8. v-show="!disabled"
  9. class="del-icon"
  10. icon="ep:circle-close-filled"
  11. @click="handleRemoveSpu(index)"
  12. />
  13. </div>
  14. </el-tooltip>
  15. </div>
  16. <el-tooltip content="选择商品" v-if="canAdd">
  17. <div class="select-box" @click="openSpuTableSelect">
  18. <Icon icon="ep:plus" />
  19. </div>
  20. </el-tooltip>
  21. </div>
  22. <!-- 商品选择对话框(表格形式) -->
  23. <SpuTableSelect ref="spuTableSelectRef" :multiple="limit != 1" @change="handleSpuSelected" />
  24. </template>
  25. <script lang="ts" setup>
  26. import * as ProductSpuApi from '@/api/mall/product/spu'
  27. import SpuTableSelect from '@/views/mall/product/spu/components/SpuTableSelect.vue'
  28. import { propTypes } from '@/utils/propTypes'
  29. import { oneOfType } from 'vue-types'
  30. import { isArray } from "@/utils/is";
  31. // 商品橱窗,一般用于与商品建立关系时使用
  32. // 提供功能:展示商品列表、添加商品、移除商品
  33. defineOptions({ name: 'SpuShowcase' })
  34. const props = defineProps({
  35. modelValue: oneOfType<number | Array<number>>([Number, Array]).isRequired,
  36. // 限制数量:默认不限制
  37. limit: propTypes.number.def(Number.MAX_VALUE),
  38. disabled: propTypes.bool.def(false)
  39. })
  40. // 计算是否可以添加
  41. const canAdd = computed(() => {
  42. // 情况一:禁用时不可以添加
  43. if(props.disabled) return false
  44. // 情况二:未指定限制数量时,可以添加
  45. if(!props.limit) return true
  46. // 情况三:检查已添加数量是否小于限制数量
  47. return productSpus.value.length < props.limit
  48. })
  49. // 商品列表
  50. const productSpus = ref<ProductSpuApi.Spu[]>([])
  51. watch(
  52. () => props.modelValue,
  53. async () => {
  54. const ids = isArray(props.modelValue)
  55. // 情况一:多选
  56. ? props.modelValue
  57. // 情况二:单选
  58. : props.modelValue ? [props.modelValue]: []
  59. // 不需要返显
  60. if(ids.length === 0) {
  61. productSpus.value = []
  62. return
  63. }
  64. // 只有商品发生变化之后,才去查询商品
  65. if (
  66. productSpus.value.length === 0 ||
  67. productSpus.value.some((spu) => !ids.includes(spu.id!))
  68. ) {
  69. productSpus.value = await ProductSpuApi.getSpuDetailList(ids)
  70. }
  71. },
  72. { immediate: true }
  73. )
  74. /** 商品表格选择对话框 */
  75. const spuTableSelectRef = ref()
  76. // 打开对话框
  77. const openSpuTableSelect = () => {
  78. spuTableSelectRef.value.open(productSpus.value)
  79. }
  80. /**
  81. * 选择商品后触发
  82. * @param spus 选中的商品列表
  83. */
  84. const handleSpuSelected = (spus: ProductSpuApi.Spu | ProductSpuApi.Spu[]) => {
  85. productSpus.value = isArray(spus) ? spus : [spus]
  86. emitSpuChange()
  87. }
  88. /**
  89. * 删除商品
  90. * @param index 商品索引
  91. */
  92. const handleRemoveSpu = (index: number) => {
  93. productSpus.value.splice(index, 1)
  94. emitSpuChange()
  95. }
  96. const emit = defineEmits(['update:modelValue', 'change'])
  97. const emitSpuChange = () => {
  98. if(props.limit === 1) {
  99. const spu = productSpus.value.length > 0 ? productSpus.value[0] : null
  100. emit('update:modelValue', spu?.id || 0)
  101. emit('change', spu)
  102. } else {
  103. emit('update:modelValue', productSpus.value.map((spu) => spu.id))
  104. emit('change', productSpus.value)
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .select-box {
  110. display: flex;
  111. width: 60px;
  112. height: 60px;
  113. border: 1px dashed var(--el-border-color-darker);
  114. border-radius: 8px;
  115. align-items: center;
  116. justify-content: center;
  117. }
  118. .spu-pic {
  119. position: relative;
  120. }
  121. .del-icon {
  122. position: absolute;
  123. top: -10px;
  124. right: -10px;
  125. z-index: 1;
  126. width: 20px !important;
  127. height: 20px !important;
  128. }
  129. </style>