SpuAndSkuList.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <el-table :data="spuData" :expand-row-keys="expandRowKeys" row-key="id">
  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[]
  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. const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
  50. /**
  51. * 获取所有 sku 活动配置
  52. *
  53. * @param extendedAttribute 在 sku 上扩展的属性,例:秒杀活动 sku 扩展属性 productConfig 请参考 seckillActivity.ts
  54. */
  55. const getSkuConfigs = (extendedAttribute: string) => {
  56. skuListRef.value.validateSku()
  57. const seckillProducts = []
  58. spuPropertyList.value.forEach((item) => {
  59. item.spuDetail.skus.forEach((sku) => {
  60. seckillProducts.push(sku[extendedAttribute])
  61. })
  62. })
  63. return seckillProducts
  64. }
  65. // 暴露出给表单提交时使用
  66. defineExpose({ getSkuConfigs })
  67. /** 商品图预览 */
  68. const imagePreview = (imgUrl: string) => {
  69. createImageViewer({
  70. zIndex: 99999999,
  71. urlList: [imgUrl]
  72. })
  73. }
  74. /**
  75. * 将传进来的值赋值给 skuList
  76. */
  77. watch(
  78. () => props.spuList,
  79. (data) => {
  80. if (!data) return
  81. spuData.value = data as Spu[]
  82. },
  83. {
  84. deep: true,
  85. immediate: true
  86. }
  87. )
  88. /**
  89. * 将传进来的值赋值给 skuList
  90. */
  91. watch(
  92. () => props.spuPropertyListP,
  93. (data) => {
  94. if (!data) return
  95. spuPropertyList.value = data as SpuProperty<T>[]
  96. // 解决如果之前选择的是单规格 spu 的话后面选择多规格 sku 多规格属性信息不展示的问题。解决方法:让 SkuList 组件重新渲染(行折叠会干掉包含的组件展开时会重新加载)
  97. setTimeout(() => {
  98. expandRowKeys.value = data.map((item) => item.spuId)
  99. }, 200)
  100. },
  101. {
  102. deep: true,
  103. immediate: true
  104. }
  105. )
  106. </script>