Ben Lin
2024-06-07 62541dd3e9ea18836bec9ab6f35d2a7b9dd1e741
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="600px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, onMounted } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useGlobSetting } from '/@/hooks/setting';
  import { SaveEntity, fetchJson } from '/@/api/tigerapi/system';
 
  const emit = defineEmits(['success', 'register']);
  const globSetting = useGlobSetting();
  const isUpdate = ref(true);
  const entityName = ref('');
  const formSchema = ref([]);
  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 140,
    schemas: formSchema as unknown as FormSchema[],
    actionColOptions: {
      span: 24,
    },
    showActionButtonGroup: false,
  });
 
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    isUpdate.value = !!data?.isUpdate;
    entityName.value = data?.entityName;
    // formSchema.value = await fetchJson(
    //   `${globSetting.downloadUrl}/LowCode/${unref(entityName)}/${
    //     entityName.value
    //   }_addEditform.json`,
    // );
    formSchema.value = data?.formJson;
    resetFields();
    setDrawerProps({ confirmLoading: false });
 
    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    }
  });
 
  const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
 
  onMounted(async () => {});
  async function handleSubmit() {
    try {
      const values = await validate();
      setDrawerProps({ confirmLoading: true });
      // TODO custom api
      //保存
      const apiAction = SaveEntity(values, unref(isUpdate), unref(entityName));
      apiAction.then((action) => {
        if (action.IsSuccessed) {
          closeDrawer();
          emit('success');
        }
      });
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>