import createWs from "@/assets/js/tools/websocket/createWs";
|
import {onMounted, ref} from "vue";
|
import door from "@/assets/js/const/door";
|
import getLabelByKey from "@/assets/js/tools/getLabelByKey";
|
import {changeDoorState, searchDoorInfoApi} from "@/views/accessControl/js/api";
|
import {ElLoading, ElMessage} from "element-plus";
|
const lockStatusList = door.lockStatus;
|
const doorInfoModule = ()=>{
|
const {
|
SOCKET
|
} = createWs("carCameraRtSocket");
|
|
const doorInfos = ref([
|
{
|
carcameraId: 8001,
|
carcameraName: "智能实验室",
|
ip: "192.168.0.18",
|
num: 1,
|
password: "a1234567.",
|
port: 8000,
|
status: 0,
|
statusText: "正常关",
|
userName: "admin"
|
}
|
]);
|
|
|
const searchDoorInfo = async ()=>{
|
try{
|
const res = await searchDoorInfoApi();
|
const rs = res.data;
|
if(rs.code === 1 && rs.data) {
|
doorInfos.value = rs.data2.map(item=>{
|
item.status = -1;
|
item.statusText = "未知";
|
return item;
|
});
|
}
|
}catch (e) {
|
console.log(e);
|
}
|
}
|
const handleOpen = ()=>{
|
SOCKET.value.send(JSON.stringify(doorInfos.value));
|
}
|
|
const handleMessage = (res)=>{
|
let rs = JSON.parse(res.data);
|
if(rs.code ===1 && rs.data) {
|
let data = rs.data2;
|
doorInfos.value.map(item=>{
|
for(let i=0; i<data.length; i++) {
|
if(item.carcameraId === data[i].carcameraId) {
|
const lockStatus = data[i].lockStatus;
|
item.status = lockStatus;
|
item.statusText = getLabelByKey(lockStatus, lockStatusList, "未知");
|
}
|
}
|
});
|
}
|
}
|
/**
|
* 开启门禁
|
*/
|
const openDoor = async (info)=>{
|
const loading = ElLoading.service({
|
lock: false,
|
text: '开启门禁中...',
|
background: 'rgba(0, 0, 0, 0.3)',
|
});
|
try {
|
const res = await changeDoorState(info, 1);
|
loading.close();
|
let rs = res.data;
|
if(rs.code === 1 && rs.data) {
|
ElMessage({
|
showClose: true,
|
message: '开启门禁成功',
|
type: 'success',
|
});
|
}else {
|
ElMessage({
|
showClose: true,
|
message: '开启门禁失败',
|
type: 'error',
|
});
|
}
|
}catch (error){
|
console.log(error);
|
loading.close();
|
}
|
|
}
|
|
/**
|
* 关闭门禁
|
*/
|
const closeDoor = async (info)=>{
|
const loading = ElLoading.service({
|
lock: false,
|
text: '关闭门禁中...',
|
background: 'rgba(0, 0, 0, 0.3)',
|
});
|
try {
|
const res = await changeDoorState(info, 0);
|
loading.close();
|
let rs = res.data;
|
if(rs.code === 1 && rs.data) {
|
ElMessage({
|
showClose: true,
|
message: '关闭成功',
|
type: 'success',
|
});
|
}else {
|
ElMessage({
|
showClose: true,
|
message: '关闭失败',
|
type: 'error',
|
});
|
}
|
}catch (error){
|
console.log(error);
|
loading.close();
|
}
|
}
|
|
onMounted(()=>{
|
searchDoorInfo();
|
|
SOCKET.value.addEventListener("open", handleOpen, false);
|
SOCKET.value.addEventListener("message", handleMessage, false);
|
});
|
|
return {
|
doorInfos,
|
openDoor,
|
closeDoor
|
};
|
}
|
|
export default doorInfoModule;
|