form.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <!-- TODO 芋艿:Dialog 貌似高度不太对劲 已解决:textarea导致 设置一个最大高就行了 -->
  3. <Dialog :title="modelTitle" v-model="modelVisible" :loading="modelLoading" :max-height="'310px'">
  4. <el-form ref="ruleFormRef" :model="formData" :rules="formRules" label-width="80px">
  5. <el-form-item label="参数分类" prop="category">
  6. <el-input v-model="formData.category" placeholder="请输入参数分类" />
  7. </el-form-item>
  8. <el-form-item label="参数名称" prop="name">
  9. <el-input v-model="formData.name" placeholder="请输入参数名称" />
  10. </el-form-item>
  11. <el-form-item label="参数键名" prop="key">
  12. <el-input v-model="formData.key" placeholder="请输入参数键名" />
  13. </el-form-item>
  14. <el-form-item label="参数键值" prop="value">
  15. <el-input v-model="formData.value" placeholder="请输入参数键值" />
  16. </el-form-item>
  17. <el-form-item label="是否可见" prop="visible">
  18. <!-- TODO 芋艿:改成组件 -->
  19. <el-radio-group v-model="formData.visible">
  20. <el-radio :key="true" :label="true">是</el-radio>
  21. <el-radio :key="false" :label="false">否</el-radio>
  22. </el-radio-group>
  23. </el-form-item>
  24. <el-form-item label="备注" prop="remark">
  25. <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容" />
  26. </el-form-item>
  27. </el-form>
  28. <template #footer>
  29. <div class="dialog-footer">
  30. <el-button type="primary" @click="submitForm">确 定</el-button>
  31. <el-button @click="colseForm(ruleFormRef)">取 消</el-button>
  32. </div>
  33. </template>
  34. </Dialog>
  35. </template>
  36. <script setup lang="ts">
  37. import * as ConfigApi from '@/api/infra/config'
  38. // import type { FormExpose } from '@/components/Form'
  39. import type { FormInstance } from 'element-plus'
  40. const { t } = useI18n() // 国际化
  41. const message = useMessage() // 消息弹窗
  42. // const { proxy } = getCurrentInstance()
  43. const modelVisible = ref(false) // 弹窗的是否展示
  44. const modelTitle = ref('') // 弹窗的标题
  45. const modelLoading = ref(false) // 弹窗的 Loading 加载
  46. const formType = ref('') // 表单的类型:create - 新增;update - 修改
  47. const formLoading = ref(false) // 操作按钮的 Loading 加载
  48. // let formRef = ref() // 表单的 Ref
  49. const formData = reactive({
  50. id: 0,
  51. category: '',
  52. name: '',
  53. key: '',
  54. value: '',
  55. visible: true,
  56. remark: ''
  57. })
  58. const formRules = reactive({
  59. category: [{ required: true, message: '参数分类不能为空', trigger: 'blur' }],
  60. name: [{ required: true, message: '参数名称不能为空', trigger: 'blur' }],
  61. key: [{ required: true, message: '参数键名不能为空', trigger: 'blur' }],
  62. value: [{ required: true, message: '参数键值不能为空', trigger: 'blur' }],
  63. visible: [{ required: true, message: '是否可见不能为空', trigger: 'blur' }]
  64. })
  65. const ruleFormRef = ref<FormInstance>() // 表单 Ref
  66. const { proxy } = getCurrentInstance() as any
  67. /** 打开弹窗 */
  68. const openModal = async (type: string, id?: number) => {
  69. modelVisible.value = true
  70. modelLoading.value = true
  71. modelTitle.value = t('action.' + type)
  72. formType.value = type
  73. // 设置数据
  74. resetForm()
  75. if (id) {
  76. const data = await ConfigApi.getConfig(id)
  77. Object.assign(formData, data)
  78. }
  79. modelLoading.value = false
  80. }
  81. defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗
  82. /** 提交表单 */
  83. const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
  84. const submitForm = async () => {
  85. const formRef = proxy.$refs['ruleFormRef']
  86. console.log(formRef, '======')
  87. // 校验表单
  88. if (!formRef) return
  89. const valid = await formRef.validate()
  90. if (!valid) return
  91. // 提交请求
  92. formLoading.value = true
  93. try {
  94. const data = formData as ConfigApi.ConfigVO
  95. if (formType.value === 'create') {
  96. await ConfigApi.createConfig(data)
  97. message.success(t('common.createSuccess'))
  98. } else {
  99. await ConfigApi.updateConfig(data)
  100. message.success(t('common.updateSuccess'))
  101. }
  102. modelVisible.value = false
  103. emit('success')
  104. } finally {
  105. formLoading.value = false
  106. }
  107. }
  108. /** 重置表单 */
  109. const resetForm = () => {
  110. formData.id = 0
  111. formData.category = ''
  112. formData.name = ''
  113. formData.key = ''
  114. formData.value = ''
  115. formData.visible = true
  116. formData.remark = ''
  117. // proxy.$refs['ruleFormRef'].resetFields()
  118. // setTimeout(() => {
  119. // console.log(ruleFormRef.value, 'formRef.value')
  120. // formRef.value.resetFields() // TODO 芋艿:为什么拿不到 formRef 呢? 表单还没加载出来
  121. // ruleFormRef.value?.resetFields()
  122. // }, 100)
  123. }
  124. /** 取消添加 */
  125. const colseForm = (formEl: FormInstance | undefined) => {
  126. if (!formEl) return
  127. formEl.resetFields()
  128. modelVisible.value = false
  129. }
  130. </script>