SeckillActivityForm.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle" width="65%">
  3. <Form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :isCol="true"
  7. :rules="rules"
  8. :schema="allSchemas.formSchema"
  9. >
  10. <!-- 先选择 -->
  11. <template #spuIds>
  12. <el-button @click="spuSelectRef.open()">选择商品</el-button>
  13. <SpuAndSkuList
  14. ref="spuAndSkuListRef"
  15. :rule-config="ruleConfig"
  16. :spu-list="spuList"
  17. :spu-property-list-p="spuPropertyList"
  18. />
  19. </template>
  20. </Form>
  21. <template #footer>
  22. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  23. <el-button @click="dialogVisible = false">取 消</el-button>
  24. </template>
  25. </Dialog>
  26. <SpuSelect ref="spuSelectRef" @confirm="selectSpu" />
  27. </template>
  28. <script lang="ts" setup>
  29. import { SpuAndSkuList, SpuProperty, SpuSelect } from '../../components'
  30. import { allSchemas, rules } from './seckillActivity.data'
  31. import * as SeckillActivityApi from '@/api/mall/promotion/seckill/seckillActivity'
  32. import { getPropertyList, RuleConfig } from '@/views/mall/product/spu/components'
  33. import * as ProductSpuApi from '@/api/mall/product/spu'
  34. defineOptions({ name: 'PromotionSeckillActivityForm' })
  35. const { t } = useI18n() // 国际化
  36. const message = useMessage() // 消息弹窗
  37. const dialogVisible = ref(false) // 弹窗的是否展示
  38. const dialogTitle = ref('') // 弹窗的标题
  39. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  40. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  41. const formRef = ref() // 表单 Ref
  42. const spuSelectRef = ref() // 商品和属性选择 Ref
  43. const spuAndSkuListRef = ref() // sku 秒杀配置组件Ref
  44. const ruleConfig: RuleConfig[] = [
  45. {
  46. name: 'productConfig.stock',
  47. rule: (arg) => arg > 1,
  48. message: '商品秒杀库存必须大于 1 !!!'
  49. },
  50. {
  51. name: 'productConfig.seckillPrice',
  52. rule: (arg) => arg > 0.01,
  53. message: '商品秒杀价格必须大于 0.01 !!!'
  54. }
  55. ]
  56. /** 打开弹窗 */
  57. const open = async (type: string, id?: number) => {
  58. dialogVisible.value = true
  59. dialogTitle.value = t('action.' + type)
  60. formType.value = type
  61. resetForm()
  62. // 修改时,设置数据 TODO 没测试估计有问题
  63. if (id) {
  64. formLoading.value = true
  65. try {
  66. const data = await SeckillActivityApi.getSeckillActivity(id)
  67. formRef.value.setValues(data)
  68. } finally {
  69. formLoading.value = false
  70. }
  71. }
  72. }
  73. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  74. const spuList = ref<SeckillActivityApi.SpuExtension[]>([]) // 选择的 spu
  75. const spuPropertyList = ref<SpuProperty<SeckillActivityApi.SpuExtension>[]>([])
  76. const selectSpu = (spuIds: number[]) => {
  77. formRef.value.setValues({ spuIds })
  78. getSpuDetails(spuIds)
  79. }
  80. /**
  81. * 获取 SPU 详情
  82. * TODO 获取 SPU 详情,放到各自活动表单来做,让 SpuAndSkuList 职责单一点
  83. * @param spuIds
  84. */
  85. const getSpuDetails = async (spuIds: number[]) => {
  86. const spuProperties: SpuProperty<SeckillActivityApi.SpuExtension>[] = []
  87. spuList.value = []
  88. // TODO puhui999: 考虑后端添加通过 spuIds 批量获取
  89. for (const spuId of spuIds) {
  90. // 获取 SPU 详情
  91. const res = (await ProductSpuApi.getSpu(spuId)) as SeckillActivityApi.SpuExtension
  92. if (!res) {
  93. continue
  94. }
  95. spuList.value.push(res)
  96. // 初始化每个 sku 秒杀配置
  97. res.skus?.forEach((sku) => {
  98. const config: SeckillActivityApi.SeckillProductVO = {
  99. spuId,
  100. skuId: sku.id!,
  101. stock: 0,
  102. seckillPrice: 0
  103. }
  104. sku.productConfig = config
  105. })
  106. spuProperties.push({ spuId, spuDetail: res, propertyList: getPropertyList(res) })
  107. }
  108. spuPropertyList.value = spuProperties
  109. }
  110. /** 重置表单 */
  111. const resetForm = () => {
  112. spuList.value = []
  113. spuPropertyList.value = []
  114. formRef.value.getElFormRef().resetFields()
  115. }
  116. /** 提交表单 */
  117. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  118. const submitForm = async () => {
  119. // 校验表单
  120. if (!formRef) return
  121. const valid = await formRef.value.getElFormRef().validate()
  122. if (!valid) return
  123. // 提交请求
  124. formLoading.value = true
  125. try {
  126. const data = formRef.value.formModel as SeckillActivityApi.SeckillActivityVO
  127. data.spuIds = spuList.value.map((spu) => spu.id!)
  128. data.products = spuAndSkuListRef.value.getSkuConfigs('productConfig')
  129. if (formType.value === 'create') {
  130. await SeckillActivityApi.createSeckillActivity(data)
  131. message.success(t('common.createSuccess'))
  132. } else {
  133. await SeckillActivityApi.updateSeckillActivity(data)
  134. message.success(t('common.updateSuccess'))
  135. }
  136. dialogVisible.value = false
  137. // 发送操作成功的事件
  138. emit('success')
  139. } finally {
  140. formLoading.value = false
  141. }
  142. }
  143. </script>
  144. <style lang="scss" scoped>
  145. .demo-table-expand {
  146. padding-left: 42px;
  147. :deep(.el-form-item__label) {
  148. width: 82px;
  149. font-weight: bold;
  150. color: #99a9bf;
  151. }
  152. }
  153. </style>