DataSourceModal.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <BasicModal v-bind="$attrs" @register="registerModal" :title="title" @ok="handleSubmit" width="40%">
  3. <BasicForm @register="registerForm">
  4. <template #pwd="{ model, field }">
  5. <a-row :gutter="8">
  6. <a-col :sm="15" :md="16" :lg="17" :xl="19">
  7. <a-input-password v-model:value="model[field]" placeholder="请输入密码" />
  8. </a-col>
  9. <a-col :sm="9" :md="7" :lg="7" :xl="5">
  10. <a-button type="primary" style="width: 100%" @click="handleTest">测试</a-button>
  11. </a-col>
  12. </a-row>
  13. </template>
  14. </BasicForm>
  15. </BasicModal>
  16. </template>
  17. <script lang="ts" setup>
  18. import { ref, computed, unref } from 'vue';
  19. import { BasicModal, useModalInner } from '/@/components/Modal';
  20. import { BasicForm, useForm } from '/@/components/Form/index';
  21. import { formSchema } from './datasource.data';
  22. import { saveOrUpdateDataSource, getDataSourceById, testConnection } from './datasource.api';
  23. import { useMessage } from '/@/hooks/web/useMessage';
  24. const { createMessage } = useMessage();
  25. // Emits声明
  26. const emit = defineEmits(['register', 'success']);
  27. const isUpdate = ref(true);
  28. //表单配置
  29. const [registerForm, { getFieldsValue, resetFields, validateFields, setFieldsValue, validate }] = useForm({
  30. // labelWidth: 150,
  31. schemas: formSchema,
  32. showActionButtonGroup: false,
  33. });
  34. //表单赋值
  35. const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  36. //重置表单
  37. await resetFields();
  38. setModalProps({ confirmLoading: false });
  39. isUpdate.value = !!data?.isUpdate;
  40. if (unref(isUpdate)) {
  41. //获取详情
  42. data.record = await getDataSourceById({ id: data.record.id });
  43. //表单赋值
  44. await setFieldsValue({
  45. ...data.record,
  46. });
  47. }
  48. });
  49. //设置标题
  50. const title = computed(() => (!unref(isUpdate) ? '新增数据源' : '编辑数据源'));
  51. async function handleTest() {
  52. let keys = ['dbType', 'dbDriver', 'dbUrl', 'dbName', 'dbUsername', 'dbPassword'];
  53. // 获取以上字段的值,并清除校验状态
  54. let fieldsValues = getFieldsValue(keys);
  55. let setFields = {};
  56. keys.forEach((key) => (setFields[key] = { value: fieldsValues[key], errors: null }));
  57. await validateFields(keys).then((values) => {
  58. let loading = createMessage.loading('连接中....', 0);
  59. testConnection(values)
  60. .then((data) => {
  61. if (data.success) {
  62. createMessage.success('连接成功');
  63. }
  64. })
  65. .catch((error) => {})
  66. .finally(() => loading());
  67. });
  68. }
  69. //表单提交事件
  70. async function handleSubmit(v) {
  71. try {
  72. let values = await validate();
  73. setModalProps({ confirmLoading: true });
  74. //提交表单
  75. await saveOrUpdateDataSource(values, isUpdate.value);
  76. //关闭弹窗
  77. closeModal();
  78. //刷新列表
  79. emit('success');
  80. } finally {
  81. setModalProps({ confirmLoading: false });
  82. }
  83. }
  84. </script>