1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-01 22:53:29 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

完善网站状态检测

This commit is contained in:
2022-03-14 23:37:06 +08:00
parent ede310d26e
commit 12a16c078b
5 changed files with 147 additions and 117 deletions

View File

@@ -0,0 +1,119 @@
var timeout = null;
$('.info').css("display", "none");
$(document).ready(function () {
startCheck();
$('#container').css("visibility", "");
$(".removeAfterScriptLoaded").remove();
});
function startCheck() {
if (timeout) clearTimeout(timeout);
document.getElementById("checkBtn").value = "检测中";
document.getElementById("checkBtn").disabled = "disabled";
$('.info').html("loading...");
$('.info-disabled').html("暂不提供检测");
$('.info').css("display", "");
$(".info-ok").removeClass("info-ok");
$(".info-err").removeClass("info-err");
var i = 0, timeSpan = 300;
setTimeout(checkOnlineStatus, timeSpan * ++i);
setTimeout(checkBackendStatus, timeSpan * ++i);
setTimeout(checkTimeOff, timeSpan * ++i);
setTimeout(finishCheck, timeSpan * ++i);
}
function checkOnlineStatus() {
var onlineStatus = window.navigator.onLine;
$("#onlineStatus").text(onlineStatus ? "已连接" : "您当前未连接互联网");
$("#onlineStatus").addClass(onlineStatus ? "info-ok" : "info-err");
}
function checkBackendStatus() {
var backendStatus = false;
getRequest("/status/getProcessCpu", {})
.then(function (response) {
console.log("response.data", response.data);
if (response.data == 0) {
backendStatus = true;
}
$("#backendStatus").text("后台连接正常");
$("#backendStatus").addClass("info-ok");
})
.catch(function (error) {
$("#backendStatus").text("后台连接异常");
$("#backendStatus").addClass("info-err");
});
}
function finishCheck() {
document.getElementById("checkBtn").value = "重新检测";
document.getElementById("checkBtn").disabled = "";
if (timeout) clearTimeout(timeout);
// timeout = setTimeout(startCheck, 10 * 1000);
}
function checkTimeOff(targetUrl = null) {
if (!targetUrl) targetUrl = location.href;
// refer: https://juejin.cn/post/6844903960705236999
var xhr = new window.XMLHttpRequest;
xhr.responseType = "document";
// 通过get的方式请求当前文件
xhr.open("head", targetUrl);
xhr.send(null);
// // 考虑请求花费时间
// var requestStartTime = getNowDate(Date.now(), 8);
// var requestEndTime;
// 监听请求状态变化
xhr.onreadystatechange = function () {
var time = null,
curDate = null;
if (xhr.readyState === 2) {
// 获取响应头里的时间戳
time = xhr.getResponseHeader("Date");
console.log("请求结束时本地时间(东八区时间): " + new Date(time).getTime());
// requestEndTime = Date.now();
// console.log("请求开始时本地时间(用于计算时间差): " + requestStartTime);
// console.log("请求结束时本地时间(用于计算时间差): " + requestEndTime);
// console.log("发送请求到收到响应的时间差: " + (requestEndTime - requestStartTime));
// console.log("发送请求到收到响应的时间差/2: " + (requestEndTime - requestStartTime) / 2);
var timeOff = getTimeOff(new Date(time).getTime());
// timeOff = timeOff + (requestEndTime - requestStartTime) / 2;
var target = document.getElementById("timeOff");
if (target) {
target.innerHTML = "本地时间比服务器" + (timeOff > 0 ? "慢" : "快") + Math.abs(timeOff / 1000) + "秒";
$(target).addClass("info-ok");
}
}
};
function getTimeOff(time) {
let serverTime = getNowDate(time, 8); // Head请求返回服务器当前时间戳
let localTime = getNowDate(Date.now(), 8); // 用户本地时间戳
let timeOff = serverTime - localTime;
return timeOff;
}
function getNowDate(localTime, timeZone) {
var timezone = timeZone || 8; //目标时区时间,东八区
// 本地时间和格林威治的时间差,单位为分钟
var offset_GMT = new Date().getTimezoneOffset();
// 本地时间距 1970 年 1 月 1 日午夜GMT 时间)之间的毫秒数
var nowDate = localTime;
var targetDate = nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000;
return targetDate;
}
}

View File

