SpuAndSkuList.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <el-table :data="spuData" :default-expand-all="true">
  3. <el-table-column type="expand" width="30">
  4. <template #default="{ row }">
  5. <SkuList
  6. ref="skuListRef"
  7. :is-activity-component="true"
  8. :prop-form-data="spuPropertyList.find((item) => item.spuId === row.id)?.spuDetail"
  9. :property-list="spuPropertyList.find((item) => item.spuId === row.id)?.propertyList"
  10. :rule-config="ruleConfig"
  11. >
  12. <template #extension>
  13. <slot></slot>
  14. </template>
  15. </SkuList>
  16. </template>
  17. </el-table-column>
  18. <el-table-column key="id" align="center" label="商品编号" prop="id" />
  19. <el-table-column label="商品图" min-width="80">
  20. <template #default="{ row }">
  21. <el-image :src="row.picUrl" class="w-30px h-30px" @click="imagePreview(row.picUrl)" />
  22. </template>
  23. </el-table-column>
  24. <el-table-column :show-overflow-tooltip="true" label="商品名称" min-width="300" prop="name" />
  25. <el-table-column align="center" label="商品售价" min-width="90" prop="price">
  26. <template #default="{ row }">
  27. {{ formatToFraction(row.price) }}
  28. </template>
  29. </el-table-column>
  30. <el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
  31. <el-table-column align="center" label="库存" min-width="90" prop="stock" />
  32. </el-table>
  33. </template>
  34. <script generic="T extends Spu" lang="ts" setup>
  35. import { formatToFraction } from '@/utils'
  36. import { createImageViewer } from '@/components/ImageViewer'
  37. import { Spu } from '@/api/mall/product/spu'
  38. import { RuleConfig, SkuList } from '@/views/mall/product/spu/components'
  39. import { SpuProperty } from '@/views/mall/promotion/components/index'
  40. defineOptions({ name: 'PromotionSpuAndSkuList' })
  41. const props = defineProps<{
  42. spuList: T[] // TODO 为了方便兼容后续可能有需要展示多个 spu 的情况暂时保持,如果后续都是只操作一个 spu 的话则可更改为接受一个 spu 或保持
  43. ruleConfig: RuleConfig[]
  44. spuPropertyListP: SpuProperty<T>[]
  45. }>()
  46. const spuData = ref<Spu[]>([]) // spu 详情数据列表
  47. const skuListRef = ref() // 商品属性列表Ref
  48. const spuPropertyList = ref<SpuProperty<T>[]>([]) // spuId 对应的 sku 的属性列表
  49. /**
  50. * 获取所有 sku 活动配置
  51. * @param extendedAttribute 在 sku 上扩展的属性,例:秒杀活动 sku 扩展属性 productConfig 请参考 seckillActivity.ts
  52. */
  53. const getSkuConfigs = (extendedAttribute: string) => {
  54. skuListRef.value.validateSku()
  55. const seckillProducts = []
  56. spuPropertyList.value.forEach((item) => {
  57. item.spuDetail.skus.forEach((sku) => {
  58. seckillProducts.push(sku[extendedAttribute])
  59. })
  60. })
  61. return seckillProducts
  62. }
  63. // 暴露出给表单提交时使用
  64. defineExpose({ getSkuConfigs })
  65. /** 商品图预览 */
  66. const imagePreview = (imgUrl: string) => {
  67. createImageViewer({
  68. zIndex: 99999999,
  69. urlList: [imgUrl]
  70. })
  71. }
  72. /**
  73. * 将传进来的值赋值给 skuList
  74. */
  75. watch(
  76. () => props.spuList,
  77. (data) => {
  78. if (!data) return
  79. spuData.value = data as Spu[]
  80. },
  81. {
  82. deep: true,
  83. immediate: true
  84. }
  85. )
  86. /**
  87. * 将传进来的值赋值给 skuList
  88. */
  89. watch(
  90. () => props.spuPropertyListP,
  91. (data) => {
  92. if (!data) return
  93. spuPropertyList.value = data as SpuProperty<T>[]
  94. },
  95. {
  96. deep: true,
  97. immediate: true
  98. }
  99. )
  100. </script>