dictionary-form.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <div>
  3. <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable width="769px">
  4. <el-form ref="formRef" :model="form" size="default" label-width="80px">
  5. <el-row :gutter="35">
  6. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  7. <el-form-item label="名称" prop="name" :rules="[{ required: true, message: '请输入名称', trigger: ['blur', 'change'] }]">
  8. <el-input v-model="form.name" autocomplete="off" />
  9. </el-form-item>
  10. </el-col>
  11. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  12. <el-form-item label="编码">
  13. <el-input v-model="form.code" autocomplete="off" />
  14. </el-form-item>
  15. </el-col>
  16. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  17. <el-form-item label="字典值">
  18. <el-input v-model="form.value" autocomplete="off" />
  19. </el-form-item>
  20. </el-col>
  21. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
  22. <el-form-item label="启用">
  23. <el-switch v-model="form.enabled" />
  24. </el-form-item>
  25. </el-col>
  26. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
  27. <el-form-item label="说明">
  28. <el-input v-model="form.description" clearable type="textarea" />
  29. </el-form-item>
  30. </el-col>
  31. </el-row>
  32. </el-form>
  33. <template #footer>
  34. <span class="dialog-footer my-flex my-flex-y-center my-flex-between">
  35. <div>
  36. <el-checkbox v-if="!(state.form?.id > 0)" v-model="state.contiAdd">连续新增</el-checkbox>
  37. </div>
  38. <div>
  39. <el-button @click="onCancel" size="default">取 消</el-button>
  40. <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
  41. </div>
  42. </span>
  43. </template>
  44. </el-dialog>
  45. </div>
  46. </template>
  47. <script lang="ts" setup name="admin/dict/form">
  48. import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
  49. import { DictionaryAddInput, DictionaryUpdateInput } from '/@/api/admin/data-contracts'
  50. import { DictionaryApi } from '/@/api/admin/Dictionary'
  51. import eventBus from '/@/utils/mitt'
  52. import { FormInstance } from 'element-plus'
  53. defineProps({
  54. title: {
  55. type: String,
  56. default: '',
  57. },
  58. })
  59. const { proxy } = getCurrentInstance() as any
  60. const formRef = ref<FormInstance>()
  61. const state = reactive({
  62. showDialog: false,
  63. sureLoading: false,
  64. form: {} as DictionaryAddInput & DictionaryUpdateInput,
  65. contiAdd: true,
  66. })
  67. const { form } = toRefs(state)
  68. // 打开对话框
  69. const open = async (row: any = {}) => {
  70. if (row.id > 0) {
  71. state.contiAdd = false
  72. const res = await new DictionaryApi().get({ id: row.id }, { loading: true }).catch(() => {
  73. proxy.$modal.closeLoading()
  74. })
  75. if (res?.success) {
  76. state.form = res.data as DictionaryAddInput & DictionaryUpdateInput
  77. }
  78. } else {
  79. state.form = { dictionaryTypeId: row.dictionaryTypeId, enabled: true } as DictionaryAddInput & DictionaryUpdateInput
  80. }
  81. state.showDialog = true
  82. }
  83. // 取消
  84. const onCancel = () => {
  85. state.showDialog = false
  86. }
  87. // 确定
  88. const onSure = () => {
  89. formRef.value!.validate(async (valid: boolean) => {
  90. if (!valid) return
  91. state.sureLoading = true
  92. let res = {} as any
  93. if (state.form.id != undefined && state.form.id > 0) {
  94. res = await new DictionaryApi().update(state.form, { showSuccessMessage: true }).catch(() => {
  95. state.sureLoading = false
  96. })
  97. } else {
  98. res = await new DictionaryApi().add(state.form, { showSuccessMessage: true }).catch(() => {
  99. state.sureLoading = false
  100. })
  101. }
  102. state.sureLoading = false
  103. if (res?.success) {
  104. if (state.contiAdd) {
  105. formRef.value!.resetFields()
  106. }
  107. eventBus.emit('refreshDict')
  108. state.showDialog = state.contiAdd
  109. }
  110. })
  111. }
  112. defineExpose({
  113. open,
  114. })
  115. </script>