Skip to content

Commit 21827d4

Browse files
YunaiVgitee-org
authored andcommitted
!25 Vue3 重构:基础设施 -> 数据源配置功能
Merge pull request !25 from xiaowuye/dev
2 parents 74d225c + 8eb8748 commit 21827d4

File tree

4 files changed

+211
-196
lines changed

4 files changed

+211
-196
lines changed
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
import request from '@/config/axios'
22

33
export interface DataSourceConfigVO {
4-
id: number
4+
id: number | undefined
55
name: string
66
url: string
77
username: string
88
password: string
9-
createTime: Date
10-
}
11-
12-
// 查询数据源配置列表
13-
export const getDataSourceConfigListApi = () => {
14-
return request.get({ url: '/infra/data-source-config/list' })
15-
}
16-
17-
// 查询数据源配置详情
18-
export const getDataSourceConfigApi = (id: number) => {
19-
return request.get({ url: '/infra/data-source-config/get?id=' + id })
9+
createTime?: Date
2010
}
2111

2212
// 新增数据源配置
23-
export const createDataSourceConfigApi = (data: DataSourceConfigVO) => {
13+
export const createDataSourceConfig = (data: DataSourceConfigVO) => {
2414
return request.post({ url: '/infra/data-source-config/create', data })
2515
}
2616

2717
// 修改数据源配置
28-
export const updateDataSourceConfigApi = (data: DataSourceConfigVO) => {
18+
export const updateDataSourceConfig = (data: DataSourceConfigVO) => {
2919
return request.put({ url: '/infra/data-source-config/update', data })
3020
}
3121

3222
// 删除数据源配置
33-
export const deleteDataSourceConfigApi = (id: number) => {
23+
export const deleteDataSourceConfig = (id: number) => {
3424
return request.delete({ url: '/infra/data-source-config/delete?id=' + id })
3525
}
26+
27+
// 查询数据源配置详情
28+
export const getDataSourceConfig = (id: number) => {
29+
return request.get({ url: '/infra/data-source-config/get?id=' + id })
30+
}
31+
32+
// 查询数据源配置列表
33+
export const getDataSourceConfigList = () => {
34+
return request.get({ url: '/infra/data-source-config/list' })
35+
}

src/views/infra/dataSourceConfig/dataSourceConfig.data.ts

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

0 commit comments

Comments
 (0)