DiyTemplateForm.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <template>
  2. <Dialog :title="dialogTitle" v-model="dialogVisible">
  3. <el-form
  4. ref="formRef"
  5. :model="formData"
  6. :rules="formRules"
  7. label-width="100px"
  8. v-loading="formLoading"
  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 @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
  22. <el-button @click="dialogVisible = false">取 消</el-button>
  23. </template>
  24. </Dialog>
  25. </template>
  26. <script setup lang="ts">
  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. const diyTemplate = await DiyTemplateApi.getDiyTemplate(id)
  57. // 处理预览图
  58. if (diyTemplate?.previewPicUrls?.length > 0) {
  59. diyTemplate.previewPicUrls = diyTemplate.previewPicUrls.map((url: string) => {
  60. return { url }
  61. })
  62. }
  63. formData.value = diyTemplate
  64. } finally {
  65. formLoading.value = false
  66. }
  67. }
  68. }
  69. defineExpose({ open }) // 提供 open 方法,用于打开弹窗
  70. /** 提交表单 */
  71. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  72. const submitForm = async () => {
  73. // 校验表单
  74. if (!formRef) return
  75. const valid = await formRef.value.validate()
  76. if (!valid) return
  77. // 提交请求
  78. formLoading.value = true
  79. try {
  80. // 处理预览图
  81. const previewPicUrls = formData.value.previewPicUrls.map((item) => {
  82. return item['url'] ? item['url'] : item
  83. })
  84. const data = { ...formData.value, previewPicUrls } as unknown as DiyTemplateApi.DiyTemplateVO
  85. if (formType.value === 'create') {
  86. await DiyTemplateApi.createDiyTemplate(data)
  87. message.success(t('common.createSuccess'))
  88. } else {
  89. await DiyTemplateApi.updateDiyTemplate(data)
  90. message.success(t('common.updateSuccess'))
  91. }
  92. dialogVisible.value = false
  93. // 发送操作成功的事件
  94. emit('success')
  95. } finally {
  96. formLoading.value = false
  97. }
  98. }
  99. /** 重置表单 */
  100. const resetForm = () => {
  101. formData.value = {
  102. id: undefined,
  103. name: undefined,
  104. remark: undefined,
  105. previewPicUrls: []
  106. }
  107. formRef.value?.resetFields()
  108. }
  109. </script>