diff --git a/backend/src/main/java/com/banben/modules/auth/controller/LoginLogController.java b/backend/src/main/java/com/banben/modules/auth/controller/LoginLogController.java index faa403b..886c52a 100644 --- a/backend/src/main/java/com/banben/modules/auth/controller/LoginLogController.java +++ b/backend/src/main/java/com/banben/modules/auth/controller/LoginLogController.java @@ -6,14 +6,20 @@ import com.banben.modules.admin.support.AdminAccess; import com.banben.modules.auth.dto.LoginLogQuery; import com.banben.modules.auth.entity.LoginLog; import com.banben.modules.auth.service.LoginLogService; +import com.banben.modules.auth.vo.AccessDimensionItem; +import com.banben.modules.auth.vo.AccessOverview; +import com.banben.modules.auth.vo.AccessTrendPoint; import com.banben.modules.auth.vo.LoginLogSummary; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + /** * 后台管理 - 登录日志。 */ @@ -39,4 +45,32 @@ public class LoginLogController { adminAccess.require(); return Result.success(loginLogService.summary()); } + + @Operation(summary = "网络访问概览:历史/本月/本周/今日 的人次与人数") + @GetMapping("/access-overview") + public Result accessOverview() { + adminAccess.require(); + return Result.success(loginLogService.accessOverview()); + } + + @Operation(summary = "网络访问趋势:按 小时/日/周/月/年 分桶(可指定时段)") + @GetMapping("/access-trend") + public Result> accessTrend( + @RequestParam(defaultValue = "hour") String granularity, + @RequestParam(required = false) String start, + @RequestParam(required = false) String end) { + adminAccess.require(); + return Result.success(loginLogService.accessTrend(granularity, start, end)); + } + + @Operation(summary = "网络访问维度统计:按 IP/单位/账号/类型 分组(可指定时段)") + @GetMapping("/access-dimension") + public Result> accessDimension( + @RequestParam(defaultValue = "ip") String dimension, + @RequestParam(required = false) String start, + @RequestParam(required = false) String end, + @RequestParam(defaultValue = "20") int limit) { + adminAccess.require(); + return Result.success(loginLogService.accessDimension(dimension, start, end, limit)); + } } diff --git a/backend/src/main/java/com/banben/modules/auth/service/impl/LoginLogServiceImpl.java b/backend/src/main/java/com/banben/modules/auth/service/impl/LoginLogServiceImpl.java index 0e33c00..5579312 100644 --- a/backend/src/main/java/com/banben/modules/auth/service/impl/LoginLogServiceImpl.java +++ b/backend/src/main/java/com/banben/modules/auth/service/impl/LoginLogServiceImpl.java @@ -121,4 +121,140 @@ public class LoginLogServiceImpl implements LoginLogService { } return s; } + + // ==================== 网络访问统计 ==================== + + @Override + public AccessOverview accessOverview() { + AccessOverview o = new AccessOverview(); + try { + long[] history = countPair(null); + o.setHistoryCount(history[0]); + o.setHistoryUsers(history[1]); + + long[] month = countPair(LocalDate.now().withDayOfMonth(1).atStartOfDay()); + o.setMonthCount(month[0]); + o.setMonthUsers(month[1]); + + long[] week = countPair(LocalDate.now().with(DayOfWeek.MONDAY).atStartOfDay()); + o.setWeekCount(week[0]); + o.setWeekUsers(week[1]); + + long[] today = countPair(LocalDate.now().atStartOfDay()); + o.setTodayCount(today[0]); + o.setTodayUsers(today[1]); + } catch (Exception e) { + log.warn("统计网络访问概览失败:{}", e.getMessage()); + } + return o; + } + + @Override + public List accessTrend(String granularity, String start, String end) { + String expr = granularityExpr(granularity); + QueryWrapper w = new QueryWrapper<>(); + w.select(expr + " AS bucket", "COUNT(*) AS cnt", "COUNT(DISTINCT user_id) AS users"); + applyRange(w, start, end); + w.groupBy(expr).orderByAsc("bucket"); + List list = new ArrayList<>(); + try { + for (Map m : logMapper.selectMaps(w)) { + Object b = m.get("bucket"); + list.add(new AccessTrendPoint(b == null ? "" : b.toString(), + toLong(m.get("cnt")), toLong(m.get("users")))); + } + } catch (Exception e) { + log.warn("统计网络访问趋势失败:{}", e.getMessage()); + } + return list; + } + + @Override + public List accessDimension(String dimension, String start, String end, int limit) { + String col = "ip"; + if ("org".equals(dimension)) { + col = "org_name"; + } else if ("account".equals(dimension)) { + col = "username"; + } else if ("type".equals(dimension)) { + col = "online"; + } + QueryWrapper w = new QueryWrapper<>(); + w.select(col + " AS name", "COUNT(*) AS cnt", "COUNT(DISTINCT user_id) AS users"); + // 字符串维度排除空值;type 维度(online 为整数)仅排除 NULL,避免 integer <> '' 报错 + if ("type".equals(dimension)) { + w.isNotNull(col); + } else { + w.isNotNull(col).ne(col, ""); + } + applyRange(w, start, end); + w.groupBy(col).orderByDesc("cnt").last("LIMIT " + Math.max(1, limit)); + List list = new ArrayList<>(); + try { + for (Map m : logMapper.selectMaps(w)) { + Object raw = m.get("name"); + String name = raw == null ? "未知" : raw.toString(); + if ("type".equals(dimension)) { + name = "1".equals(name) ? "在线" : "0".equals(name) ? "离线" : name; + } + list.add(new AccessDimensionItem(name, toLong(m.get("cnt")), toLong(m.get("users")))); + } + } catch (Exception e) { + log.warn("统计网络访问维度失败:{}", e.getMessage()); + } + return list; + } + + /** 人次 + 去重人数(按给定起始时间)。 */ + private long[] countPair(LocalDateTime ge) { + QueryWrapper w = new QueryWrapper<>(); + w.select("COUNT(*) AS c", "COUNT(DISTINCT user_id) AS u"); + if (ge != null) { + w.ge("login_time", ge); + } + List> maps = logMapper.selectMaps(w); + long c = 0, u = 0; + if (!maps.isEmpty()) { + Map m = maps.get(0); + c = toLong(m.get("c")); + u = toLong(m.get("u")); + } + return new long[]{c, u}; + } + + /** 时间分桶表达式(PostgreSQL)。 */ + private String granularityExpr(String g) { + if ("day".equals(g)) { + return "to_char(login_time, 'YYYY-MM-DD')"; + } + if ("week".equals(g)) { + return "to_char(date_trunc('week', login_time), 'YYYY-MM-DD')"; + } + if ("month".equals(g)) { + return "to_char(login_time, 'YYYY-MM')"; + } + if ("year".equals(g)) { + return "to_char(login_time, 'YYYY')"; + } + // 默认按小时(含日期,避免跨天合并) + return "to_char(login_time, 'YYYY-MM-DD HH24')"; + } + + /** 时间区间条件:start 含当天 00:00,end 含当天 23:59:59。 */ + private void applyRange(QueryWrapper w, String start, String end) { + if (StrUtil.isNotBlank(start)) { + w.ge("login_time", start.trim()); + } + if (StrUtil.isNotBlank(end)) { + String e = end.trim(); + if (e.length() == 10) { + e = e + " 23:59:59"; + } + w.le("login_time", e); + } + } + + private long toLong(Object value) { + return value instanceof Number number ? number.longValue() : 0L; + } } diff --git a/frontend/src/api/admin.ts b/frontend/src/api/admin.ts index 15584ff..6f52995 100644 --- a/frontend/src/api/admin.ts +++ b/frontend/src/api/admin.ts @@ -201,3 +201,54 @@ export function pageLoginLogs(params: LoginLogQuery) { export function loginLogSummary() { return request.get('/admin/login-logs/summary') } + +// ==================== 网络访问统计 ==================== + +export interface AccessOverview { + /** 历史访问人次 */ + historyCount: number + /** 历史访问人数(去重) */ + historyUsers: number + /** 本月访问人次 */ + monthCount: number + monthUsers: number + /** 本周访问人次 */ + weekCount: number + weekUsers: number + /** 今日访问人次 */ + todayCount: number + todayUsers: number +} + +export interface AccessTrendPoint { + /** 时间桶,如 2026-11-04 08 / 2026-11 / 2026 */ + bucket: string + /** 访问人次 */ + count: number + /** 访问人数(去重) */ + users: number +} + +export interface AccessDimensionItem { + /** 维度值(IP / 单位 / 账号 / 在线状态) */ + name: string + /** 访问人次 */ + count: number + /** 访问人数(去重) */ + users: number +} + +/** 网络访问概览:历史 / 本月 / 本周 / 今日 */ +export function accessOverview() { + return request.get('/admin/login-logs/access-overview') +} + +/** 网络访问趋势:granularity = hour|day|week|month|year,可指定 start/end(yyyy-MM-dd) */ +export function accessTrend(params: { granularity: string; start?: string; end?: string }) { + return request.get('/admin/login-logs/access-trend', { params }) +} + +/** 网络访问维度统计:dimension = ip|org|account|type */ +export function accessDimension(params: { dimension: string; start?: string; end?: string; limit?: number }) { + return request.get('/admin/login-logs/access-dimension', { params }) +} diff --git a/frontend/src/views/admin/AccessStatsPanel.vue b/frontend/src/views/admin/AccessStatsPanel.vue new file mode 100644 index 0000000..b081c76 --- /dev/null +++ b/frontend/src/views/admin/AccessStatsPanel.vue @@ -0,0 +1,329 @@ + + + + + diff --git a/frontend/src/views/admin/AdminView.vue b/frontend/src/views/admin/AdminView.vue index be4df88..c8bffbf 100644 --- a/frontend/src/views/admin/AdminView.vue +++ b/frontend/src/views/admin/AdminView.vue @@ -13,7 +13,8 @@ const menuTitle: Record = { library: '院校资源库', fields: '列表字段配置', deposit: '呈缴管理', - log: '日志管理' + log: '日志管理', + access: '网络访问统计' } const pageTitle = computed(() => menuTitle[activeMenu.value] || '后台管理') @@ -44,6 +45,7 @@ import { type SensitiveWord, } from '@/api/admin' import OrgUserPanel from './OrgUserPanel.vue' +import AccessStatsPanel from './AccessStatsPanel.vue' import { acceptDeposit, pageDeposit, @@ -547,6 +549,7 @@ onMounted(() => { 列表字段配置 呈缴管理 日志管理 + 网络访问统计 @@ -940,6 +943,9 @@ onMounted(() => { @size-change="fetchLogs" /> + + +