验证form表单的内容是否已存在数据库
<template>
<el-dialog
:visible.sync="dialogVisible"
width="50%"
:show-close="false"
:lock-scroll="false"
>
<dv-border-box-10
ref="dv"
:backgroundColor="chartBackgroundColor"
:color="['white', 'white']"
>
<div style="padding: 30px; margin: 0px 30px">
<i class="el-icon-close" @click="cancel"></i>
<el-form :model="form" ref="form" label-width="160px" :rules="rules">
<div class="title">
隧道配置
<el-form-item label="源设备" prop="sourceId">
<el-select
v-model="value1"
placeholder="请选择"
@change="change1"
>
<el-option
v-for="item in sourceId"
:key="item['res-id']"
:label="item.name"
:value="item['res-id']"
:disabled="item['res-id'] === value2"
>
</el-option>
</el-select>
</el-form-item>
<hr />
<el-row>
<el-form-item label="源端点IP类型" prop="IPtype">
<el-input v-model="value" type="text"></el-input>
<!-- <el-select v-model="value" placeholder="请选择">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select> -->
</el-form-item>
<el-form-item label="源端点IP地址" prop="ipname">
<el-input v-model="ipname1"></el-input>
</el-form-item>
</el-row>
<hr />
<el-form-item label="目的设备" prop="destinationId">
<el-select
v-model="value2"
placeholder="请选择"
@change="change2"
>
<el-option
v-for="item in destinationId"
:key="item['res-id']"
:label="item.name"
:value="item['res-id']"
:disabled="item['res-id'] === value1"
>
</el-option>
</el-select>
</el-form-item>
<hr />
<el-row>
<el-form-item label="目的节点IP类型" prop="IPtype">
<el-input v-model="value" type="text"></el-input>
<!-- <el-select v-model="value3" placeholder="请选择">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
>
</el-option>
</el-select> -->
</el-form-item>
<el-form-item label="目的节点IP地址" prop="sourceIp2">
<el-input v-model="ipname2"></el-input>
</el-form-item>
</el-row>
<hr />
<el-form-item label="自动生成隧道名称">
<el-input v-model="appName" disabled></el-input>
</el-form-item>
<el-form-item label="隧道ID" prop="destNode">
<el-input v-model="destNode"></el-input>
</el-form-item>
<el-form-item label="隧道别名" prop="othername">
<el-input v-model="othername"></el-input>
</el-form-item>
<el-form-item label="描述" prop="text">
<el-input v-model="text"></el-input>
</el-form-item>
</div>
<hr />
</el-form>
<!-- 按钮-->
<el-row>
<el-col :span="2" :offset="9">
<el-button type="primary" style="width: 100%" @click="submit"
>创建</el-button
>
</el-col>
<el-col :span="2" :offset="2">
<el-button style="width: 100%" @click="reset">重置</el-button>
</el-col>
</el-row>
</div>
</dv-border-box-10>
</el-dialog>
</template>
<script>
import guid from "../../../utils/guid";
import formatter from "../../../utils/formatter";
import apis from "../../../apis";
export default {
name: "Tunnel",
data() {
return {
form: {
"tunnel-id": "",
"tunnel-number": "",
"tunnel-name": "",
"tunnel-alis-name": "",
description: "",
createTime: "",
updateTime: "",
"mpls-te-tunnel-id": "",
"source-point": {
"src-node-id": "",
"node-name": "",
"point-ip": "",
"point-ip-family": "",
"point-mask": "",
},
"destination-point": {
"src-node-id": "",
"node-name": "",
"point-ip": "",
"point-ip-family": "",
"point-mask": "",
},
},
mask1: "",
mask2: "",
text: "",
othername: "",
destNode: "",
value1: "",
appName: "",
sourceId: [],
newNode: [],
destinationId: [],
value2: "",
value: "IPv4",
value3: "",
sourceIp1: "",
sourceIp2: "",
ipname1: "",
ipname2: "",
nodename1: "",
nodename2: "",
options: [
{
value: "IPv4",
label: "",
},
],
dialogVisible: false,
rendered: false,
chartBackgroundColor: "rgba(0,0,0,0.7)",
rules: {
},
};
},
computed: {
node() {
return this.$store.getters.node;
},
nodeEnable() {
return this.$store.getters.nodeEnable;
},
tunnel() {
return this.$store.getters.tunnel;
},
},
watch: {
node(_new) {
this.sourceId = _new;
this.destinationId = _new;
console.log(this.sourceId);
},
tunnel(_new) {
this.oldtunnel = _new;
console.log(this.oldtunnel);
},
},
methods: {
submit(value) {
this.$refs.form.validate((valid) => {
if (valid) {
this.form["tunnel-id"] = this.form["tunnel-id"] ? this.form["tunnel-id"] : guid.createGuid();
this.form.createTime = formatter.date(new Date());
this.form.updateTime = formatter.date(new Date());
this.form["tunnel-number"] = parseInt(this.destNode);
this.form["mpls-te-tunnel-id"] = parseInt(this.destNode);
this.form["tunnel-name"] = this.appName;
this.form["tunnel-alis-name"] = this.othername;
this.form["description"] = this.text;
this.form["destination-point"]["node-name"] = this.nodename2;
this.form["destination-point"]["point-ip-family"] = this.value;
this.form["destination-point"]["point-ip"] = this.ipname2;
this.form["destination-point"]["src-node-id"] = this.value2;
this.form["destination-point"]["point-mask"] = this.mask2;
this.form["source-point"]["node-name"] = this.nodename1;
this.form["source-point"]["point-ip-family"] = this.value3;
this.form["source-point"]["point-ip"] = this.ipname1;
this.form["source-point"]["src-node-id"] = this.value1;
this.form["source-point"]["point-mask"] = this.mask1;
let southData = JSON.parse(JSON.stringify(this.form));
let flag = false;
for (let i = 0; i < this.oldtunnel.length; i++) {
if (this.oldtunnel[i]["tunnel-name"] === this.form["tunnel-name"]) {
flag = true;
break;
}
}
if (flag) {
this.$message.warning("已存在相同隧道");
} else {
apis
.getNodeSsh(this.form["source-point"]["src-node-id"])
.then((res) => {
let ssh = res.data.server[0];
southData["ssh-connect"] = {
ip: ssh.ip,
port: ssh.port,
ipFamily: ssh["ip-family"],
userName: ssh["current-ssh-user"].userName,
password: ssh["current-ssh-user"].password,
encryptType: ssh["current-ssh-user"].encryptType,
};
this.$axios
.all([
apis.updateTunnel(this.form["tunnel-id"], {
tunnel: this.form,
}),
apis.updateTunnelSouth({ tunnels: [southData] }),
])
.then(
this.$axios.spread((res, resSouth) => {
apis.getTunnel().then((res) => {
this.$store.commit(
"updateTunnel",
res.data["tunnel-config"].tunnel
);
this.cancel();
this.reset();
this.$message.success("创建成功");
});
})
)
.catch((err) => {
if (err.response) {
this.$message.error(
err.response.status + " " + err.response.statusText
);
} else {
this.$message.error("Timeout");
}
});
});
}
}
});
},
reset() {
this.value1 = "";
this.value = "";
this.ipname1 = "";
this.value2 = "";
this.value3 = "";
this.ipname2 = "";
this.appName = "";
this.destNode = "";
this.othername = "";
this.text = "";
this.$refs.form.resetFields();
},
show() {
this.dialogVisible = true;
if (this.rendered) {
this.$refs.dv.initWH();
}
this.rendered = true;
},
cancel() {
this.dialogVisible = false;
this.reset();
},
change1(value) {
let tar = this.nodeEnable.find((element) => element["node-id"] === value);
if (tar) {
this.nodename1 = this.node.find(
(element) => element["res-id"] === value
).name;
this.ipname1 = tar["default-virtual-intf"].ip;
this.appName =
this.nodename1 +
"@" +
this.ipname1 +
"=" +
this.nodename2 +
"@" +
this.ipname2;
this.mask1 = tar["default-virtual-intf"].mask;
} else {
console.log("配置未找到");
}
},
change2(value) {
let tar = this.nodeEnable.find((element) => element["node-id"] === value);
if (tar) {
this.nodename2 = this.node.find(
(element) => element["res-id"] === value
).name;
this.ipname2 = tar["default-virtual-intf"].ip;
this.appName =
this.nodename1 +
"@" +
this.ipname1 +
"=" +
this.nodename2 +
"@" +
this.ipname2;
this.mask2 = tar["default-virtual-intf"].mask;
} else {
console.log("配置未找到");
}
},
},
};
</script>
<style scoped>
.title {
margin-bottom: 20px;
margin-top: 20px;
font-size: 24px !important;
font-weight: bold !important;
}
.el-icon-close {
position: absolute;
right: 30px;
font-size: 16px;
cursor: pointer;
}
.el-col-12 {
width: 90%;
}
.el-input {
width: 350px;
}
/deep/ .el-dialog {
background-color: transparent;
}
/deep/ .el-dialog__body {
padding: 0;
background-color: transparent;
}
/deep/ .el-tabs__item {
font-size: 24px !important;
}
/deep/ .el-input__inner {
background-color: transparent !important;
}
/deep/ .el-textarea__inner {
background-color: transparent !important;
}
</style>