2023-08-27 11:07:29 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div>
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-drawer :title="title" v-model="dialogVisible" :before-close="cancel" :destroy-on-close="true" :close-on-click-modal="false" size="40%">
|
|
|
|
|
|
<template #header>
|
|
|
|
|
|
<DrawerHeader :header="title" :back="cancel" />
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
2023-08-27 11:07:29 +08:00
|
|
|
|
<el-form :model="form" ref="dbForm" :rules="rules" label-width="auto">
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-divider content-position="left">基本</el-divider>
|
2024-04-17 21:28:28 +08:00
|
|
|
|
|
|
|
|
|
|
<el-form-item ref="tagSelectRef" prop="tagCodePaths" label="标签">
|
|
|
|
|
|
<tag-tree-select
|
|
|
|
|
|
multiple
|
|
|
|
|
|
@change-tag="
|
|
|
|
|
|
(paths: any) => {
|
|
|
|
|
|
form.tagCodePaths = paths;
|
|
|
|
|
|
tagSelectRef.validate();
|
|
|
|
|
|
}
|
|
|
|
|
|
"
|
|
|
|
|
|
:select-tags="form.tagCodePaths"
|
|
|
|
|
|
style="width: 100%"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item prop="name" label="名称" required>
|
|
|
|
|
|
<el-input v-model.trim="form.name" placeholder="请输入数据库别名" auto-complete="off"></el-input>
|
|
|
|
|
|
</el-form-item>
|
2024-03-07 17:26:11 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item prop="type" label="类型" required>
|
|
|
|
|
|
<el-select @change="changeDbType" style="width: 100%" v-model="form.type" placeholder="请选择数据库类型">
|
|
|
|
|
|
<el-option
|
|
|
|
|
|
v-for="(dbTypeAndDialect, key) in getDbDialectMap()"
|
|
|
|
|
|
:key="key"
|
|
|
|
|
|
:value="dbTypeAndDialect[0]"
|
|
|
|
|
|
:label="dbTypeAndDialect[1].getInfo().name"
|
|
|
|
|
|
>
|
|
|
|
|
|
<SvgIcon :name="dbTypeAndDialect[1].getInfo().icon" :size="20" />
|
|
|
|
|
|
{{ dbTypeAndDialect[1].getInfo().name }}
|
|
|
|
|
|
</el-option>
|
2024-01-29 16:02:28 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<template #prefix>
|
|
|
|
|
|
<SvgIcon :name="getDbDialect(form.type).getInfo().icon" :size="20" />
|
|
|
|
|
|
</template>
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
2024-03-07 17:26:11 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item v-if="form.type !== DbType.sqlite" prop="host" label="host" required>
|
|
|
|
|
|
<el-col :span="18">
|
2024-04-17 21:28:28 +08:00
|
|
|
|
<el-input v-model.trim="form.host" placeholder="请输入ip" auto-complete="off"></el-input>
|
2024-04-12 13:24:20 +08:00
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col style="text-align: center" :span="1">:</el-col>
|
|
|
|
|
|
<el-col :span="5">
|
|
|
|
|
|
<el-input type="number" v-model.number="form.port" placeholder="端口"></el-input>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
</el-form-item>
|
2024-01-19 08:59:35 +00:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item v-if="form.type === DbType.sqlite" prop="host" label="sqlite地址">
|
|
|
|
|
|
<el-input v-model.trim="form.host" placeholder="请输入sqlite文件在服务器的绝对地址"></el-input>
|
|
|
|
|
|
</el-form-item>
|
2024-01-19 08:59:35 +00:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item v-if="form.type === DbType.oracle" label="SID|服务名">
|
|
|
|
|
|
<el-col :span="5">
|
|
|
|
|
|
<el-select
|
|
|
|
|
|
@change="
|
|
|
|
|
|
() => {
|
|
|
|
|
|
state.extra.serviceName = '';
|
|
|
|
|
|
state.extra.sid = '';
|
|
|
|
|
|
}
|
|
|
|
|
|
"
|
|
|
|
|
|
v-model="state.extra.stype"
|
|
|
|
|
|
placeholder="请选择"
|
|
|
|
|
|
>
|
|
|
|
|
|
<el-option label="服务名" :value="1" />
|
|
|
|
|
|
<el-option label="SID" :value="2" />
|
|
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
<el-col style="text-align: center" :span="1">:</el-col>
|
|
|
|
|
|
<el-col :span="18">
|
|
|
|
|
|
<el-input v-if="state.extra.stype == 1" v-model="state.extra.serviceName" placeholder="请输入服务名"> </el-input>
|
|
|
|
|
|
<el-input v-else v-model="state.extra.sid" placeholder="请输入SID"> </el-input>
|
|
|
|
|
|
</el-col>
|
|
|
|
|
|
</el-form-item>
|
2024-03-07 17:26:11 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item prop="remark" label="备注">
|
|
|
|
|
|
<el-input v-model="form.remark" auto-complete="off" type="textarea"></el-input>
|
|
|
|
|
|
</el-form-item>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
2024-04-13 17:01:12 +08:00
|
|
|
|
<el-divider content-position="left">账号</el-divider>
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<ResourceAuthCertTableEdit
|
|
|
|
|
|
v-model="form.authCerts"
|
|
|
|
|
|
:resource-code="form.code"
|
|
|
|
|
|
:resource-type="TagResourceTypeEnum.Db.value"
|
|
|
|
|
|
:test-conn-btn-loading="testConnBtnLoading"
|
|
|
|
|
|
@test-conn="testConn"
|
|
|
|
|
|
:disable-ciphertext-type="[AuthCertCiphertextTypeEnum.PrivateKey.value]"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</div>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-divider content-position="left">其他</el-divider>
|
|
|
|
|
|
<el-form-item prop="params" label="连接参数">
|
2024-04-17 21:28:28 +08:00
|
|
|
|
<el-input v-model.trim="form.params" placeholder="其他连接参数,形如: key1=value1&key2=value2"> </el-input>
|
2024-04-12 13:24:20 +08:00
|
|
|
|
</el-form-item>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
<el-form-item prop="sshTunnelMachineId" label="SSH隧道">
|
|
|
|
|
|
<ssh-tunnel-select v-model="form.sshTunnelMachineId" />
|
|
|
|
|
|
</el-form-item>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
</el-form>
|
|
|
|
|
|
|
|
|
|
|
|
<template #footer>
|
2024-04-17 21:28:28 +08:00
|
|
|
|
<el-button @click="cancel()">取 消</el-button>
|
|
|
|
|
|
<el-button type="primary" :loading="saveBtnLoading" @click="btnOk">确 定</el-button>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
</template>
|
2024-04-12 13:24:20 +08:00
|
|
|
|
</el-drawer>
|
2023-08-27 11:07:29 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2024-04-17 21:28:28 +08:00
|
|
|
|
import { reactive, ref, toRefs, watchEffect } from 'vue';
|
2023-08-27 11:07:29 +08:00
|
|
|
|
import { dbApi } from './api';
|
|
|
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
import SshTunnelSelect from '../component/SshTunnelSelect.vue';
|
2024-01-29 16:02:28 +08:00
|
|
|
|
import { DbType, getDbDialect, getDbDialectMap } from './dialect';
|
2023-12-07 11:48:17 +08:00
|
|
|
|
import SvgIcon from '@/components/svgIcon/index.vue';
|
2024-04-12 13:24:20 +08:00
|
|
|
|
import DrawerHeader from '@/components/drawer-header/DrawerHeader.vue';
|
|
|
|
|
|
import { TagResourceTypeEnum } from '@/common/commonEnum';
|
|
|
|
|
|
import ResourceAuthCertTableEdit from '../component/ResourceAuthCertTableEdit.vue';
|
|
|
|
|
|
import { AuthCertCiphertextTypeEnum } from '../tag/enums';
|
2024-04-17 21:28:28 +08:00
|
|
|
|
import TagTreeSelect from '../component/TagTreeSelect.vue';
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
|
|
|
|
|
const props = defineProps({
|
|
|
|
|
|
visible: {
|
|
|
|
|
|
type: Boolean,
|
|
|
|
|
|
},
|
2023-08-30 22:41:42 +08:00
|
|
|
|
data: {
|
2023-08-27 11:07:29 +08:00
|
|
|
|
type: [Boolean, Object],
|
|
|
|
|
|
},
|
|
|
|
|
|
title: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
},
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//定义事件
|
|
|
|
|
|
const emit = defineEmits(['update:visible', 'cancel', 'val-change']);
|
|
|
|
|
|
|
|
|
|
|
|
const rules = {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
tagCodePaths: [
|
|
|
|
|
|
{
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
message: '请选择标签',
|
|
|
|
|
|
trigger: ['change'],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
2023-08-27 11:07:29 +08:00
|
|
|
|
name: [
|
|
|
|
|
|
{
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
message: '请输入别名',
|
|
|
|
|
|
trigger: ['change', 'blur'],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
type: [
|
|
|
|
|
|
{
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
message: '请选择数据库类型',
|
|
|
|
|
|
trigger: ['change', 'blur'],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
|
|
|
|
|
host: [
|
|
|
|
|
|
{
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
message: '请输入主机ip和port',
|
2023-12-07 11:48:17 +08:00
|
|
|
|
trigger: ['blur'],
|
2023-08-27 11:07:29 +08:00
|
|
|
|
},
|
|
|
|
|
|
],
|
2024-01-15 11:55:59 +00:00
|
|
|
|
sid: [
|
|
|
|
|
|
{
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
message: '请输入SID',
|
|
|
|
|
|
trigger: ['change', 'blur'],
|
|
|
|
|
|
},
|
|
|
|
|
|
],
|
2023-08-27 11:07:29 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const dbForm: any = ref(null);
|
2024-04-17 21:28:28 +08:00
|
|
|
|
const tagSelectRef: any = ref(null);
|
|
|
|
|
|
|
|
|
|
|
|
const DefaultForm = {
|
|
|
|
|
|
id: null,
|
|
|
|
|
|
type: DbType.mysql,
|
|
|
|
|
|
code: '',
|
|
|
|
|
|
name: null,
|
|
|
|
|
|
host: '',
|
2024-04-18 20:50:14 +08:00
|
|
|
|
port: getDbDialect(DbType.mysql).getInfo().defaultPort,
|
2024-04-17 21:28:28 +08:00
|
|
|
|
extra: '', // 连接需要的额外参数(json字符串)
|
|
|
|
|
|
params: null,
|
|
|
|
|
|
remark: '',
|
|
|
|
|
|
sshTunnelMachineId: null as any,
|
|
|
|
|
|
authCerts: [],
|
|
|
|
|
|
tagCodePaths: [],
|
|
|
|
|
|
};
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
|
|
|
|
|
const state = reactive({
|
|
|
|
|
|
dialogVisible: false,
|
2024-03-07 17:26:11 +08:00
|
|
|
|
extra: {} as any, // 连接需要的额外参数(json)
|
2024-04-17 21:28:28 +08:00
|
|
|
|
form: DefaultForm,
|
2024-04-12 13:24:20 +08:00
|
|
|
|
submitForm: {} as any,
|
2023-08-27 11:07:29 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
const { dialogVisible, form, submitForm } = toRefs(state);
|
2023-12-11 01:00:09 +08:00
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
const { isFetching: saveBtnLoading, execute: saveInstanceExec, data: saveInstanceRes } = dbApi.saveInstance.useApi(submitForm);
|
2024-01-29 04:20:23 +00:00
|
|
|
|
const { isFetching: testConnBtnLoading, execute: testConnExec } = dbApi.testConn.useApi(submitForm);
|
2023-08-27 11:07:29 +08:00
|
|
|
|
|
2024-04-17 21:28:28 +08:00
|
|
|
|
watchEffect(() => {
|
|
|
|
|
|
state.dialogVisible = props.visible;
|
2023-08-27 11:07:29 +08:00
|
|
|
|
if (!state.dialogVisible) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2024-04-17 21:28:28 +08:00
|
|
|
|
const dbInst: any = props.data;
|
|
|
|
|
|
if (dbInst) {
|
|
|
|
|
|
state.form = { ...dbInst };
|
|
|
|
|
|
state.form.tagCodePaths = dbInst.tags.map((t: any) => t.codePath) || [];
|
2024-04-12 07:53:42 +00:00
|
|
|
|
try {
|
|
|
|
|
|
state.extra = JSON.parse(state.form.extra);
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
state.extra = {};
|
|
|
|
|
|
}
|
2023-08-27 11:07:29 +08:00
|
|
|
|
} else {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
state.form = { ...DefaultForm };
|
2024-04-12 13:24:20 +08:00
|
|
|
|
state.form.authCerts = [];
|
2023-08-27 11:07:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2023-11-12 20:14:44 +08:00
|
|
|
|
const getReqForm = async () => {
|
2024-04-17 21:28:28 +08:00
|
|
|
|
const reqForm: any = { ...state.form };
|
|
|
|
|
|
reqForm.selectAuthCert = null;
|
|
|
|
|
|
reqForm.tags = null;
|
2023-11-12 20:14:44 +08:00
|
|
|
|
if (!state.form.sshTunnelMachineId) {
|
|
|
|
|
|
reqForm.sshTunnelMachineId = -1;
|
|
|
|
|
|
}
|
2024-03-07 17:26:11 +08:00
|
|
|
|
if (Object.keys(state.extra).length > 0) {
|
|
|
|
|
|
reqForm.extra = JSON.stringify(state.extra);
|
|
|
|
|
|
}
|
2023-11-12 20:14:44 +08:00
|
|
|
|
return reqForm;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-04-12 13:24:20 +08:00
|
|
|
|
const testConn = async (authCert: any) => {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await dbForm.value.validate();
|
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
ElMessage.error('请正确填写信息');
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2023-12-11 01:00:09 +08:00
|
|
|
|
|
2024-04-28 23:45:57 +08:00
|
|
|
|
state.submitForm = await getReqForm();
|
|
|
|
|
|
state.submitForm.authCerts = [authCert];
|
|
|
|
|
|
await testConnExec();
|
|
|
|
|
|
ElMessage.success('连接成功');
|
2023-11-12 20:14:44 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2023-08-27 11:07:29 +08:00
|
|
|
|
const btnOk = async () => {
|
2024-04-28 23:45:57 +08:00
|
|
|
|
try {
|
|
|
|
|
|
await dbForm.value.validate();
|
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
|
ElMessage.error('请正确填写信息');
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2023-12-11 01:00:09 +08:00
|
|
|
|
|
2024-04-28 23:45:57 +08:00
|
|
|
|
state.submitForm = await getReqForm();
|
|
|
|
|
|
await saveInstanceExec();
|
|
|
|
|
|
ElMessage.success('保存成功');
|
|
|
|
|
|
state.form.id = saveInstanceRes as any;
|
|
|
|
|
|
emit('val-change', state.form);
|
|
|
|
|
|
cancel();
|
2023-08-27 11:07:29 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const cancel = () => {
|
|
|
|
|
|
emit('update:visible', false);
|
|
|
|
|
|
emit('cancel');
|
2024-03-07 17:26:11 +08:00
|
|
|
|
state.extra = {};
|
2023-08-27 11:07:29 +08:00
|
|
|
|
};
|
2024-04-17 21:28:28 +08:00
|
|
|
|
|
|
|
|
|
|
const changeDbType = (val: string) => {
|
|
|
|
|
|
if (!state.form.id) {
|
|
|
|
|
|
state.form.port = getDbDialect(val).getInfo().defaultPort as any;
|
|
|
|
|
|
}
|
|
|
|
|
|
state.extra = {};
|
|
|
|
|
|
};
|
2023-08-27 11:07:29 +08:00
|
|
|
|
</script>
|
|
|
|
|
|
<style lang="scss"></style>
|