From fe91a8ffb258a0796d8a2968d2f230b5f6ef6ff4 Mon Sep 17 00:00:00 2001 From: kevin Date: Fri, 3 Jul 2026 10:24:52 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=90=8E=E5=8F=B0=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E7=94=A8=E6=88=B7=E6=80=BB=E6=95=B0=E5=8D=A1=E7=89=87?= =?UTF-8?q?=EF=BC=8C=E7=82=B9=E5=87=BB=E8=BF=9B=E5=85=A5=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E7=AE=A1=E7=90=86=EF=BC=8C=E7=A7=BB=E9=99=A4header=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E7=AE=A1=E7=90=86=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/App.vue | 8 ------- frontend/src/views/AdminView.vue | 36 ++++++++++++++++++++++++++------ internal/handler/user.go | 6 ++++++ internal/router/router.go | 1 + 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/frontend/src/App.vue b/frontend/src/App.vue index 636de6c..7eb0bd9 100644 --- a/frontend/src/App.vue +++ b/frontend/src/App.vue @@ -52,14 +52,6 @@ const navLinks = [ > 管理后台 - - 用户管理 - (null) + +async function fetchUserCount() { + try { + const res = await fetch('/api/admin/users/count', { + headers: { Authorization: `Bearer ${authStore.token}` }, + }) + if (res.ok) { + const data = await res.json() + userCount.value = data.count + const stat = stats.value.find(s => s.label === '用户总数') + if (stat) stat.value = String(data.count) + } + } catch {} +} + onMounted(async () => { await authStore.fetchUser() + fetchUserCount() }) +function handleStatClick(route: string) { + if (route) router.push(route) +} + function handleLogout() { authStore.logout() router.push('/') @@ -26,11 +48,13 @@ function handleLogout() {

管理后台

-
+
diff --git a/internal/handler/user.go b/internal/handler/user.go index 934e989..c3e3a2c 100644 --- a/internal/handler/user.go +++ b/internal/handler/user.go @@ -44,6 +44,12 @@ func formatUser(u *model.User) userResponse { } } +func GetUserCount(c *gin.Context) { + var count int64 + db.DB.Model(&model.User{}).Count(&count) + c.JSON(http.StatusOK, gin.H{"count": count}) +} + func ListUsers(c *gin.Context) { var users []model.User db.DB.Order("id asc").Find(&users) diff --git a/internal/router/router.go b/internal/router/router.go index 4deae44..620f37f 100644 --- a/internal/router/router.go +++ b/internal/router/router.go @@ -26,6 +26,7 @@ func Setup(r *gin.Engine) { admin := r.Group("/api/admin") admin.Use(middleware.AuthMiddleware(), middleware.AdminMiddleware()) { + admin.GET("/users/count", handler.GetUserCount) admin.GET("/users", handler.ListUsers) admin.POST("/users", handler.CreateUser) admin.PUT("/users/:id", handler.UpdateUser)