ny
22 小时以前 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script lang="ts" setup>
import { computed, ref, unref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { BasicForm, useForm } from '@jnpf/ui/form';
import { BasicModal, useModalInner } from '@jnpf/ui/modal';
import { formValidate } from '@jnpf/utils';
 
import { createCommonFields as create, getCommonFieldsInfo as getInfo, updateCommonFields as update } from '#/api/systemData/commonFields';
import { $t } from '#/locales';
 
const emit = defineEmits(['register', 'reload']);
const typeOptions = [
  { fullName: '字符串', id: 'varchar' },
  { fullName: '整型', id: 'int' },
  { fullName: '日期时间', id: 'datetime' },
  { fullName: '浮点', id: 'decimal' },
  { fullName: '长整型', id: 'bigint' },
  { fullName: '文本', id: 'text' },
];
const [registerForm, { setFieldsValue, resetFields, validate }] = useForm({
  labelWidth: 60,
  schemas: [
    {
      field: 'field',
      label: '列名',
      component: 'Input',
      componentProps: { placeholder: '请输入', maxlength: 50 },
      rules: [
        { required: true, trigger: 'blur', message: '必填' },
        { pattern: /(^_([a-z0-9]_?)*$)|(^[a-z](_?[a-z0-9])*_?$)/i, message: '请输入正确的列名', trigger: 'blur' },
      ],
    },
    {
      field: 'fieldName',
      label: '说明',
      component: 'Input',
      componentProps: { placeholder: '请输入', maxlength: 50 },
      rules: [
        { required: true, trigger: 'blur', message: '必填' },
        { validator: formValidate('fullName', '不能含有特殊符号'), trigger: 'blur' },
      ],
    },
    {
      field: 'dataType',
      label: '类型',
      component: 'Select',
      componentProps: { options: typeOptions, placeholder: '请选择', showSearch: true },
      rules: [{ required: true, trigger: 'change', message: '必填' }],
    },
    {
      field: 'dataLength',
      label: '长度',
      defaultValue: 50,
      component: 'InputNumber',
      componentProps: { min: 1, max: 999999 },
      rules: [{ required: true, trigger: 'blur', message: '必填', type: 'number' }],
    },
    {
      field: 'allowNull',
      label: '允许空',
      defaultValue: 1,
      component: 'Switch',
      rules: [{ required: true, trigger: 'change', message: '必填' }],
    },
  ],
});
const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner(init);
const id = ref('');
const { createMessage } = useMessage();
 
const getTitle = computed(() => (unref(id) ? $t('common.editText') : $t('common.addText')));
 
function init(data) {
  changeLoading(true);
  resetFields();
  id.value = data.id;
  if (id.value) {
    getInfo(id.value).then((res) => {
      res.data.dataLength = Number(res.data.dataLength);
      setFieldsValue(res.data);
      changeLoading(false);
    });
  } else {
    changeLoading(false);
  }
}
 
async function handleSubmit() {
  const values = await validate();
  if (!values) return;
  changeOkLoading(true);
  const query = {
    ...values,
    id: id.value,
  };
  const formMethod = id.value ? 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">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>