调整ws的调用方式

This commit is contained in:
2023-02-16 11:34:36 +08:00
parent 28856c980c
commit 0c65313b35

View File

@@ -6,10 +6,12 @@
<ai-search-bar>
<template #left>
<el-button type="primary" icon="iconfont iconAdd" @click="handleAdd()">添加</el-button>
<ai-select placeholder="系统类型" v-model="search.type" :selectList="dict.getDict('systemType')" @change="page.current=1,getTableData()"/>
<ai-select placeholder="系统类型" v-model="search.type" :selectList="dict.getDict('systemType')"
@change="page.current=1,getTableData()"/>
</template>
<template #right>
<el-input size="small" placeholder="搜索" v-model="search.name" clearable @change="page.current=1,getTableData()"/>
<el-input size="small" placeholder="搜索" v-model="search.name" clearable
@change="page.current=1,getTableData()"/>
</template>
</ai-search-bar>
<ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
@@ -108,31 +110,10 @@ export default {
},
handleUpdate(row) {
let {id} = row
if (!this.ws[id]) {
this.ws[id] = new WebSocket(`ws://192.168.1.87:12525/custom/getZip?id=${id}`)
this.ws[id].onmessage = res => {
if (res?.data) {
const data = JSON.parse(res.data)
if (data.code == '0') {
row.count = data.progress
if (row.count == 100) {
row.count = 0
this.$message.success("打包成功!")
this.refreshRow(row, data.remark)
}
} else if (data.code == 1) {
row.count = 0
this.$message.error("打包失败!")
this.refreshRow(row, data.row)
}
}
}
this.ws[id].onclose = () => {
row.count = 0
this.ws[id].close()
delete this.ws[id]
}
} else this.ws[id]?.send(JSON.stringify({id}))
if (!this.ws) {
this.initWs()
}
this.ws?.send(JSON.stringify({action: "/custom/getZip", id}))
},
refreshRow(row, v) {
row.error = v.error
@@ -146,9 +127,10 @@ export default {
},
handleCancelUpdate(row) {
let {id} = row
this.ws[id].send(JSON.stringify({
cid: id
}))
if (!this.ws) {
this.initWs()
}
this.ws.send(JSON.stringify({action: "/custom/getZip", cid: id}))
},
getRowById(id) {
return this.instance.post("/node/custom/detail", null, {
@@ -165,13 +147,38 @@ export default {
this.$message.success("消息发送成功!")
}
})
},
initWs() {
this.ws = new WebSocket(`ws://192.168.1.87:12525/ws`)
this.ws.onmessage = res => {
if (res?.data) {
const data = JSON.parse(res.data),
row = this.tableData.find(e => e.id == data?.row.id)
if (data.code == '0') {
row.count = data.progress
if (row.count == 100) {
row.count = 0
this.$message.success("打包成功!")
this.refreshRow(row, data.row)
}
} else if (data.code == 1) {
row.count = 0
this.$message.error("打包失败!")
this.refreshRow(row, data.row)
}
}
}
this.ws.onclose = () => {
this.tableData.map(e => e.count = 0)
this.ws.close()
}
}
},
created() {
this.getTableData()
},
beforeDestroy() {
Object.values(this.ws).map(t => t.close())
this.ws?.close()
}
}
</script>