访问日志增加更容易可视化的时间显示

This commit is contained in:
刘祥超
2021-07-12 18:06:43 +08:00
parent b8295e5cfc
commit 40f32c7cf9
3 changed files with 43 additions and 9 deletions

View File

@@ -16,14 +16,16 @@ Tea.context(function () {
.success(function (resp) {
this.accessLogs = resp.data.accessLogs.concat(this.accessLogs)
// 添加区域信息
this.accessLogs.forEach(function (accessLog) {
if (typeof (resp.data.regions[accessLog.remoteAddr]) == "string") {
accessLog.region = resp.data.regions[accessLog.remoteAddr]
} else {
accessLog.region = ""
}
})
// 添加区域信息
let that = this
this.accessLogs.forEach(function (accessLog) {
that.formatTime(accessLog)
if (typeof (resp.data.regions[accessLog.remoteAddr]) == "string") {
accessLog.region = resp.data.regions[accessLog.remoteAddr]
} else {
accessLog.region = ""
}
})
let max = 100
if (this.accessLogs.length > max) {
@@ -45,4 +47,18 @@ Tea.context(function () {
}, 5000)
})
}
this.formatTime = function (accessLog) {
let elapsedSeconds = Math.ceil(new Date().getTime() / 1000) - accessLog.timestamp
if (elapsedSeconds >= 0) {
if (elapsedSeconds < 60) {
accessLog.humanTime = elapsedSeconds + "秒前"
} else if (elapsedSeconds < 3600) {
accessLog.humanTime = Math.ceil(elapsedSeconds / 60) + "分钟前"
} else if (elapsedSeconds < 3600 * 24) {
accessLog.humanTime = Math.ceil(elapsedSeconds / 3600) + "小时前"
}
}
}
})