@@ -1,55 +0,0 @@
// refer: https://juejin.cn/post/6844903960705236999
var xhr = new window.XMLHttpRequest;
xhr.responseType = "document";
// 通过get的方式请求当前文件
xhr.open("head", location.href);
xhr.send(null);
// 监听请求状态变化
xhr.onreadystatechange = function () {
var time = null,
curDate = null;
if (xhr.readyState === 2) {
// 获取响应头里的时间戳
time = xhr.getResponseHeader("Date");
// countDown(new Date(time).getTime());
var timeOff = getTimeOff(new Date(time).getTime());
var target = document.getElementById("timeOff");
if (target) {
target.innerHTML = "本地时间比服务器" + (timeOff > 0 ? "慢" : "快") + Math.abs(timeOff / 1000) + "秒";
}
}
};
function getTimeOff(time) {
let serverTime = getNowDate(time, 8); // Head请求返回服务器当前时间戳
let localTime = getNowDate(Date.now(), 8); // 用户本地时间戳
let timeOff = serverTime - localTime;
return timeOff;
}
// function countDown(time) {
// let targetTime = Date.parse('2025/12/12 00:00:00');
// let serverTime = getNowDate(time, 8); // Head请求返回服务器当前时间戳
// let localTime = getNowDate(Date.now(), 8); // 用户本地时间戳
// let timeOff = serverTime - localTime;
// let rightTargetTime = targetTime - timeOff; // 去除偏差后的目标时间
// if(rightTargetTime <= localTime) {
// console.log('按钮可点击')
// } else {
// console.log('按钮不可点击')
// }
// }
function getNowDate(localTime, timeZone) {
var timezone = timeZone || 8; //目标时区时间,东八区
// 本地时间和格林威治的时间差,单位为分钟
var offset_GMT = new Date().getTimezoneOffset();
// 本地时间距 1970 年 1 月 1 日午夜GMT 时间)之间的毫秒数
var nowDate = localTime;
var targetDate = nowDate + offset_GMT * 60 * 1000 + timezone * 60 * 60 * 1000;
return targetDate;
}

View File

@@ -6,6 +6,14 @@
.info-disabled {
color: #999;
}
.info-ok {
color: green;
}
.info-err {
color: red;
}
</style>
</head>
<body>
@@ -14,9 +22,14 @@
<h1>
<%= title %>
</h1>
<div id="container">
<input id="checkBtn" type="button" value="检测" onclick="startCheck()">
<div class="removeAfterScriptLoaded">
<p>正在准备检测环境,请稍候...</p>
<p>若长时间无响应请尝试刷新页面</p>
</div>
<div id="container" style="visibility: hidden;">
<input id="checkBtn" type="button" value="检测" onclick="startCheck()">
<div class="parentNode">
<div class="childrenNode">
<h3 class="title">网络连通性</h3>
@@ -38,67 +51,17 @@
</div>
</main>
<%- include("./component/footer.html"); %>
<script async="true" src="./assets/javascripts/siteStatus.js"></script>
<script>
var timeout = null;
$('.info').css("display", "none");
// function checkTimeOff() {
// var oldScriptDom = document.getElementById("timeCalibrationScript");
// if (oldScriptDom) oldScriptDom.parentNode.removeChild(oldScriptDom);
function startCheck() {
if (timeout) clearTimeout(timeout);
document.getElementById("checkBtn").value = "检测中";
document.getElementById("checkBtn").disabled = "disabled";
$('.info').html("loading...");
$('.info-disabled').html("暂不提供检测");
$('.info').css("display", "");
var i = 0, timeSpan = 300;
setTimeout(checkOnlineStatus, timeSpan * ++i);
setTimeout(checkBackendStatus, timeSpan * ++i);
setTimeout(checkTimeOff, timeSpan * ++i);
setTimeout(finishCheck, timeSpan * ++i);
}
function checkOnlineStatus() {
var onlineStatus = window.navigator.onLine;
$("#onlineStatus").text(onlineStatus ? "已连接" : "您当前未连接互联网");
}
function checkBackendStatus() {
var backendStatus = false;
getRequest("/status/getProcessCpu", {})
.then(function (response) {
console.log("response.data", response.data);
if (response.data == 0) {
backendStatus = true;
}
$("#backendStatus").text("后台连接正常");
})
.catch(function (error) {
$("#backendStatus").text("后台连接异常");
});
}
function checkTimeOff() {
var oldScriptDom = document.getElementById("timeCalibrationScript");
if (oldScriptDom) oldScriptDom.parentNode.removeChild(oldScriptDom);
oldScriptDom = document.createElement("script");
oldScriptDom.id = "timeCalibrationScript";
oldScriptDom.src = "./assets/javascripts/timeCalibration.js?" + Date.now() + "-" + new Date().getTime();
document.body.appendChild(oldScriptDom);
}
function finishCheck() {
document.getElementById("checkBtn").value = "重新检测";
document.getElementById("checkBtn").disabled = "";
if (timeout) clearTimeout(timeout);
timeout = setTimeout(startCheck, 5000);
}
// $(document).ready(function () {
// startCheck();
// });
// oldScriptDom = document.createElement("script");
// oldScriptDom.id = "timeCalibrationScript";
// oldScriptDom.src = "./assets/javascripts/timeCalibration.js?" + Date.now() + "-" + new Date().getTime();
// document.body.appendChild(oldScriptDom);
// }
</script>
</body>
</html>

View File

@@ -41,7 +41,6 @@ public class SwaggerApiDocGeneratorTest {
// .withGeneratedExamples()
// .withoutInlineSchema()
// .build();
// System.out.println(swaggerApiUrl);
// //设置swagger-api的json来源
// Swagger2MarkupConverter.from(new URL(swaggerApiUrl))
// .withConfig(config)

View File

@@ -30,6 +30,10 @@
url: 'http://localhost/api/swagger-ui.html',
title: 'Swagger'
},
{
url: './bookshelfplus/docs/asciidoc/html/all.html',
title: 'Swagger生成的Api文档'
},
{
url: 'http://localhost/api/',
title: 'API'