SpuAndSkuList.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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="h-30px w-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-column v-if="spuData.length > 1 && isDelete" align="center" label="操作" min-width="90" >
  33. <template #default="scope">
  34. <el-button
  35. type="primary"
  36. link
  37. @click="deleteSpu(scope.row.id)"
  38. >
  39. 删除
  40. </el-button>
  41. </template>
  42. </el-table-column>
  43. </el-table>
  44. </template>
  45. <script generic="T extends Spu" lang="ts" setup>
  46. import { formatToFraction } from '@/utils'
  47. import { createImageViewer } from '@/components/ImageViewer'
  48. import { Spu } from '@/api/mall/product/spu'
  49. import { RuleConfig, SkuList } from '@/views/mall/product/spu/components'
  50. import { SpuProperty } from '@/views/mall/promotion/components/index'
  51. defineOptions({ name: 'PromotionSpuAndSkuList' })
  52. const message = useMessage() // 消息弹窗
  53. const props = defineProps<{
  54. spuList: T[]
  55. ruleConfig: RuleConfig[]
  56. spuPropertyListP: SpuProperty<T>[]
  57. isDelete?: boolean //spu是否可以多选
  58. }>()
  59. const spuData = ref<Spu[]>([]) // spu 详情数据列表
  60. const skuListRef = ref() // 商品属性列表Ref
  61. const spuPropertyList = ref<SpuProperty<T>[]>([]) // spuId 对应的 sku 的属性列表
  62. const expandRowKeys = ref<number[]>() // 控制展开行需要设置 row-key 属性才能使用,该属性为展开行的 keys 数组。
  63. /**
  64. * 获取所有 sku 活动配置
  65. *
  66. * @param extendedAttribute 在 sku 上扩展的属性,例:秒杀活动 sku 扩展属性 productConfig 请参考 seckillActivity.ts
  67. */
  68. const getSkuConfigs = (extendedAttribute: string) => {
  69. skuListRef.value.validateSku()
  70. const seckillProducts = []
  71. spuPropertyList.value.forEach((item) => {
  72. item.spuDetail.skus.forEach((sku) => {
  73. seckillProducts.push(sku[extendedAttribute])
  74. })
  75. })
  76. return seckillProducts
  77. }
  78. // 暴露出给表单提交时使用
  79. defineExpose({ getSkuConfigs })
  80. /** 商品图预览 */
  81. const imagePreview = (imgUrl: string) => {
  82. createImageViewer({
  83. zIndex: 99999999,
  84. urlList: [imgUrl]
  85. })
  86. }
  87. // 删除时的触发事件
  88. const emits = defineEmits<{
  89. (e: 'delete', spuId: number): void
  90. }>()
  91. /** 多选时可以删除spu **/
  92. const deleteSpu = async (spuId: number) => {
  93. await message.confirm('是否删除商品编号为' + spuId + '的数据?')
  94. let index = spuData.value.findIndex((item) => item.id == spuId)
  95. spuData.value.splice(index,1);
  96. emits('delete',spuId)
  97. }
  98. /**
  99. * 将传进来的值赋值给 skuList
  100. */
  101. watch(
  102. () => props.spuList,
  103. (data) => {
  104. if (!data) return
  105. spuData.value = data as Spu[]
  106. },
  107. {
  108. deep: true,
  109. immediate: true
  110. }
  111. )
  112. /**
  113. * 将传进来的值赋值给 skuList
  114. */
  115. watch(
  116. () => props.spuPropertyListP,
  117. (data) => {
  118. if (!data) return
  119. spuPropertyList.value = data as SpuProperty<T>[]
  120. // 解决如果之前选择的是单规格 spu 的话后面选择多规格 sku 多规格属性信息不展示的问题。解决方法:让 SkuList 组件重新渲染(行折叠会干掉包含的组件展开时会重新加载)
  121. setTimeout(() => {
  122. expandRowKeys.value = data.map((item) => item.spuId)
  123. }, 200)
  124. },
  125. {
  126. deep: true,
  127. immediate: true
  128. }
  129. )
  130. </script>