SeckillConfigForm.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle">
  3. <Form ref="formRef" v-loading="formLoading" :rules="rules" :schema="allSchemas.formSchema" />
  4. <template #footer>
  5. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  6. <el-button @click="dialogVisible = false">取 消</el-button>
  7. </template>
  8. </Dialog>
  9. </template>
  10. <script lang="ts" name="SeckillConfigForm" setup>
  11. import * as SeckillConfigApi from '@/api/mall/promotion/seckill/seckillConfig'
  12. import { allSchemas, rules } from './seckillConfig.data'
  13. import { cloneDeep } from 'lodash-es'
  14. const { t } = useI18n() // 国际化
  15. const message = useMessage() // 消息弹窗
  16. const dialogVisible = ref(false) // 弹窗的是否展示
  17. const dialogTitle = ref('') // 弹窗的标题
  18. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  19. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  20. const formRef = ref() // 表单 Ref
  21. /** 打开弹窗 */
  22. const open = async (type: string, id?: number) => {
  23. dialogVisible.value = true
  24. dialogTitle.value = t('action.' + type)
  25. formType.value = type
  26. // 修改时,设置数据
  27. if (id) {
  28. formLoading.value = true
  29. try {
  30. const data = await SeckillConfigApi.getSeckillConfig(id)
  31. data.sliderPicUrls = data['sliderPicUrls']?.map((item) => ({
  32. url: item
  33. }))
  34. formRef.value.setValues(data)
  35. } finally {
  36. formLoading.value = false
  37. }
  38. }
  39. }
  40. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  41. /** 提交表单 */
  42. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  43. const submitForm = async () => {
  44. // 校验表单
  45. if (!formRef) return
  46. const valid = await formRef.value.getElFormRef().validate()
  47. if (!valid) return
  48. // 提交请求
  49. formLoading.value = true
  50. try {
  51. // 处理轮播图列表
  52. const data = formRef.value.formModel as SeckillConfigApi.SeckillConfigVO
  53. const cloneData = cloneDeep(data)
  54. const newSliderPicUrls = []
  55. cloneData.sliderPicUrls.forEach((item) => {
  56. // 如果是前端选的图
  57. typeof item === 'object' ? newSliderPicUrls.push(item.url) : newSliderPicUrls.push(item)
  58. })
  59. cloneData.sliderPicUrls = newSliderPicUrls
  60. if (formType.value === 'create') {
  61. await SeckillConfigApi.createSeckillConfig(cloneData)
  62. message.success(t('common.createSuccess'))
  63. } else {
  64. await SeckillConfigApi.updateSeckillConfig(cloneData)
  65. message.success(t('common.updateSuccess'))
  66. }
  67. dialogVisible.value = false
  68. // 发送操作成功的事件
  69. emit('success')
  70. } finally {
  71. formLoading.value = false
  72. }
  73. }
  74. </script>