add-or-update.vue 12 KB
Newer Older
geqilin's avatar
geqilin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
<template>
  <el-dialog :close-on-click-modal="false" :visible.sync="visible" append-to-body :modal-append-to-body='false'>
    <div slot="title" class="title-bold">{{id ? '编辑设备': '新增设备'}}</div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="formLabelWidth" v-loading="loading">
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备所属层级" prop="level">
              <el-select v-model="dataForm.level" style='width:100%;' placeholder="请选择" autocomplete="off" clearable @change="changeHandle" disabled>
                <el-option v-for="item in optionLevel" :key="item.value" :label="item.label" :value="item.value" ></el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="所属节点名称" prop="stationId">
            <el-input style="width:100%" v-model="jdName" disabled placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备名称" prop="name">
            <el-input style="width:100%" v-model="dataForm.name" placeholder="请输入设备名称" autocomplete="off" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编码" prop="code">
            <el-input style="width:100%" v-model="dataForm.code" placeholder="请输入设备编码" autocomplete="off" clearable></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="设备类型" prop="type">
            <el-select  style='width:100%;'  :disabled="typeDisabled" v-model="dataForm.type" placeholder="请选择设备类型" clearable>
              <el-option
                v-for="item in optionType"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备IP地址" prop="specifications">
            <el-input style="width:100%" v-model="dataForm.specifications" placeholder="请输入设备IP地址" autocomplete="off" clearable ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="端口" prop="port">
            <el-input style="width:100%" v-model="dataForm.port" placeholder="请输入端口" autocomplete="off" type="number" clearable ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产厂家" prop="factory">
            <el-input style="width:100%" v-model="dataForm.factory" placeholder="请输入生产厂家" autocomplete="off" clearable ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="部署地址" prop="deployAddress">
            <el-input style="width:100%" v-model="dataForm.deployAddress" placeholder="请输入部署地址" autocomplete="off" clearable ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input style="width:100%" type="textarea" placeholder="请输入备注" v-model="dataForm.remark" rows="5" resize="none" ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" size='medium' @click="cancel">取 消</el-button>
      <el-button type="primary" size='medium'  @click="saveHandling" v-prevent-re-click>保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    let validateIpUnique = (rule, value, callback) => {
             this.$http({
               url: this.$http.adornUrl(`/eqEquipment/queryIpUnique`),
               method: 'get',
               params: {
                 ip: this.dataForm.specifications,
                 stationId: this.dataForm.stationId
               }
             }).then(data => {
                   if (data && data.code === 0) {
                       if (data.bean) {
                         if (this.id) {
                           if (this.dataForm.id !== data.bean.id) {
                             callback(new Error('ip已存在'))
                           } else {
                             callback()
                           }
                         } else {
                           callback(new Error('ip已存在'))
                         }
                       } else {
                         callback()
                       }
                   }
             })
    }

    let nameUnique = (rule, value, callback) => {
      this.$http({
        url: this.$http.adornUrl(`/eqEquipment/queryNameUnique`),
        method: 'get',
        params: {
          name: this.dataForm.name,
          stationId: this.dataForm.stationId
        }
      }).then(data => {
        if (data && data.code === 0) {
          if (data.bean) {
            if (this.id) {
              if (this.dataForm.id !== data.bean.id) {
                callback(new Error('设备名称已存在'))
              } else {
                callback()
              }
            } else {
              callback(new Error('设备名称已存在'))
            }
          } else {
            callback()
          }
        }
      })
    }

    return {
      id: null,
      visible: true,
      formLabelWidth: '120px',
      dataForm: {},
      jdName: '',
      loading: false,
      typeDisabled: false,
      dataRule: {
        level: [
          { required: true, message: '请选择设备所属层级', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' },
          { max: 100, message: '长度不能大于100个字符', trigger: 'blur' },
          { validator: nameUnique, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' },
          { max: 20, message: '长度不能大于20个字符', trigger: 'blur' }
        ],
        // stationId: [
        //   { required: true, message: '线路站点不能为空', trigger: 'blur' }
        // ],
        type: [
          { required: true, message: '请选择设备类型', trigger: 'change' }
        ],
        specifications: [
          { required: true, message: '设备IP地址不能为空', trigger: 'blur' },
          { max: 20, message: '长度不能大于20个字符', trigger: 'blur' },
          { validator: validateIpUnique, trigger: 'blur' }
        ],
        port: [
          { required: true, message: '端口不能为空', trigger: 'blur' },
          { pattern: /(^[1-9]\d*$)/, message: '只能输入正数', trigger: 'blur' },
          { max: 10, message: '长度不能大于10个字符', trigger: 'blur' }
        ],
        factory: [
          { max: 100, message: '长度不能大于100个字符', trigger: 'blur' }
        ],
        deployAddress: [
          { max: 100, message: '长度不能大于100个字符', trigger: 'blur' }
        ],
        remark: [
          { max: 500, message: '长度不能大于500个字符', trigger: 'blur' }
        ]
      },
      options: [],
      optionLevel: [
        {
          label: '本级设备',
          value: 1
        },
        {
          label: '外接设备',
          value: 2
        }
      ],
      stationInfo: null,
      optionType: [
        {
          label: '应用服务器',
          value: 1
        },
        {
          label: '数据库服务器',
          value: 2
        },
        {
          label: '集群服务器',
          value: 3
        }
      ]
    }
  },
  created() {
  },
  methods: {
    init(id, paramLevel) {
      //清除验证提示
      this.jdName = ''
      this.id = null
      this.dataForm = {}
      this.visible = true
      this.typeDisabled = false
      //let code = localStorage.getItem('stationId')
      //this.dataForm.byx3 = code
      this.$refs['dataForm'].resetFields()
      if (id) {
        this.id = id
        this.$http({
          url: this.$http.adornUrl(`/eqEquipment/getId/` + this.id),
          method: 'get'
        }).then(data => {
          if (data && data.code === 0) {
            this.dataForm = data.bean
            this.dataForm.stationName = this.dataForm.sname
            this.dataForm.lineName = this.dataForm.liName
            this.changeHandle(this.dataForm.level)
            if (this.dataForm.level === 2) this.typeDisabled = true
            //this.dataForm.stationId = Array.from([data.bean.lineId, data.bean.stationId])
          }
        })
      } else {
        if (paramLevel === 2) {
          this.dataForm.type = 1
          this.typeDisabled = true
        }
        this.getStationName(paramLevel)
      }
    },
    changeHandle(val) {
      if (val === 1) {
        this.jdName = this.dataForm.stationName
      } else if (val === 2) {
        this.jdName = this.dataForm.lineName
      } else {
        this.jdName = ''
      }
    },
    //获取当前站点名称
    getStationName(paramLevel) {
      this.$http({
        url: this.$http.adornUrl(`/liStation/queryId/` + localStorage.getItem('stationId')),
        method: 'get'
      }).then(data => {
        if (data && data.code === 0) {
          this.dataForm.byx3 = data.bean.id
          this.dataForm.stationName = data.bean.stationName
          this.dataForm.lineId = data.bean.lineId
          this.dataForm.lineName = data.bean.lineName
          if (paramLevel) {
            this.changeHandle(paramLevel)
            this.dataForm.level = paramLevel
          }
        }
      })
    },
    //根据用户角色权限获取站点信息
    // getStationByUserId() {
    //   this.$http({
    //     url: this.$http.adornUrl(`/liLine/getTreeListStation`),
    //     method: 'post',
    //     data: {}
    //   }).then(data => {
    //     if (data && data.code === 0) {
    //       for (let li of data.liList) {
    //         let arr = []
    //         for (let lis of data.lisList) {
    //           if (li.id === lis.lineId) {
    //             arr.push({ value: lis.id, label: lis.stationName })
    //           }
    //         }
    //         this.options.push({ value: li.id, label: li.name, children: arr })
    //       }
    //     }
    //   })
    // },
    //添加,修改
    saveHandling() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (!/(^[1-9]\d*$)/.test(this.dataForm.port)) {
            this.$message.error('端口必须为正整数!')
            return
          }
          // debugger
          //let node = this.$refs['stationOption'].getCheckedNodes()
          //this.dataForm.stationId = this.dataForm.stationId.toString()
          //this.dataForm.stationName = node[0].parent.data.label + node[0].data.label
          this.loading = true
          this.dataForm.stationId = localStorage.getItem('stationId')
          this.$http({
            url: this.$http.adornUrl(
              `/eqEquipment/${!this.dataForm.id ? 'save' : 'update'}`
            ),
            method: 'post',
            data: this.dataForm
          }).then(data => {
            if (data && data.code === 0) {
              this.$message.success('保存成功')
              this.visible = false
              this.$emit('refreshDataList')
            }
            this.loading = false
          })
        }
      })
    },
    handleChange(value) {},
    //取消
    cancel() {
      this.$confirm('确认取消?', this.id ? '编辑设备' : '新增设备', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.visible = false
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>