liuxiaolong
2019-05-06 2418a2d61feea858e9e63aa52fd64ddfe17e392a
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
<template>
  <b-modal
    size="lg"
    :title="title"
    ref="myModalRef"
    ok-title="保存"
    @ok="handleOk"
    @cancel="clearName"
    cancel-title="取消"
    :hide-header="false"
    :hide-footer="!isShowFooter"
  >
    <div>
      <!-- <h4>{{title}}</h4> -->
      <el-form
        :model="addForm"
        label-position="right"
        :rules="rules"
        ref="addForm"
        label-width="130px"
      >
        <div class="row">
          <div class="col-md-10 col-sm-12">
            <el-form-item label="输入IP地址:" prop="address">
              <el-input
                v-model="addForm.address"
                placeholder="请输入集群内任意节点的IP地址"
                @keyup.native.enter="searchForm('addForm')"
                :disabled="isDisabled"
              ></el-input>
            </el-form-item>
          </div>
          <div class="col-md-2 col-sm-12">
            <b-button variant="primary" size="md" @click="searchForm('addForm')">搜索集群</b-button>
          </div>
        </div>
        <!-- 集群信息回显 start -->
        <div class="row" v-if="colonyData">
          <div class="col-md-12 col-sm-12">
            <el-form-item label="集群名称:" prop="ip">
              <div class="colony-table">
                <h5 class="mt-3">{{colonyData.cluster_name||''}}</h5>
                <Table :data="colonyData.list ? colonyData.list : []" :border="false" :fit="true">
                  <TableColumn label="节点ID" prop="node_id"/>
                  <TableColumn label="注册时间" prop="create_time"/>
                  <TableColumn label="节点IP" prop="node_ip"/>
                  <TableColumn label="节点设备ID" prop="dev_id"/>
                  <TableColumn label="节点设备名称" prop="dev_name"/>
                </Table>
              </div>
            </el-form-item>
            <el-form-item label="输入集群密码:" prop="cookie">
              <el-input v-model="addForm.cookie" placeholder="请输入要加入集群的密码" :disabled="isDisabled"></el-input>
            </el-form-item>
          </div>
        </div>
        <div v-else class="pt30 pb30"></div>
        <div class="text-right mt-2 pt-4 border-top" v-if="colonyData">
          <b-button size="md" class="mx-2" @click="hideModel">取消</b-button>
          <b-button variant="primary" class="mx-2" size="md" @click="submitForm('addForm')">保存</b-button>
        </div>
      </el-form>
    </div>
  </b-modal>
</template>
 
<script>
import {
  Table,
  TableColumn,
  Col,
  Form,
  FormItem,
  Input,
  Checkbox,
  Radio,
  RadioGroup,
  Select,
  Option
} from 'element-ui'
import { getNodeByIp, saveCluster } from '@/server/home.js'
export default {
  props: {
    title: String,
    item: Object,
    isShowFooter: {
      default: false
    }
  },
  components: {
    Table,
    TableColumn,
    elCol: Col,
    elForm: Form,
    elFormItem: FormItem,
    elInput: Input,
    elCheckbox: Checkbox,
    elRadio: Radio,
    elRadioGroup: RadioGroup,
    elSelect: Select,
    elOption: Option
  },
  data() {
    var isIP = (rule, value, callback) => {
      if (!value) {
        return callback(new Error('IP必填'))
      }
      /* 端口 /^([0-9]|[1-9]\d|[1-9]\d{2}|[1-9]\d{3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/ */
      const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
      if (value && !reg.test(value)) {
        return callback(new Error('IP不合法'))
      } else {
        callback()
      }
    }
    return {
      name: '',
      typeList: [],
      addForm: {
        address: '',
        cookie: ''
      },
      rules: {
        address: [{ required: true, validator: isIP, trigger: 'blur' }],
        cookie: [{ required: true, message: '集群密码必填', trigger: 'blur' }]
      },
      colonyData: null
    }
  },
  computed: {
    isDisabled() {
      return this.$route.query.type === 'check'
    },
    isCamera() {
      return false
    }
  },
  methods: {
    // * 打开modal
    showModel() {
      this.$refs.myModalRef.show()
      this.$nextTick(() => {
        this.reView(this.item)
      })
    },
    hideModel() {
      this.$refs.myModalRef.hide()
    },
    /* 接口 start */
    async getColonyByIp(json) {
      const res = await getNodeByIp({
        nodeIp: json.address ? json.address : ''
      })
      if (res.success && res.code === 200 && res.data) {
        this.colonyData = res.data
      } else {
        this.colonyData = null
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '查询失败'
        })
      }
    },
    async saveColony(json) {
      const res = await saveCluster(json)
      if (res.success && res.code === 200 && res.data) {
        /* 成功操作 */
        this.$emit('submit')
        this.hideModel()
        this.$toast({
          type: 'success',
          message: res && res.msg ? res.msg : '加入集群成功'
        })
      } else {
        this.$toast({
          type: 'error',
          message: res && res.msg ? res.msg : '加入集群失败!'
        })
      }
    },
    /* 接口 end */
    searchForm(formName) {
      this.$refs[formName].validateField('address', msg => {
        if (msg) {
          console.log(`${msg},参数不合法!!`)
          return false
        } else {
          this.getColonyByIp(this.addForm)
        }
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.saveColony(this.addForm)
        } else {
          console.log('保存接口,参数不合法!!')
          return false
        }
      })
    },
 
    // 回显
    reView(item) {
      // if (item) {
      //   this.name = item.name
      // } else {
      //   this.name = ''
      // }
      this.resetForm('addForm')
      this.addForm = {
        address: '',
        cookie: ''
      }
      this.colonyData = null
    },
    // * 提交表单
    handleOk(e) {
      e.preventDefault()
      if (!this.name) {
        this.$toast({
          type: 'warning',
          message: '请输入角色名称'
        })
        return
      }
      this.$emit('submit', this.name, this.item ? this.item.id : '')
    },
    // * 取消
    clearName() {},
 
    resetForm(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" >
.colony-table {
  .el-table td {
    padding: 6px !important;
  }
  .has-gutter th {
    padding: 2px !important;
    background: transparent !important;
  }
  .el-table td,
  .el-table th.is-leaf {
    border-bottom: none !important;
  }
  .el-table::before {
    background-color: transparent;
  }
}
</style>