mirror of
https://github.com/ctwj/urldb.git
synced 2025-11-25 03:15:04 +08:00
fix: 搜索记录重复的问题
This commit is contained in:
@@ -20,7 +20,11 @@ func GetResources(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
||||
|
||||
utils.Info("资源列表请求 - page: %d, pageSize: %d", page, pageSize)
|
||||
utils.Info("资源列表请求 - page: %d, pageSize: %d, User-Agent: %s", page, pageSize, c.GetHeader("User-Agent"))
|
||||
|
||||
// 添加缓存控制头,优化 SSR 性能
|
||||
c.Header("Cache-Control", "public, max-age=30") // 30秒缓存,平衡性能和实时性
|
||||
c.Header("ETag", fmt.Sprintf("resources-%d-%d-%s-%s", page, pageSize, c.Query("search"), c.Query("pan_id")))
|
||||
|
||||
params := map[string]interface{}{
|
||||
"page": page,
|
||||
@@ -62,16 +66,6 @@ func GetResources(c *gin.Context) {
|
||||
|
||||
resources, total, err := repoManager.ResourceRepository.SearchWithFilters(params)
|
||||
|
||||
// 搜索统计(仅非管理员)
|
||||
if search, ok := params["search"].(string); ok && search != "" {
|
||||
user, _ := c.Get("user")
|
||||
if user == nil || (user != nil && user.(entity.User).Role != "admin") {
|
||||
ip := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
repoManager.SearchStatRepository.RecordSearch(search, ip, userAgent)
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
ErrorResponse(c, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -283,10 +277,6 @@ func SearchResources(c *gin.Context) {
|
||||
} else {
|
||||
// 有搜索关键词时,执行搜索
|
||||
resources, total, err = repoManager.ResourceRepository.Search(query, nil, page, pageSize)
|
||||
// 新增:记录搜索关键词
|
||||
ip := c.ClientIP()
|
||||
userAgent := c.GetHeader("User-Agent")
|
||||
repoManager.SearchStatRepository.RecordSearch(query, ip, userAgent)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -148,6 +148,25 @@ export const useStatsApi = () => {
|
||||
return { getStats }
|
||||
}
|
||||
|
||||
export const useSearchStatsApi = () => {
|
||||
const getSearchStats = (params?: any) => useApiFetch('/search-stats', { params }).then(parseApiResponse)
|
||||
const getHotKeywords = (params?: any) => useApiFetch('/search-stats/hot-keywords', { params }).then(parseApiResponse)
|
||||
const getDailyStats = (params?: any) => useApiFetch('/search-stats/daily', { params }).then(parseApiResponse)
|
||||
const getSearchTrend = (params?: any) => useApiFetch('/search-stats/trend', { params }).then(parseApiResponse)
|
||||
const getKeywordTrend = (keyword: string, params?: any) => useApiFetch(`/search-stats/keyword/${keyword}/trend`, { params }).then(parseApiResponse)
|
||||
const getSearchStatsSummary = () => useApiFetch('/search-stats/summary').then(parseApiResponse)
|
||||
const recordSearch = (data: { keyword: string }) => useApiFetch('/search-stats/record', { method: 'POST', body: data }).then(parseApiResponse)
|
||||
return {
|
||||
getSearchStats,
|
||||
getHotKeywords,
|
||||
getDailyStats,
|
||||
getSearchTrend,
|
||||
getKeywordTrend,
|
||||
getSearchStatsSummary,
|
||||
recordSearch
|
||||
}
|
||||
}
|
||||
|
||||
export const useSystemConfigApi = () => {
|
||||
const getSystemConfig = () => useApiFetch('/system/config').then(parseApiResponse)
|
||||
const updateSystemConfig = (data: any) => useApiFetch('/system/config', { method: 'POST', body: data }).then(parseApiResponse)
|
||||
|
||||
@@ -62,7 +62,13 @@ export default defineNuxtConfig({
|
||||
},
|
||||
ssr: true,
|
||||
nitro: {
|
||||
logLevel: 'verbose',
|
||||
preset: 'node-server'
|
||||
logLevel: 'info',
|
||||
preset: 'node-server',
|
||||
storage: {
|
||||
redis: {
|
||||
driver: 'memory',
|
||||
max: 1000
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -230,7 +230,7 @@ useHead({
|
||||
// 获取运行时配置
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
import { useResourceApi, useStatsApi, usePanApi, useSystemConfigApi, usePublicSystemConfigApi } from '~/composables/useApi'
|
||||
import { useResourceApi, useStatsApi, usePanApi, useSystemConfigApi, usePublicSystemConfigApi, useSearchStatsApi } from '~/composables/useApi'
|
||||
|
||||
const resourceApi = useResourceApi()
|
||||
const statsApi = useStatsApi()
|
||||
@@ -329,6 +329,20 @@ const handleSearch = () => {
|
||||
// 初始化认证状态
|
||||
onMounted(() => {
|
||||
animateCounters()
|
||||
|
||||
// 页面挂载完成时,如果有搜索关键词,请求 record 接口
|
||||
if (process.client && route.query.search) {
|
||||
const searchKeyword = route.query.search as string
|
||||
if (searchKeyword.trim()) {
|
||||
// 延迟执行,确保页面完全加载
|
||||
setTimeout(() => {
|
||||
const searchStatsApi = useSearchStatsApi()
|
||||
searchStatsApi.recordSearch({ keyword: searchKeyword }).catch(err => {
|
||||
console.error('记录搜索统计失败:', err)
|
||||
})
|
||||
}, 0)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user