feat: 看板最近访问查看更多跳转+访问埋点今日/昨日/7天/30天/来源筛选(visits接口补range/source参数与统计)
This commit is contained in:
@@ -72,12 +72,42 @@ router.get('/visits', requirePermission('dashboard:read'), async (req, res) => {
|
||||
try {
|
||||
const page = Math.max(1, parseInt(req.query.page) || 1)
|
||||
const limit = Math.min(50, parseInt(req.query.limit) || 20)
|
||||
const source = String(req.query.source || '').trim()
|
||||
const range = String(req.query.range || '7d')
|
||||
// range → mongo 时间条件
|
||||
const now = new Date()
|
||||
const startOfDay = (d) => { const x = new Date(d); x.setHours(0, 0, 0, 0); return x }
|
||||
let timeQuery = {}
|
||||
if (range === 'today') timeQuery = { createdAt: { $gte: startOfDay(now) } }
|
||||
else if (range === 'yesterday') {
|
||||
const y = new Date(now); y.setDate(y.getDate() - 1)
|
||||
timeQuery = { createdAt: { $gte: startOfDay(y), $lt: startOfDay(now) } }
|
||||
} else if (range === '30d') {
|
||||
const d = new Date(now); d.setDate(d.getDate() - 29)
|
||||
timeQuery = { createdAt: { $gte: startOfDay(d) } }
|
||||
} else if (range === 'all') timeQuery = {}
|
||||
else { // 7d
|
||||
const d = new Date(now); d.setDate(d.getDate() - 6)
|
||||
timeQuery = { createdAt: { $gte: startOfDay(d) } }
|
||||
}
|
||||
const query = { ...timeQuery, ...(source ? { source } : {}) }
|
||||
const [total, items] = await Promise.all([
|
||||
VisitLog.countDocuments({}),
|
||||
VisitLog.find({}).sort({ createdAt: -1 }).skip((page - 1) * limit).limit(limit).lean(),
|
||||
VisitLog.countDocuments(query),
|
||||
VisitLog.find(query).sort({ createdAt: -1 }).skip((page - 1) * limit).limit(limit).lean(),
|
||||
])
|
||||
items.forEach(v => { v.ipLoc = ipLocation(v.ip) })
|
||||
return ApiResponse.success(res, { items, total, page, limit, pagination: { total } })
|
||||
// 快捷统计(当日/昨日/7天/30天总量)
|
||||
const cnt = (q) => VisitLog.countDocuments(q)
|
||||
const y = new Date(now); y.setDate(y.getDate() - 1)
|
||||
const d7 = new Date(now); d7.setDate(d7.getDate() - 6)
|
||||
const d30 = new Date(now); d30.setDate(d30.getDate() - 29)
|
||||
const stats = {
|
||||
today: await cnt({ createdAt: { $gte: startOfDay(now) } }),
|
||||
yesterday: await cnt({ createdAt: { $gte: startOfDay(y), $lt: startOfDay(now) } }),
|
||||
last7: await cnt({ createdAt: { $gte: startOfDay(d7) } }),
|
||||
last30: await cnt({ createdAt: { $gte: startOfDay(d30) } }),
|
||||
}
|
||||
return ApiResponse.success(res, { items, total, page, limit, stats, pagination: { total } })
|
||||
} catch (error) {
|
||||
logger.error('访问日志查询失败:', error)
|
||||
return ApiResponse.serverError(res, error.message)
|
||||
|
||||
Reference in New Issue
Block a user