2021-10-15 验证form表单的内容是否已存在数据库

验证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: {
        // destNode: [{ required: true, message: "请输入隧道ID" }],
      },
    };
  },
  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));
          
          //方法一:用遍历find()命令找到符合条件的元素
          // this.tunnel === this.$store.getters.tunnel === this.oldtunnel
          // let tar = this.tunnel.find(t => t['tunnel-name'] === this.form['tunnel-name'])
          // if (tar) {
                      //如果获取到的数据库中的this.tunnel的['tunnel-name']与准备提交的this.form['tunnel-name']相同,则不进行提交下发,如果不是,进行下一步的提交
          //   // 重复
          //   this.$message.warning("已存在相同隧道");
          // } else {
          //   // 不重复
          //   apis
          //     .getNodeSsh(this.form["source-point"]["src-node-id"])
          //     .then((res) => {
          //       // 获取源设备ssh
          //       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");
          //           }
          //         });
          //     });
          // }
          // }


          //方法二:用for()循环遍历
          //首先定义一个标识符:flag    flag的值只有true和false
        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) => {
                // 获取源设备ssh
                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);
      // console.log(tar);
      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("配置未找到");
      }
    },

    //  change3(){
    //    let tar = this.oldtunnel.find((element)=>element['tunnel-name'] !== this.appName)
    //    if(tar){
    //        this.submit()
    //    }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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值