dict-form.vue 4.2 KB

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