dictionary-form.vue 3.5 KB

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