diff --git a/mock/analytics.mock.ts b/mock/analytics.mock.ts new file mode 100644 index 00000000..ae305901 --- /dev/null +++ b/mock/analytics.mock.ts @@ -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", + }, + }, +]); diff --git a/mock/log.mock.ts b/mock/log.mock.ts index 9d2e4b28..a50b9366 100644 --- a/mock/log.mock.ts +++ b/mock/log.mock.ts @@ -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", - }, - }, ]); diff --git a/package.json b/package.json index f8aba0ab..3e7570eb 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/api/analytics/index.ts b/src/api/analytics/index.ts new file mode 100644 index 00000000..f0ccf93a --- /dev/null +++ b/src/api/analytics/index.ts @@ -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({ + url: `${ANALYTICS_BASE_URL}/visits/summary`, + method: "get", + }); + }, + + /** + * 获取访问趋势 + */ + getVisitTrend(queryParams: VisitTrendQuery) { + return request({ + url: `${ANALYTICS_BASE_URL}/visits/trend`, + method: "get", + params: queryParams, + }); + }, + + /** + * 获取用户汇总 + */ + getUserSummary() { + return request({ + url: `${ANALYTICS_BASE_URL}/users/summary`, + method: "get", + }); + }, +}; + +export default AnalyticsAPI; + +export * from "./types"; diff --git a/src/api/analytics/types.ts b/src/api/analytics/types.ts new file mode 100644 index 00000000..5cbb593c --- /dev/null +++ b/src/api/analytics/types.ts @@ -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; +} diff --git a/src/api/system/log/index.ts b/src/api/system/log/index.ts index c2bd3877..6567c422 100644 --- a/src/api/system/log/index.ts +++ b/src/api/system/log/index.ts @@ -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({ - url: `${LOG_BASE_URL}/analytics/trend`, - method: "get", - params: queryParams, - }); - }, - - /** - * 获取访问概览统计 - */ - getVisitOverview() { - return request({ - url: `${LOG_BASE_URL}/analytics/overview`, - method: "get", - }); - }, }; export default LogAPI; diff --git a/src/api/system/log/types.ts b/src/api/system/log/types.ts index d2591d12..0065dabc 100644 --- a/src/api/system/log/types.ts +++ b/src/api/system/log/types.ts @@ -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; -} diff --git a/src/views/dashboard/index.vue b/src/views/dashboard/index.vue index 7a655e2c..3515f415 100644 --- a/src/views/dashboard/index.vue +++ b/src/views/dashboard/index.vue @@ -142,7 +142,7 @@
系统用户 - {{ userStats ? fmt(userStats.users) : "--" }} + {{ userSummary ? fmt(userSummary.total) : "--" }}
@@ -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({ - todayUvCount: 0, - uvGrowthRate: 0, - totalUvCount: 0, - todayPvCount: 0, - pvGrowthRate: 0, - totalPvCount: 0, +const visitSummary = ref({ + 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(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(); +const visitTrendData = ref(); 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(); });