21.
[4] [新会话] @老虎会游泳,在的,您好!有什么问题我可以帮您解答吗?
22.
@ChatGPT,优化下列代码;使其更加易读简洁:
var geomeAata = [], dList = [];
Promise.all([
fetch(`${top.apiServer}/iotapi/classes/Device?skip=0&order=-createdAt&count=objectId&where=%7B%7D`, fetchOptions).then((res) => isSuccess(res)),
fetch(`/iotapi/locationlist/${top.userInfo.objectId}`, fetchOptions).then((res) => isSuccess(res)),
]).then(allResponses => {
const response1 = allResponses[0];
const response2 = allResponses[1];
//解析设备数量卡片数据
dataPack = {
deviceNumber: response1.count,
onlineNumber: 0,
offlineNumber: 0,
alarmNumber: 0,
alarmedNumber: 0,
unalarmNumber: 0,
typeNumber: 0,
deviceAdded: 0,
orderNumber: 0,
orderEd: 0,
unOrderEd: 0,
product: []
};
if (response1.count != 0) {
$.each(response1.results, function (index, value) {
if (dataPack.product.indexOf(value.product.objectId) == -1) {
dataPack.product.push(value.product.objectId);
}
devType["p_" + value.product.objectId].value += 1;
if (value.status == 'ONLINE') {
dataPack.onlineNumber += 1;
}
try {
dList['d_' + value.objectId] = { name: value.name, devaddr: value.devaddr, status: value.status ,latlag:{lat:value.latitude,lng:value.longitude}};
} catch (error) {
dList['d_' + value.objectId] = { name: value.name, devaddr: value.devaddr, status: "OFFLINE" };
}
})
console.log(dList);
top.localforage.setItem('device', dList);
$.each(response2.result, function (index, value) {
var status = dList['d_' + value.deviceid].status;
var dname = dList['d_' + value.deviceid].name;
var devaddr = dList['d_' + value.deviceid].devaddr;
if (value.isGPS == "0") {
var data = {
styleId: status,
position: new TMap.LatLng(value.latitude, value.longitude),
properties: {
title: "marker" + index,
htmlConetent: `<div class ="wininfo">
<span class =" ${status == "ONLINE" ? "dev-online" : "dev-offline"} ">
${status == "ONLINE" ? "在线" : "离线"}
</span>
<div class="devname" title="${dname}">${dname} </div >
<div class ="devid" title="${devaddr}">设备编号:${devaddr} </div>
<div class="devlocation" title="${value.text} ">设备位置:${value.text}</div>
<a data-id="${value.deviceid}"><span class="mapbutton"> 实时数据 </span></a>
<a data-id="${value.deviceid}"><span class="mapbutton-i"> 组态 </span></a>
<a data-id="${value.deviceid}" title="查看视频监控"> <i class ="ri-vidicon-line"></i></a>
</div>`,
}
}
geomeAata.push(data);
}
})
dataPack.offlineNumber = response1.count - dataPack.onlineNumber;
dataPack.typeNumber = Object.keys(devType).length;
dataPack.deviceAdded = dataPack.product.length;
}
return dList;
}).then((dList) => {
const keysArray = Object.keys(dList).map(key => key.replace('d_', ''))
fetchOptions.headers.dev = keysArray.join('|');
return fetch('/iotapi/notificationList', fetchOptions).then((res) => {
return isSuccess(res);
})
}).then((results)=>{
results.lastupdated = timestamp;
var msgConut = 0;
$.each(results.results, function (i, item) {
if (item.status == '1') { msgConut += 1 };
results.results
.name = dList['d_'+item.deviceid].name;
});
top.localforage.setItem("notificationList", results);
top.layui.yadmin.setMsgConut('msg1', msgConut);
dataPack.alarmedNumber = results.count - msgConut;
dataPack.unalarmNumber = msgConut;
dataPack.alarmNumber = results.count;
//渲染卡片视图
var template = $("#card").html();
laytpl(template).render(dataPack, function (html) {
$("#view").html(html).promise().done(function () {
//渲染图表
rendercard(dataPack);
//渲染地图
var qqmap = new TMap.Map("mapDiv", {
zoom: 5, //设置地图缩放级别
center: new TMap.LatLng(ipLocation.lat, ipLocation.lon), //设置地图中心点坐标
mapStyleId: "style2",
viewMode: "2D",
})
//渲染位置标注
renderMarker(qqmap, geomeAata);
})
})
})