feat(analytics): 新增统计接口并迁移首页看板

- 新增访问汇总、访问趋势、用户汇总接口封装
- 首页看板改用新接口并解析趋势数据点
- 移除日志 API 中的旧统计方法与类型
This commit is contained in:
Ray.Hao committed 2026-11-24 09:20:33 +08:00
1 parent 38189e5fdf
commit 4c4e12d335
8 files changed
+199 -144

No files matched your search

+53
View File
@@ -0,0 +1,53 @@
import { defineMock } from "./base";
export default defineMock([
{
url: "analytics/visits/summary",
method: ["GET"],
body: {
code: "00000",
data: {
todayUv: 169,
yesterdayUv: 176,
totalUv: 19985,
todayPv: 1629,
yesterdayPv: 1712,
totalPv: 286086,
uvGrowthRate: -0.0398,
pvGrowthRate: -0.0485,
},
msg: "一切ok",
},
},
{
url: "analytics/visits/trend",
method: ["GET"],
body: {
code: "00000",
data: {
startDate: "2024-06-30",
endDate: "2024-07-07",
points: [
{ date: "2024-06-30", pv: 1751, uv: 207 },
{ date: "2024-07-01", pv: 5168, uv: 566 },
{ date: "2024-07-02", pv: 4882, uv: 565 },
{ date: "2024-07-03", pv: 5301, uv: 631 },
{ date: "2024-07-04", pv: 4721, uv: 579 },
{ date: "2024-07-05", pv: 4885, uv: 496 },
{ date: "2024-07-06", pv: 1901, uv: 222 },
{ date: "2024-07-07", pv: 1003, uv: 152 },
],
},
msg: "一切ok",
},
},
{
url: "analytics/users/summary",
method: ["GET"],
body: {
code: "00000",
data: { total: 128 },
msg: "一切ok",
},
},
]);
-38
View File
@@ -164,42 +164,4 @@ export default defineMock([
msg: "一切ok",
},
},
{
url: "logs/analytics/trend",
method: ["GET"],
body: {
code: "00000",
data: {
dates: [
"2024-06-30",
"2024-07-01",
"2024-07-02",
"2024-07-03",
"2024-07-04",
"2024-07-05",
"2024-07-06",
"2024-07-07",
],
pvList: [1751, 5168, 4882, 5301, 4721, 4885, 1901, 1003],
uvList: [207, 566, 565, 631, 579, 496, 222, 152],
},
msg: "一切ok",
},
},
{
url: "logs/analytics/overview",
method: ["GET"],
body: {
code: "00000",
data: {
todayUvCount: 169,
totalUvCount: 19985,
uvGrowthRate: -0.57,
todayPvCount: 1629,
totalPvCount: 286086,
pvGrowthRate: -0.65,
},
msg: "一切ok",
},
},
]);
+1 -1
View File
@@ -1,7 +1,7 @@
{
"name": "vue3-element-admin",
"description": "Vue3 + Vite + TypeScript + Element-Plus 的后台管理模板,vue-element-admin 的 Vue3 版本",
"version": "4.13.0",
"version": "4.14.0",
"private": true,
"type": "module",
"scripts": {
+41
View File
@@ -0,0 +1,41 @@
import request from "@/utils/request";
import type { UserSummary, VisitSummary, VisitTrend, VisitTrendQuery } from "./types";
const ANALYTICS_BASE_URL = "/api/v1/analytics";
const AnalyticsAPI = {
/**
* 获取访问汇总
*/
getVisitSummary() {
return request<unknown, VisitSummary>({
url: `${ANALYTICS_BASE_URL}/visits/summary`,
method: "get",
});
},
/**
* 获取访问趋势
*/
getVisitTrend(queryParams: VisitTrendQuery) {
return request<unknown, VisitTrend>({
url: `${ANALYTICS_BASE_URL}/visits/trend`,
method: "get",
params: queryParams,
});
},
/**
* 获取用户汇总
*/
getUserSummary() {
return request<unknown, UserSummary>({
url: `${ANALYTICS_BASE_URL}/users/summary`,
method: "get",
});
},
};
export default AnalyticsAPI;
export * from "./types";
+63
View File
@@ -0,0 +1,63 @@
/**
* 访问汇总
*/
export interface VisitSummary {
/** 今日访客数 */
todayUv: number;
/** 昨日访客数 */
yesterdayUv: number;
/** 累计访客数 */
totalUv: number;
/** 今日浏览量 */
todayPv: number;
/** 昨日浏览量 */
yesterdayPv: number;
/** 累计浏览量 */
totalPv: number;
/** 访客增长率;昨日为 0 时为 null */
uvGrowthRate: number | null;
/** 浏览量增长率;昨日为 0 时为 null */
pvGrowthRate: number | null;
}
/**
* 单日访问量
*/
export interface VisitTrendPoint {
/** 日期 yyyy-MM-dd */
date: string;
/** 浏览量 */
pv: number;
/** 访客数 */
uv: number;
}
/**
* 访问趋势查询参数
*/
export interface VisitTrendQuery {
/** 开始日期 yyyy-MM-dd */
startDate: string;
/** 结束日期 yyyy-MM-dd */
endDate: string;
}
/**
* 访问趋势
*/
export interface VisitTrend {
/** 查询开始日期 */
startDate: string;
/** 查询结束日期 */
endDate: string;
/** 逐日访问量 */
points: VisitTrendPoint[];
}
/**
* 用户汇总
*/
export interface UserSummary {
/** 用户总数 */
total: number;
}
+1 -28
View File
@@ -1,11 +1,5 @@
import request from "@/utils/request";
import type {
LogQuery,
LogItem,
VisitTrendQuery,
VisitTrendDetail,
VisitOverviewDetail,
} from "./types";
import type { LogQuery, LogItem } from "./types";
import type { PageResult } from "@/api/common";
const LOG_BASE_URL = "/api/v1/logs";
@@ -21,27 +15,6 @@ const LogAPI = {
params: queryParams,
});
},
/**
* 获取访问趋势统计
*/
getVisitTrend(queryParams: VisitTrendQuery) {
return request<unknown, VisitTrendDetail>({
url: `${LOG_BASE_URL}/analytics/trend`,
method: "get",
params: queryParams,
});
},
/**
* 获取访问概览统计
*/
getVisitOverview() {
return request<unknown, VisitOverviewDetail>({
url: `${LOG_BASE_URL}/analytics/overview`,
method: "get",
});
},
};
export default LogAPI;
-40
View File
@@ -51,43 +51,3 @@ export interface LogItem {
/** 操作时间 */
createTime?: string;
}
/**
* 访问趋势查询参数
*/
export interface VisitTrendQuery {
/** 开始日期 */
startDate: string;
/** 结束日期 */
endDate: string;
}
/**
* 访问趋势视图对象
*/
export interface VisitTrendDetail {
/** 日期列表 */
dates: string[];
/** 浏览量(PV)列表 */
pvList: number[];
/** 访客数(UV)列表 */
uvList: number[];
}
/**
* 访问概览视图对象
*/
export interface VisitOverviewDetail {
/** 今日独立访客数(UV) */
todayUvCount: number;
/** 累计独立访客数(UV) */
totalUvCount: number;
/** 独立访客增长率 */
uvGrowthRate: number;
/** 今日页面浏览量(PV) */
todayPvCount: number;
/** 累计页面浏览量(PV) */
totalPvCount: number;
/** 页面浏览量增长率 */
pvGrowthRate: number;
}
+40 -37
View File
@@ -142,7 +142,7 @@
</div>
<div class="stat-card__body">
<span class="stat-card__label">系统用户</span>
<span class="stat-card__num">{{ userStats ? fmt(userStats.users) : "--" }}</span>
<span class="stat-card__num">{{ userSummary ? fmt(userSummary.total) : "--" }}</span>
</div>
</div>
</div>
@@ -241,9 +241,8 @@ defineOptions({ name: "Dashboard", inheritAttrs: false });
import { dayjs } from "element-plus";
import { ref } from "vue";
import LogAPI from "@/api/system/log";
import UserAPI from "@/api/system/user";
import type { VisitOverviewDetail, VisitTrendDetail } from "@/api/system/log";
import AnalyticsAPI from "@/api/analytics";
import type { UserSummary, VisitSummary, VisitTrend } from "@/api/analytics";
import { useUserStore } from "@/stores/user";
import { useSettingsStore } from "@/stores/settings";
import { formatGrowthRate } from "@/utils";
@@ -355,49 +354,50 @@ const todoSummaryItems = [
{ label: "今日完成", value: "1" },
];
const visitOverviewData = ref<VisitOverviewDetail>({
todayUvCount: 0,
uvGrowthRate: 0,
totalUvCount: 0,
todayPvCount: 0,
pvGrowthRate: 0,
totalPvCount: 0,
const visitSummary = ref<VisitSummary>({
todayUv: 0,
yesterdayUv: 0,
totalUv: 0,
todayPv: 0,
yesterdayPv: 0,
totalPv: 0,
uvGrowthRate: null,
pvGrowthRate: null,
});
const uvGrowthText = computed(() => {
const r = visitOverviewData.value.uvGrowthRate;
const r = visitSummary.value.uvGrowthRate;
return r == null ? "--" : formatGrowthRate(r);
});
const pvGrowthText = computed(() => {
const r = visitOverviewData.value.pvGrowthRate;
const r = visitSummary.value.pvGrowthRate;
return r == null ? "--" : formatGrowthRate(r);
});
const uvIsUp = computed(() => (visitOverviewData.value.uvGrowthRate || 0) > 0);
const pvIsUp = computed(() => (visitOverviewData.value.pvGrowthRate || 0) > 0);
const uvIsUp = computed(() => (visitSummary.value.uvGrowthRate ?? 0) > 0);
const pvIsUp = computed(() => (visitSummary.value.pvGrowthRate ?? 0) > 0);
/** 千分位格式化,空值显示占位符 */
const fmt = (n?: number | null) => (n == null ? "--" : n.toLocaleString("en-US"));
// 用户统计:总数取接口真实数据,无权限时降级为占位符
const userStats = ref<{ users: number } | null>(null);
function fetchUserStats() {
UserAPI.getPage({ pageNum: 1, pageSize: 1 })
.then((all) => {
userStats.value = { users: all.total ?? 0 };
const userSummary = ref<UserSummary | null>(null);
function fetchUserSummary() {
AnalyticsAPI.getUserSummary()
.then((data) => {
userSummary.value = data;
})
.catch(() => {
userStats.value = null;
userSummary.value = null;
});
}
const tUv = useTransition(
computed(() => visitOverviewData.value.todayUvCount),
computed(() => visitSummary.value.todayUv),
{
duration: 800,
transition: [0.25, 0.1, 0.25, 1.0],
},
);
const tPv = useTransition(
computed(() => visitOverviewData.value.todayPvCount),
computed(() => visitSummary.value.todayPv),
{
duration: 800,
transition: [0.25, 0.1, 0.25, 1.0],
@@ -411,7 +411,7 @@ const visitTrendRangeOptions = [
{ label: "近7天", value: 7 },
{ label: "近30天", value: 30 },
];
const visitTrendData = ref<VisitTrendDetail>();
const visitTrendData = ref<VisitTrend>();
const visitTrendChartOptions = ref({});
/**
@@ -453,20 +453,20 @@ function colorWithAlpha(color: string, alpha: number) {
/**
* 拉取访问概览数据
*/
function fetchVisitOverviewData() {
LogAPI.getVisitOverview().then((d) => {
visitOverviewData.value = d;
function fetchVisitSummary() {
AnalyticsAPI.getVisitSummary().then((d) => {
visitSummary.value = d;
});
}
/**
* 拉取访问趋势数据
*/
function fetchVisitTrendData() {
function fetchVisitTrend() {
const s = dayjs()
.subtract(visitTrendDateRange.value - 1, "day")
.toDate();
LogAPI.getVisitTrend({
AnalyticsAPI.getVisitTrend({
startDate: dayjs(s).format("YYYY-MM-DD"),
endDate: dayjs(new Date()).format("YYYY-MM-DD"),
}).then((d) => {
@@ -478,7 +478,10 @@ function fetchVisitTrendData() {
/**
* 更新访问趋势图表配置
*/
function updateVisitTrendChartOptions(d: VisitTrendDetail) {
function updateVisitTrendChartOptions(d: VisitTrend) {
const dates = d.points.map((point) => point.date);
const pvList = d.points.map((point) => point.pv);
const uvList = d.points.map((point) => point.uv);
const primary = getCssVar("--chart-1", "#165dff");
const second = getCssVar("--chart-2", "#14c9c9");
const textSecondary = getCssVar("--el-text-color-secondary", "#909399");
@@ -503,7 +506,7 @@ function updateVisitTrendChartOptions(d: VisitTrendDetail) {
grid: { left: "0%", right: "3%", bottom: "14%", top: "5%", containLabel: true },
xAxis: {
type: "category",
data: d.dates,
data: dates,
axisTick: { show: false },
axisLine: { lineStyle: { color: colorWithAlpha(borderLighter, 0.82) } },
axisLabel: { fontSize: 11, color: textSecondary },
@@ -520,7 +523,7 @@ function updateVisitTrendChartOptions(d: VisitTrendDetail) {
{
name: "浏览量",
type: "line",
data: d.pvList,
data: pvList,
smooth: true,
symbol: "circle",
symbolSize: 5,
@@ -546,7 +549,7 @@ function updateVisitTrendChartOptions(d: VisitTrendDetail) {
{
name: "访客量",
type: "line",
data: d.uvList,
data: uvList,
smooth: true,
symbol: "circle",
symbolSize: 5,
@@ -573,7 +576,7 @@ function updateVisitTrendChartOptions(d: VisitTrendDetail) {
watch(
() => visitTrendDateRange.value,
() => fetchVisitTrendData(),
() => fetchVisitTrend(),
{ immediate: true },
);
@@ -589,8 +592,8 @@ watch(
{ deep: true },
);
onMounted(() => {
fetchVisitOverviewData();
fetchUserStats();
fetchVisitSummary();
fetchUserSummary();
});
</script>