优化代码

This commit is contained in:
java110 2022-01-05 10:42:43 +08:00
parent a94de23b6b
commit 2b685cc3de
2 changed files with 57 additions and 57 deletions

View File

@ -1,7 +1,7 @@
/**
入驻小区
**/
(function (vc) {
(function(vc) {
vc.extends({
data: {
parkingAreaControlVideoInfo: {
@ -13,11 +13,11 @@
}
},
_initMethod: function () {
_initMethod: function() {
},
_initEvent: function () {
vc.on('parkingAreaControlVideo', 'notify', function (param) {
_initEvent: function() {
vc.on('parkingAreaControlVideo', 'notify', function(param) {
if (param.hasOwnProperty('boxId')) {
$that.parkingAreaControlVideoInfo.boxId = param.boxId;
$that._listMachines();
@ -25,21 +25,21 @@
})
},
methods: {
_listMachines: function () {
_listMachines: function() {
let param = {
params: {
locationObjId: $that.parkingAreaControlVideoInfo.boxId,
page: 1,
row: 100,
machineTypeCd: '9996',
communityId: vc.getCurrentCommunity().communityId
params: {
locationObjId: $that.parkingAreaControlVideoInfo.boxId,
page: 1,
row: 100,
machineTypeCd: '9996',
communityId: vc.getCurrentCommunity().communityId
}
}
}
//发送get请求
//发送get请求
vc.http.get('machineManage',
'list',
param,
function (json, res) {
function(json, res) {
let _machineManageInfo = JSON.parse(json);
let _machines = _machineManageInfo.machines;
_machines.forEach(item => {
@ -50,12 +50,13 @@
}
});
}, function (errInfo, error) {
},
function(errInfo, error) {
console.log('请求失败处理');
}
);
},
_swatchVedio: function () {
_swatchVedio: function() {
//创建一个socket实例
let wsUrl = "";
let _enterMachineId = $that.parkingAreaControlVideoInfo.inMachineId;
@ -75,13 +76,13 @@
wsUrl = wsUrl.replace(':8131', ':9080');
let _protocol = window.location.protocol;
if (_protocol.startsWith('https')) {
wsUrl =
"wss://" + wsUrl;
} else {
wsUrl =
"ws://" + wsUrl;
}
// if (_protocol.startsWith('https')) {
// wsUrl =
// "wss://" + wsUrl;
// } else {
wsUrl =
"ws://" + wsUrl;
// }
let image = document.getElementById("receiver1");
if (wsUrl.endsWith(".flv")) {
image = document.getElementById("receiver1Div");
@ -90,16 +91,16 @@
videoBuffer: 0.2,
isResize: false,
});
jessibuca.onLoad = function () {
jessibuca.onLoad = function() {
this.play(wsUrl);
};
return;
}
let receiver_socket = new WebSocket(wsUrl);
// 监听消息
receiver_socket.onmessage = function (data) {
receiver_socket.onmessage = function(data) {
let reader = new FileReader();
reader.onload = function (evt) {
reader.onload = function(evt) {
if (evt.target.readyState == FileReader.DONE) {
let url = evt.target.result;
image.src = "data:image/png;" + url;
@ -108,7 +109,7 @@
reader.readAsDataURL(data.data);
};
},
_swatchOutVedio: function () {
_swatchOutVedio: function() {
//创建一个socket实例
let wsUrl = "";
let _outMachineId = $that.parkingAreaControlVideoInfo.outMachineId;
@ -150,17 +151,17 @@
videoBuffer: 0.2,
isResize: false,
});
jessibuca.onLoad = function () {
jessibuca.onLoad = function() {
this.play(wsUrl);
};
return;
}
let receiver_socket = new WebSocket(wsUrl);
// 监听消息
receiver_socket.onmessage = function (data) {
receiver_socket.onmessage = function(data) {
//image.src = 'data:image/png;' + data.data;
let reader = new FileReader();
reader.onload = function (evt) {
reader.onload = function(evt) {
if (evt.target.readyState == FileReader.DONE) {
let url = evt.target.result;
image.src = "data:image/png;" + url;
@ -169,7 +170,7 @@
reader.readAsDataURL(data.data);
};
},
_openDoor: function (_inOut) {
_openDoor: function(_inOut) {
let _machines = [];
let _machineId = "";
if (_inOut == 'in') {
@ -199,11 +200,10 @@
"communityId": vc.getCurrentCommunity().communityId
};
vc.http.apiPost('/machine/openDoor',
JSON.stringify(_data),
{
JSON.stringify(_data), {
emulateJSON: true
},
function (json, res) {
function(json, res) {
let _data = JSON.parse(json);
if (_data.code != 0) {
vc.toast(_data.msg);
@ -211,12 +211,12 @@
vc.toast('已请求设备');
}
},
function (errInfo, error) {
function(errInfo, error) {
console.log('请求失败处理');
vc.toast(json);
});
},
customCarIn: function (_type) {
customCarIn: function(_type) {
let _machineId = $that.parkingAreaControlVideoInfo.inMachineId;
if (_type != '1101') {
_machineId = $that.parkingAreaControlVideoInfo.outMachineId;

View File

@ -1,7 +1,7 @@
/**
入驻小区
**/
(function (vc) {
(function(vc) {
vc.extends({
data: {
parkingAreaControlInfo: {
@ -12,38 +12,38 @@
}
},
_initMethod: function () {
_initMethod: function() {
$that.parkingAreaControlInfo.boxId = vc.getParam('boxId');
$that._initParkingAreaWs();
vc.emit('parkingAreaControlVideo', 'notify', {
boxId: $that.parkingAreaControlInfo.boxId
});
},
_initEvent: function () {
vc.on('parkingAreaControl', 'notify', function (_param) {
_initEvent: function() {
vc.on('parkingAreaControl', 'notify', function(_param) {
vc.copyObject(_param, $that.parkingAreaControlInfo);
})
},
methods: {
changeTab: function (_tab) {
changeTab: function(_tab) {
$that.parkingAreaControlInfo._currentTab = _tab;
vc.emit(_tab, 'switch', {
boxId: $that.parkingAreaControlInfo.boxId
})
},
_initParkingAreaWs: function () {
_initParkingAreaWs: function() {
let clientId = vc.uuid();
let heartCheck = {
timeout: 30000, // 9分钟发一次心跳比server端设置的连接时间稍微小一点在接近断开的情况下以通信的方式去重置连接时间。
timeout: 30000, // 9分钟发一次心跳比server端设置的连接时间稍微小一点在接近断开的情况下以通信的方式去重置连接时间。
serverTimeoutObj: null,
pingTime: new Date().getTime(),
reset: function () {
reset: function() {
clearTimeout(this.serverTimeoutObj);
return this;
},
start: function () {
start: function() {
let self = this;
this.serverTimeoutObj = setInterval(function () {
this.serverTimeoutObj = setInterval(function() {
if (websocket.readyState == 1) {
console.log("连接状态,发送消息保持连接");
let _pingTime = new Date().getTime();
@ -54,7 +54,7 @@
websocket.send("{'cmd':'ping'}");
self.pingTime = _pingTime;
heartCheck.reset().start(); // 如果获取到消息,说明连接是正常的,重置心跳检测
heartCheck.reset().start(); // 如果获取到消息,说明连接是正常的,重置心跳检测
} else {
console.log("断开状态,尝试重连");
$that._initParkingAreaWs();
@ -70,12 +70,12 @@
"wss://" + window.location.host + "/ws/parkingArea/" +
$that.parkingAreaControlInfo.boxId + "/" + clientId;
} else {
// url =
// "ws://" + window.location.host + "/ws/parkingArea/" +
// $that.parkingAreaControlInfo.paId + "/" + clientId;
url =
"ws://demo.homecommunity.cn:9011/ws/parkingArea/" +
$that.parkingAreaControlInfo.boxId + "/" + clientId;
"ws://" + window.location.host + "/ws/parkingArea/" +
$that.parkingAreaControlInfo.paId + "/" + clientId;
// url =
// "ws://demo.homecommunity.cn:9011/ws/parkingArea/" +
// $that.parkingAreaControlInfo.boxId + "/" + clientId;
}
@ -88,7 +88,7 @@
}
//连接发生错误的回调方法
websocket.onerror = function (_err) {
websocket.onerror = function(_err) {
console.log("初始化失败", _err);
this.$notify.error({
title: "错误",
@ -97,13 +97,13 @@
};
//连接成功建立的回调方法
websocket.onopen = function () {
websocket.onopen = function() {
heartCheck.reset().start();
console.log("ws初始化成功");
};
//接收到消息的回调方法
websocket.onmessage = function (event) {
websocket.onmessage = function(event) {
heartCheck.reset().start();
console.log("event", event);
let _data = event.data;
@ -121,7 +121,7 @@
};
//连接关闭的回调方法
websocket.onclose = function () {
websocket.onclose = function() {
console.log("初始化失败");
//$that._initParkingAreaWs();
this.$notify.error({
@ -131,7 +131,7 @@
};
//监听窗口关闭事件当窗口关闭时主动去关闭websocket连接防止连接还没断开就关闭窗口server端会抛异常。
window.onbeforeunload = function () {
window.onbeforeunload = function() {
websocket.close();
};
}