feat: change danmaku access to frontend

This commit is contained in:
lyz05
2024-01-17 21:55:56 +08:00
parent 57cddc97c1
commit 7c29349cfb
5 changed files with 57 additions and 39 deletions

1
app.js
View File

@@ -31,6 +31,7 @@ app.use("/assets", [
express.static(__dirname + "/node_modules/jquery/dist/"),
express.static(__dirname + "/node_modules/bootstrap/dist/"),
express.static(__dirname + "/node_modules/axios/dist/"),
express.static(__dirname + "/node_modules/leancloud-storage/dist"),
]);
// app.use("/upload", express.static(__dirname + "/upload"));

View File

@@ -125,18 +125,4 @@ router.get("/", apiLimiter, async function (req, res) {
if (!req.query.url) index(req, res); else resolve(req, res);
});
router.get("/pageinfo", async function (req, res) {
const promises = [
leancloud.danmakuQuery(leancloud.currentDay()),
leancloud.danmakuQuery(leancloud.lastDay()),
leancloud.danmakuQuery(leancloud.currentMonth())
];
const [today_visited, lastday_visited, month_visited] = await Promise.all(promises);
res.json({
today_visited,
lastday_visited,
month_visited
});
});
module.exports = router;

View File

@@ -22,20 +22,6 @@ function currentDay() {
return [start, end, y, m+1];
}
function lastDay() {
const currentday = currentDay();
currentday[0].setDate(currentday[0].getDate() - 1);
currentday[1].setDate(currentday[1].getDate() - 1);
return currentday;
}
function currentMonth() {
const date = new Date(), y = date.getFullYear(), m = date.getMonth();
const firstDay = new Date(y, m, 1);
const lastDay = new Date(y, m + 1, 0);
return [firstDay, lastDay];
}
async function danmakuQuery(date, ip) {
if (!AV) return 0;
const className = `DanmakuAccess_${currentDay()[2]}_${currentDay()[3]}`;
@@ -74,7 +60,13 @@ function getipCountry(ip) {
}
}
module.exports = { danmakuQuery, currentDay, currentMonth, lastDay, add };
module.exports = { danmakuQuery, currentDay, add };
if (!module.parent) {
}
// curl -X POST \
// -H "X-LC-Id: {{appid}}" \
// -H "X-LC-Key: {{appkey}}" \
// -H "Content-Type: application/json" \
// -d '{"content": "每个 Java 程序员必备的 8 个开发工具","pubUser": "官方客服","pubTimestamp": 1435541999}' \
// https://{{host}}/1.1/classes/Post

View File

@@ -85,16 +85,53 @@
</body>
<script>
$.ajax({
url: 'pageinfo',
type: 'GET',
dataType: 'json',
success: function (data) {
$('#today_visited').text(data.today_visited);
$('#lastday_visited').text(data.lastday_visited);
$('#month_visited').text(data.month_visited);
}
AV.init({
appId: "DBvUNEk87tttf0zxelzVRfuW-9Nh9j0Va",
appKey: "CnqCkxWUFTmxwzCqu1alwFEm",
serverURL: "https://dbvunek8.lc-cn-n1-shared.com"
});
function currentDay() {
const date = new Date(), y = date.getFullYear(), m = date.getMonth();
const start = new Date(date.setHours(0, 0, 0, 0));
const end = new Date(date.setHours(23, 59, 59, 999));
return [start, end, y, m+1];
}
function lastDay() {
const currentday = currentDay();
currentday[0].setDate(currentday[0].getDate() - 1);
currentday[1].setDate(currentday[1].getDate() - 1);
return currentday;
}
function currentMonth() {
const date = new Date(), y = date.getFullYear(), m = date.getMonth();
const firstDay = new Date(y, m, 1);
const lastDay = new Date(y, m + 1, 0);
return [firstDay, lastDay];
}
function danmakuQuery(date, ip) {
const className = `DanmakuAccess_${currentDay()[2]}_${currentDay()[3]}`;
const query = new AV.Query(className);
query.greaterThanOrEqualTo("createdAt", date[0]);
query.lessThan("createdAt", date[1]);
if (ip) query.equalTo("remoteIP", ip);
query.exists("url");
return query.count();
}
danmakuQuery(currentDay()).then((count)=>{
$('#today_visited').text(count);
});
danmakuQuery(lastDay()).then((count)=>{
$('#lastday_visited').text(count);
});
danmakuQuery(currentMonth()).then((count)=>{
$('#month_visited').text(count);
});
</script>
</html>
</html>

View File

@@ -14,4 +14,6 @@
<script src="/assets/js/bootstrap.min.js"></script>
<!-- axios-->
<script src="/assets/axios.min.js"></script>
<!-- Leancloud -->
<script src="/assets/av-min.js"></script>
</head>