edit.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <script lang="ts" setup>
  2. import { computed, ref, unref } from 'vue';
  3. import { useVbenModal } from '@vben/common-ui';
  4. import { message } from 'ant-design-vue';
  5. import { useFormOptions, useVbenForm } from '#/adapter';
  6. import { DictionaryApi } from '#/api';
  7. import { useSchema } from './data.config';
  8. defineOptions({
  9. name: 'DictItemEdit',
  10. });
  11. const emit = defineEmits(['success']);
  12. const modelRef = ref<Record<string, any>>({});
  13. const isUpdate = ref(true);
  14. const [Form, { validate, setValues, getValues }] = useVbenForm(
  15. useFormOptions({
  16. schema: useSchema(),
  17. }),
  18. );
  19. const [Modal, { close, setState, getData, lock, unlock }] = useVbenModal({
  20. fullscreenButton: false,
  21. draggable: true,
  22. onCancel() {
  23. close();
  24. },
  25. onConfirm: async () => {
  26. try {
  27. const { valid } = await validate();
  28. if (!valid) return;
  29. const values = await getValues();
  30. lock();
  31. const postParams = unref(modelRef);
  32. Object.assign(postParams, values);
  33. await (unref(isUpdate)
  34. ? DictionaryApi.editItemDetail(postParams as DictionaryApi.RecordItem)
  35. : DictionaryApi.addItemDetail(
  36. postParams as DictionaryApi.BasicRecordItem,
  37. ));
  38. message.success('操作成功');
  39. emit('success');
  40. close();
  41. } finally {
  42. unlock();
  43. }
  44. },
  45. onOpenChange: async (isOpen: boolean) => {
  46. if (isOpen) {
  47. setState({ loading: true });
  48. const data = getData<Record<string, any>>();
  49. isUpdate.value = !!data.isUpdate;
  50. modelRef.value = { ...data.baseData };
  51. if (unref(isUpdate)) {
  52. const entity = await DictionaryApi.getItemDetail(data.baseData.id);
  53. modelRef.value = { ...entity };
  54. setValues(entity);
  55. }
  56. setState({ loading: false });
  57. }
  58. },
  59. });
  60. const getTitle = computed(() =>
  61. unref(isUpdate) ? '编辑字典项' : '新增字典项',
  62. );
  63. </script>
  64. <template>
  65. <Modal class="w-[1000px]" :title="getTitle">
  66. <Form />
  67. </Modal>
  68. </template>