SeckillActivityForm.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 #spuId>
  12. <el-button @click="spuAndSkuSelectForm.open('秒杀商品选择')">添加商品</el-button>
  13. <SpuAndSkuList ref="spuAndSkuListRef" :spu-list="spuList" />
  14. </template>
  15. </Form>
  16. <template #footer>
  17. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  18. <el-button @click="dialogVisible = false">取 消</el-button>
  19. </template>
  20. </Dialog>
  21. <SpuAndSkuSelectForm ref="spuAndSkuSelectForm" @confirm="selectSpu" />
  22. </template>
  23. <script lang="ts" name="PromotionSeckillActivityForm" setup>
  24. import { SpuAndSkuList, SpuAndSkuSelectForm } from './components'
  25. import { allSchemas, rules } from './seckillActivity.data'
  26. import { Spu } from '@/api/mall/product/spu'
  27. import * as SeckillActivityApi from '@/api/mall/promotion/seckill/seckillActivity'
  28. const { t } = useI18n() // 国际化
  29. const message = useMessage() // 消息弹窗
  30. const dialogVisible = ref(false) // 弹窗的是否展示
  31. const dialogTitle = ref('') // 弹窗的标题
  32. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  33. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  34. const formRef = ref() // 表单 Ref
  35. const spuAndSkuSelectForm = ref() // 商品和属性选择 Ref
  36. const spuAndSkuListRef = ref() // sku 秒杀配置组件Ref
  37. /** 打开弹窗 */
  38. const open = async (type: string, id?: number) => {
  39. dialogVisible.value = true
  40. dialogTitle.value = t('action.' + type)
  41. formType.value = type
  42. // 修改时,设置数据 TODO 没测试估计有问题
  43. if (id) {
  44. formLoading.value = true
  45. try {
  46. const data = await SeckillActivityApi.getSeckillActivity(id)
  47. formRef.value.setValues(data)
  48. } finally {
  49. formLoading.value = false
  50. }
  51. }
  52. }
  53. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  54. const spuList = ref<Spu[]>([]) // 选择的 spu
  55. const selectSpu = (val: Spu) => {
  56. formRef.value.setValues({ spuId: val.id })
  57. spuList.value = [val]
  58. }
  59. /** 提交表单 */
  60. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  61. const submitForm = async () => {
  62. // 校验表单
  63. if (!formRef) return
  64. const valid = await formRef.value.getElFormRef().validate()
  65. if (!valid) return
  66. // 提交请求
  67. formLoading.value = true
  68. try {
  69. const data = formRef.value.formModel as SeckillActivityApi.SeckillActivityVO
  70. data.spuIds = spuList.value.map((spu) => spu.id!)
  71. data.products = spuAndSkuListRef.value.getSkuConfigs()
  72. if (formType.value === 'create') {
  73. await SeckillActivityApi.createSeckillActivity(data)
  74. message.success(t('common.createSuccess'))
  75. } else {
  76. await SeckillActivityApi.updateSeckillActivity(data)
  77. message.success(t('common.updateSuccess'))
  78. }
  79. dialogVisible.value = false
  80. // 发送操作成功的事件
  81. emit('success')
  82. } finally {
  83. formLoading.value = false
  84. }
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. .demo-table-expand {
  89. padding-left: 42px;
  90. :deep(.el-form-item__label) {
  91. width: 82px;
  92. font-weight: bold;
  93. color: #99a9bf;
  94. }
  95. }
  96. </style>