SeckillConfigForm.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. const { t } = useI18n() // 国际化
  14. const message = useMessage() // 消息弹窗
  15. const dialogVisible = ref(false) // 弹窗的是否展示
  16. const dialogTitle = ref('') // 弹窗的标题
  17. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  18. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  19. const formRef = ref() // 表单 Ref
  20. /** 打开弹窗 */
  21. const open = async (type: string, id?: number) => {
  22. dialogVisible.value = true
  23. dialogTitle.value = t('action.' + type)
  24. formType.value = type
  25. // 修改时,设置数据
  26. if (id) {
  27. formLoading.value = true
  28. try {
  29. const data = await SeckillConfigApi.getSeckillConfig(id)
  30. data.sliderPicUrls = data['sliderPicUrls']?.map((item) => ({
  31. url: item
  32. }))
  33. formRef.value.setValues(data)
  34. } finally {
  35. formLoading.value = false
  36. }
  37. }
  38. }
  39. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  40. /** 提交表单 */
  41. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  42. const submitForm = async () => {
  43. // 校验表单
  44. if (!formRef) return
  45. const valid = await formRef.value.getElFormRef().validate()
  46. if (!valid) return
  47. // 提交请求
  48. formLoading.value = true
  49. try {
  50. // 处理轮播图列表
  51. const sliderPicUrls = []
  52. formRef.value.formModel.sliderPicUrls.forEach((item) => {
  53. // 如果是前端选的图
  54. typeof item === 'object' ? sliderPicUrls.push(item.url) : sliderPicUrls.push(item)
  55. })
  56. // 真正提交
  57. const data = {
  58. ...formRef.value.formModel,
  59. sliderPicUrls
  60. } as SeckillConfigApi.SeckillConfigVO
  61. if (formType.value === 'create') {
  62. await SeckillConfigApi.createSeckillConfig(data)
  63. message.success(t('common.createSuccess'))
  64. } else {
  65. await SeckillConfigApi.updateSeckillConfig(data)
  66. message.success(t('common.updateSuccess'))
  67. }
  68. dialogVisible.value = false
  69. // 发送操作成功的事件
  70. emit('success')
  71. } finally {
  72. formLoading.value = false
  73. }
  74. }
  75. </script>