ny
昨天 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<script lang="ts" setup>
import type { FormSchema } from '@jnpf/ui/form';
 
import { computed, reactive } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
 
import { create, getInfo, update } from '#/api/systemData/variate';
import { $t } from '#/locales';
 
interface State {
  id: string;
  interfaceId: string;
  dataForm: any;
}
 
const emit = defineEmits(['register', 'reload']);
const state = reactive<State>({
  id: '',
  interfaceId: '',
  dataForm: {},
});
const schemas: FormSchema[] = [
  {
    field: 'fullName',
    label: '变量名',
    component: 'Input',
    componentProps: { placeholder: '请输入', maxlength: 50 },
    rules: [{ required: true, message: '必填', trigger: 'blur' }],
  },
  {
    field: 'expression',
    label: 'JS表达式',
    component: 'Textarea',
    componentProps: { placeholder: '请输入', rows: 4 },
    rules: [{ required: true, message: '必填', trigger: 'blur' }],
  },
];
const getTitle = computed(() => (state.id ? $t('common.editText') : $t('common.addText')));
const { createMessage } = useMessage();
const [registerForm, { validate, resetFields, setFieldsValue }] = useForm({ labelWidth: 80, schemas });
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
 
function init(data) {
  resetFields();
  state.interfaceId = data.interfaceId;
  state.id = data.id;
  if (state.id) {
    changeLoading(true);
    getInfo(state.id)
      .then((res) => {
        setFieldsValue(res.data);
        changeLoading(false);
      })
      .catch(() => {
        changeLoading(false);
      });
  }
}
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  changeOkLoading(true);
  const query = {
    ...values,
    id: state.id,
    interfaceId: state.interfaceId,
  };
  const formMethod = state.id ? update : create;
  formMethod(query)
    .then((res) => {
      createMessage.success(res.msg);
      changeOkLoading(false);
      closeModal();
      setTimeout(() => {
        emit('reload');
      }, 200);
    })
    .catch(() => {
      changeOkLoading(false);
    });
}
</script>
<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit" destroy-on-close>
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>