提交 | 用户 | 时间
|
820397
|
1 |
<template> |
H |
2 |
<Dialog v-model="dialogVisible" :title="dialogTitle"> |
|
3 |
<el-form |
|
4 |
ref="formRef" |
|
5 |
v-loading="formLoading" |
|
6 |
:model="formData" |
|
7 |
:rules="formRules" |
|
8 |
label-width="100px" |
|
9 |
> |
|
10 |
<el-form-item label="数据源名称" prop="name"> |
|
11 |
<el-input v-model="formData.name" placeholder="请输入参数名称" /> |
|
12 |
</el-form-item> |
|
13 |
<el-form-item label="数据源连接" prop="url"> |
|
14 |
<el-input v-model="formData.url" placeholder="请输入数据源连接" /> |
|
15 |
</el-form-item> |
|
16 |
<el-form-item label="用户名" prop="username"> |
|
17 |
<el-input v-model="formData.username" placeholder="请输入用户名" /> |
|
18 |
</el-form-item> |
|
19 |
<el-form-item label="密码" prop="password"> |
|
20 |
<el-input v-model="formData.password" placeholder="请输入密码" /> |
|
21 |
</el-form-item> |
|
22 |
</el-form> |
|
23 |
<template #footer> |
|
24 |
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button> |
|
25 |
<el-button @click="dialogVisible = false">取 消</el-button> |
|
26 |
</template> |
|
27 |
</Dialog> |
|
28 |
</template> |
|
29 |
<script lang="ts" setup> |
|
30 |
import * as DataSourceConfigApi from '@/api/infra/dataSourceConfig' |
|
31 |
|
|
32 |
defineOptions({ name: 'InfraDataSourceConfigForm' }) |
|
33 |
|
|
34 |
const { t } = useI18n() // 国际化 |
|
35 |
const message = useMessage() // 消息弹窗 |
|
36 |
|
|
37 |
const dialogVisible = ref(false) // 弹窗的是否展示 |
|
38 |
const dialogTitle = ref('') // 弹窗的标题 |
|
39 |
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
|
40 |
const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
|
41 |
const formData = ref<DataSourceConfigApi.DataSourceConfigVO>({ |
|
42 |
id: undefined, |
|
43 |
name: '', |
|
44 |
url: '', |
|
45 |
username: '', |
|
46 |
password: '' |
|
47 |
}) |
|
48 |
const formRules = reactive({ |
|
49 |
name: [{ required: true, message: '数据源名称不能为空', trigger: 'blur' }], |
|
50 |
url: [{ required: true, message: '数据源连接不能为空', trigger: 'blur' }], |
|
51 |
username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }], |
|
52 |
password: [{ required: true, message: '密码不能为空', trigger: 'blur' }] |
|
53 |
}) |
|
54 |
const formRef = ref() // 表单 Ref |
|
55 |
|
|
56 |
/** 打开弹窗 */ |
|
57 |
const open = async (type: string, id?: number) => { |
|
58 |
dialogVisible.value = true |
|
59 |
dialogTitle.value = t('action.' + type) |
|
60 |
formType.value = type |
|
61 |
resetForm() |
|
62 |
// 修改时,设置数据 |
|
63 |
if (id) { |
|
64 |
formLoading.value = true |
|
65 |
try { |
|
66 |
formData.value = await DataSourceConfigApi.getDataSourceConfig(id) |
|
67 |
} finally { |
|
68 |
formLoading.value = false |
|
69 |
} |
|
70 |
} |
|
71 |
} |
|
72 |
defineExpose({ open }) // 提供 open 方法,用于打开弹窗 |
|
73 |
|
|
74 |
/** 提交表单 */ |
|
75 |
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
|
76 |
const submitForm = async () => { |
|
77 |
// 校验表单 |
|
78 |
if (!formRef) return |
|
79 |
const valid = await formRef.value.validate() |
|
80 |
if (!valid) return |
|
81 |
// 提交请求 |
|
82 |
formLoading.value = true |
|
83 |
try { |
|
84 |
const data = formData.value as DataSourceConfigApi.DataSourceConfigVO |
|
85 |
if (formType.value === 'create') { |
|
86 |
await DataSourceConfigApi.createDataSourceConfig(data) |
|
87 |
message.success(t('common.createSuccess')) |
|
88 |
} else { |
|
89 |
await DataSourceConfigApi.updateDataSourceConfig(data) |
|
90 |
message.success(t('common.updateSuccess')) |
|
91 |
} |
|
92 |
dialogVisible.value = false |
|
93 |
// 发送操作成功的事件 |
|
94 |
emit('success') |
|
95 |
} finally { |
|
96 |
formLoading.value = false |
|
97 |
} |
|
98 |
} |
|
99 |
|
|
100 |
/** 重置表单 */ |
|
101 |
const resetForm = () => { |
|
102 |
formData.value = { |
|
103 |
id: undefined, |
|
104 |
name: '', |
|
105 |
url: '', |
|
106 |
username: '', |
|
107 |
password: '' |
|
108 |
} |
|
109 |
formRef.value?.resetFields() |
|
110 |
} |
|
111 |
</script> |