DiyTemplateForm.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <template>
  2. <Dialog v-model="dialogVisible" :title="dialogTitle">
  3. <el-form
  4. ref="formRef"
  5. v-loading="formLoading"
  6. :model="formData"
  7. :rules="formRules"
  8. label-width="100px"
  9. >
  10. <el-form-item label="模板名称" prop="name">
  11. <el-input v-model="formData.name" placeholder="请输入模板名称" />
  12. </el-form-item>
  13. <el-form-item label="备注" prop="remark">
  14. <el-input v-model="formData.remark" placeholder="请输入备注" type="textarea" />
  15. </el-form-item>
  16. <el-form-item label="预览图" prop="previewPicUrls">
  17. <UploadImgs v-model="formData.previewPicUrls" />
  18. </el-form-item>
  19. </el-form>
  20. <template #footer>
  21. <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
  22. <el-button @click="dialogVisible = false">取 消</el-button>
  23. </template>
  24. </Dialog>
  25. </template>
  26. <script lang="ts" setup>
  27. import * as DiyTemplateApi from '@/api/mall/promotion/diy/template'
  28. /** 装修模板表单 */
  29. defineOptions({ name: 'DiyTemplateForm' })
  30. const { t } = useI18n() // 国际化
  31. const message = useMessage() // 消息弹窗
  32. const dialogVisible = ref(false) // 弹窗的是否展示
  33. const dialogTitle = ref('') // 弹窗的标题
  34. const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
  35. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  36. const formData = ref({
  37. id: undefined,
  38. name: undefined,
  39. remark: undefined,
  40. previewPicUrls: []
  41. })
  42. const formRules = reactive({
  43. name: [{ required: true, message: '模板名称不能为空', trigger: 'blur' }]
  44. })
  45. const formRef = ref() // 表单 Ref
  46. /** 打开弹窗 */
  47. const open = async (type: string, id?: number) => {
  48. dialogVisible.value = true
  49. dialogTitle.value = t('action.' + type)
  50. formType.value = type
  51. resetForm()
  52. // 修改时,设置数据
  53. if (id) {
  54. formLoading.value = true
  55. try {
  56. formData.value = await DiyTemplateApi.getDiyTemplate(id)
  57. } finally {
  58. formLoading.value = false
  59. }
  60. }
  61. }
  62. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  63. /** 提交表单 */
  64. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  65. const submitForm = async () => {
  66. // 校验表单
  67. if (!formRef) return
  68. const valid = await formRef.value.validate()
  69. if (!valid) return
  70. // 提交请求
  71. formLoading.value = true
  72. try {
  73. const data = formData.value as unknown as DiyTemplateApi.DiyTemplateVO
  74. if (formType.value === 'create') {
  75. await DiyTemplateApi.createDiyTemplate(data)
  76. message.success(t('common.createSuccess'))
  77. } else {
  78. await DiyTemplateApi.updateDiyTemplate(data)
  79. message.success(t('common.updateSuccess'))
  80. }
  81. dialogVisible.value = false
  82. // 发送操作成功的事件
  83. emit('success')
  84. } finally {
  85. formLoading.value = false
  86. }
  87. }
  88. /** 重置表单 */
  89. const resetForm = () => {
  90. formData.value = {
  91. id: undefined,
  92. name: undefined,
  93. remark: undefined,
  94. previewPicUrls: []
  95. }
  96. formRef.value?.resetFields()
  97. }
  98. </script>