edit.vue 1.9 KB

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