60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package handlers
|
||
|
||
import (
|
||
"net/http"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/niangaodev/art-code/config"
|
||
"github.com/niangaodev/art-code/repositories"
|
||
)
|
||
|
||
// GetDashboardStats 获取仪表盘统计数据
|
||
func GetDashboardStats(c *gin.Context) {
|
||
// 1. 获取博文增长趋势 (最近7天新增)
|
||
postsTrend, err := repositories.GetNewPostsTrend()
|
||
if err != nil {
|
||
postsTrend = []struct {
|
||
Date string
|
||
Count int
|
||
}{} // Fallback empty
|
||
}
|
||
|
||
// 2. 获取今日访客UV (根据IP)
|
||
// 这里可以复用GetDailyUV获取最近7天,取最后一天即为今日(或者根据前端需要展示7天趋势)
|
||
uvTrend, err := repositories.GetDailyUV()
|
||
if err != nil {
|
||
uvTrend = []struct {
|
||
Date string
|
||
Count int
|
||
}{}
|
||
}
|
||
|
||
// 3. 用户画像-地域分布
|
||
userRegions, err := repositories.GetUserRegions()
|
||
if err != nil {
|
||
userRegions = []struct {
|
||
Region string
|
||
Count int
|
||
}{}
|
||
}
|
||
|
||
// 4. 热门文章 Top 5
|
||
// 需要在Post repository增加方法
|
||
topPosts, err := repositories.GetTopPosts(5)
|
||
if err != nil {
|
||
topPosts = nil // Fallback
|
||
}
|
||
|
||
// 5. 合作咨询总数
|
||
var inquiryCount int
|
||
config.DB.QueryRow("SELECT COUNT(*) FROM inquiries").Scan(&inquiryCount)
|
||
|
||
c.JSON(http.StatusOK, gin.H{
|
||
"postsTrend": postsTrend,
|
||
"uvTrend": uvTrend,
|
||
"userRegions": userRegions,
|
||
"topPosts": topPosts,
|
||
"inquiryCount": inquiryCount,
|
||
})
|
||
}
|