添加账号密码录入&添加服务设备状态&优化代码

This commit is contained in:
wu
2022-08-20 00:11:27 +08:00
parent fd060438bf
commit cf05ec7653
4 changed files with 252 additions and 90 deletions

View File

@@ -25,7 +25,7 @@
<div style="position:relative;">
<div style="display:inline-block;">{{ item.name }}</div>
<div
:class="{ 'device-status': true, 'device-status-online': item.status === 1, 'device-status-offline': item.status === 0 }">
:class="{ 'device-status': true, 'device-status-online': item.status, 'device-status-offline': !item.status }">
</div>
</div>
</el-menu-item>
@@ -54,6 +54,21 @@
</el-card>
</el-main>
<el-dialog title="服务账号登录" :visible.sync="dialogFormVisible" :modal="false">
<el-alert title="请先输入该服务的账号密码,再进行后续操作" type="warning" style="width:400px;display: inline-block;vertical-align: middle; margin-left: 5px;" :closable="false" />
<el-form :model="userForm" :rules="userRules" ref="userForm">
<el-form-item label="账号" prop="username">
<el-input v-model="userForm.username"></el-input>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" v-model="userForm.password"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="primary" @click="setUserInfo"> </el-button>
</div>
</el-dialog>
</el-container>
</template>
@@ -284,6 +299,19 @@ export default {
'@RequestInterval': [{ required: true, message: '请输入采集周期(ms)', trigger: 'blur' }],
'@Length': [{ required: true, message: '请输入长度', trigger: 'blur' }]
},
dialogFormVisible: false,
userForm: {
username: '',
password: ''
},
userRules: {
username: [
{ required: true, message: '请输入账号', trigger: 'blur' }
],
password: [
{ required: true, message: '请输入密码', trigger: 'blur' }
]
},
serverData: {},
devicePointData: [],
nodeCodeData: [],
@@ -301,6 +329,9 @@ export default {
handler (val) {
this.serverData = val
this.getDevice()
if (val.id) {
this.getDeviceStatus()
}
},
immediate: true
}
@@ -433,14 +464,13 @@ export default {
}
},
addDevice () {
const that = this
this.$prompt('输入设备名称', '新加设备', {
confirmButtonText: '确定',
inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
inputErrorMessage: '请输入设备名称'
}).then(({ value }) => {
this.$api.ADD_DEVICE({ action: 'add_device', name: value, server_id: this.serverData.id })
that.getDevice()
this.getDevice()
this.$message({
message: '新加设备成功',
type: 'success'
@@ -448,7 +478,10 @@ export default {
})
},
async delDevice () {
const deviceConfigure = await this.$api.GET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, 'admin', '123456')
// 校验是否已经有账号和密码
const userInfo = this.getUserInfo()
const deviceConfigure = await this.$api.GET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, userInfo.username, userInfo.password)
let deviceNode = deviceConfigure.Content.Settings.GroupNode[0].DeviceNode
if (deviceNode !== undefined && isArray(deviceNode)) {
deviceNode = filter(deviceNode, item => {
@@ -460,7 +493,7 @@ export default {
}
try {
await this.$api.SET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, 'admin', '123456', { data: deviceConfigure.Content })
await this.$api.SET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, userInfo.username, userInfo.password, { data: deviceConfigure.Content })
await this.$api.DEL_DEVICE({ action: 'remove_device', id: this.deviceData[this.deviceActiveStatus].id })
this.$message({
message: '删除设备成功',
@@ -487,10 +520,10 @@ export default {
}
this.formOptions.saveLoading = false
},
async delDevicePoint ({ index, row }, done) {
this.devicePointData.splice(index, 1)
this.$refs.deviceConfigure.deviceConfigureModelValue.RequestNode = this.devicePointData
await this.setDeviceConfigure()
done()
},
@@ -507,7 +540,17 @@ export default {
this.setDeviceConfigure()
done()
},
setUserInfo () {
this.$refs.userForm.validate((valid) => {
if (valid) {
sessionStorage.setItem(this.serverData.id, JSON.stringify(this.userForm))
this.dialogFormVisible = false
}
})
},
async setDeviceConfigure () {
// 校验是否已经有账号和密码
const userInfo = this.getUserInfo()
try {
// 验证表单
this.$refs.deviceConfigure.$refs.form.validate((valid) => {
@@ -515,10 +558,11 @@ export default {
return false
}
})
const deviceConfigure = await this.$api.GET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, 'admin', '123456')
const deviceConfigure = await this.$api.GET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, userInfo.username, userInfo.password)
const deviceConfigureModelValue = this.$refs.deviceConfigure.deviceConfigureModelValue
if (this.devicePointData.length > 0) {
const devicePointData = this.devicePointData
each(devicePointData, (item) => {
let devicePointData = this.devicePointData
devicePointData = each(devicePointData, (item) => {
unset(item, 'id')
unset(item, 'showBindButton')
unset(item, 'showCopyButton')
@@ -526,9 +570,9 @@ export default {
unset(item, 'showSendButton')
item['@Binding'] = item['@Binding'] || ''
})
this.$refs.deviceConfigure.deviceConfigureModelValue.RequestNode = devicePointData
deviceConfigureModelValue.RequestNode = devicePointData
}
this.$refs.deviceConfigure.deviceConfigureModelValue['@Name'] = this.deviceData[this.deviceActiveStatus].name
deviceConfigureModelValue['@Name'] = this.deviceData[this.deviceActiveStatus].name
let deviceNode = deviceConfigure.Content.Settings.GroupNode[0].DeviceNode || []
let isExist = false
if (deviceNode !== undefined) {
@@ -536,21 +580,21 @@ export default {
deviceNode = map(deviceNode, (item) => {
if (item['@Name'] === this.deviceData[this.deviceActiveStatus].name) {
isExist = true
item = this.$refs.deviceConfigure.deviceConfigureModelValue
item = deviceConfigureModelValue
}
return item
})
}
if (!isExist) {
deviceNode.push(this.$refs.deviceConfigure.deviceConfigureModelValue)
deviceNode.push(deviceConfigureModelValue)
}
deviceConfigure.Content.Settings.GroupNode[0].DeviceNode = deviceNode
await this.$api.SET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, 'admin', '123456', { data: deviceConfigure.Content })
this.$refs.deviceConfigure.deviceConfigureModelValue.DeviceTypeName = this.$refs.deviceConfigure.defaultDeviceTypeNameValue
await this.$api.SET_HSLSERVER_CONFIGURE('http://' + this.serverData.url + ':' + this.serverData.port, userInfo.username, userInfo.password, { data: deviceConfigure.Content })
deviceConfigureModelValue.DeviceTypeName = this.$refs.deviceConfigure.defaultDeviceTypeNameValue
const data = {
action: 'update_device',
conf: JSON.stringify(this.$refs.deviceConfigure.deviceConfigureModelValue),
conf: JSON.stringify(deviceConfigureModelValue),
id: this.deviceData[this.deviceActiveStatus].id
}
this.$api.SET_DEVICE_CONFIGURE(data)
@@ -558,25 +602,45 @@ export default {
message: '操作成功',
type: 'success'
})
this.$emit('changeServerConfig', 't')
} catch (e) {
console.log(e)
}
},
getDeviceStatus () {
this.$api.GET_DEVICE_STATUS(this.serverData.id).then(res => {
each(this.deviceData, (item) => {
if (item.id in res.device_status) {
item.status = res.device_status[item.id]
getUserInfo () {
const userInfo = sessionStorage.getItem(this.serverData.id)
if (!userInfo) {
this.dialogFormVisible = true
return false
}
return JSON.parse(userInfo)
},
async getDeviceStatus () {
const userInfo = this.getUserInfo()
const res = await this.$api.GET_SERVER_DEVICE_STATUS('http://' + this.serverData.url + ':' + this.serverData.port, userInfo.username, userInfo.password)
if (res) {
this.$emit('changeStatus', 'online')
const deviceStatus = {}
each(res.Content.__deviceList, (item) => {
deviceStatus[item.__name] = {
status: item.__requestEnable,
name: item.__name
}
})
this.$emit('changeStatus', res.server_status)
})
each(this.deviceData, (item) => {
if (deviceStatus[item.name]) {
item.status = deviceStatus[item.name].status
}
})
} else {
this.$emit('changeStatus', false)
}
}
},
mounted () {
this.getDevice()
this.getworkingSubclasses()
// setInterval(this.getDeviceStatus, 2000)
this.getDeviceStatus()
}
}
</script>