优化搜索结果
This commit is contained in:
@@ -123,22 +123,32 @@
|
|||||||
.result-meta {
|
.result-meta {
|
||||||
display: flex;
|
display: flex;
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
align-items: center;
|
align-items: flex-start;
|
||||||
font-size: 0.9rem;
|
font-size: 0.9rem;
|
||||||
color: var(--fontcolor);
|
color: var(--fontcolor);
|
||||||
opacity: 0.8;
|
opacity: 0.8;
|
||||||
margin-top: 15px;
|
margin-top: 15px;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.meta-stats {
|
.meta-stats {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
gap: 15px;
|
gap: 15px;
|
||||||
|
flex-wrap: wrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
.meta-pulls {
|
.meta-pulls {
|
||||||
font-size: 0.85rem;
|
font-size: 0.85rem;
|
||||||
color: #666;
|
color: #666;
|
||||||
|
text-align: right;
|
||||||
|
}
|
||||||
|
|
||||||
|
.meta-pulls .pulls-count {
|
||||||
|
font-size: 1rem;
|
||||||
|
color: #333;
|
||||||
|
margin-top: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.badge {
|
.badge {
|
||||||
@@ -378,6 +388,18 @@
|
|||||||
.back-to-search:hover {
|
.back-to-search:hover {
|
||||||
text-decoration: underline;
|
text-decoration: underline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@media (max-width: 768px) {
|
||||||
|
.result-meta {
|
||||||
|
flex-direction: column;
|
||||||
|
align-items: flex-start;
|
||||||
|
}
|
||||||
|
|
||||||
|
.meta-pulls {
|
||||||
|
text-align: left;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -536,7 +558,25 @@
|
|||||||
if (!dateString) return '未知时间';
|
if (!dateString) return '未知时间';
|
||||||
|
|
||||||
const date = new Date(dateString);
|
const date = new Date(dateString);
|
||||||
if (isNaN(date.getTime())) return '未知时间';
|
if (isNaN(date.getTime())) {
|
||||||
|
// 尝试解析其他日期格式
|
||||||
|
const formats = [
|
||||||
|
'YYYY-MM-DD',
|
||||||
|
'YYYY/MM/DD',
|
||||||
|
'MM/DD/YYYY',
|
||||||
|
'DD/MM/YYYY'
|
||||||
|
];
|
||||||
|
|
||||||
|
for (const format of formats) {
|
||||||
|
const d = tryParseDate(dateString, format);
|
||||||
|
if (d && !isNaN(d.getTime())) {
|
||||||
|
date = d;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNaN(date.getTime())) return '未知时间';
|
||||||
|
}
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const diffTime = Math.abs(now - date);
|
const diffTime = Math.abs(now - date);
|
||||||
@@ -546,6 +586,7 @@
|
|||||||
const diffMonths = Math.floor(diffDays / 30);
|
const diffMonths = Math.floor(diffDays / 30);
|
||||||
const diffYears = Math.floor(diffDays / 365);
|
const diffYears = Math.floor(diffDays / 365);
|
||||||
|
|
||||||
|
if (diffMinutes < 5) return '几秒前';
|
||||||
if (diffMinutes < 60) return `${diffMinutes}分钟前`;
|
if (diffMinutes < 60) return `${diffMinutes}分钟前`;
|
||||||
if (diffHours < 24) return `${diffHours}小时前`;
|
if (diffHours < 24) return `${diffHours}小时前`;
|
||||||
if (diffDays < 7) return `${diffDays}天前`;
|
if (diffDays < 7) return `${diffDays}天前`;
|
||||||
@@ -554,6 +595,21 @@
|
|||||||
return `${diffYears}年前`;
|
return `${diffYears}年前`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function tryParseDate(dateString, format) {
|
||||||
|
const parts = dateString.split(/[-/]/);
|
||||||
|
const formatParts = format.split(/[-/]/);
|
||||||
|
const dateObj = {};
|
||||||
|
|
||||||
|
for (let i = 0; i < formatParts.length; i++) {
|
||||||
|
const part = formatParts[i].toUpperCase();
|
||||||
|
if (part === 'YYYY') dateObj.year = parseInt(parts[i]);
|
||||||
|
else if (part === 'MM') dateObj.month = parseInt(parts[i]) - 1;
|
||||||
|
else if (part === 'DD') dateObj.day = parseInt(parts[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Date(dateObj.year, dateObj.month, dateObj.day);
|
||||||
|
}
|
||||||
|
|
||||||
function displayResults(results) {
|
function displayResults(results) {
|
||||||
const resultsContainer = document.getElementById('searchResults');
|
const resultsContainer = document.getElementById('searchResults');
|
||||||
resultsContainer.innerHTML = '';
|
resultsContainer.innerHTML = '';
|
||||||
@@ -563,6 +619,14 @@
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 对结果进行排序:官方镜像优先,然后按照拉取次数排序
|
||||||
|
results.sort((a, b) => {
|
||||||
|
if (a.is_official !== b.is_official) {
|
||||||
|
return b.is_official - a.is_official;
|
||||||
|
}
|
||||||
|
return b.pull_count - a.pull_count;
|
||||||
|
});
|
||||||
|
|
||||||
results.forEach(result => {
|
results.forEach(result => {
|
||||||
const card = document.createElement('div');
|
const card = document.createElement('div');
|
||||||
card.className = 'result-card';
|
card.className = 'result-card';
|
||||||
@@ -597,14 +661,14 @@
|
|||||||
</div>
|
</div>
|
||||||
${pullsLastWeek > 0 ? `
|
${pullsLastWeek > 0 ? `
|
||||||
<div class="meta-pulls">
|
<div class="meta-pulls">
|
||||||
<span class="meta-item">本周拉取: ${formatNumber(pullsLastWeek)}</span>
|
<div>本周拉取次数</div>
|
||||||
|
<div class="pulls-count">${formatNumber(pullsLastWeek)}</div>
|
||||||
</div>
|
</div>
|
||||||
` : ''}
|
` : ''}
|
||||||
</div>
|
</div>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
card.addEventListener('click', () => {
|
card.addEventListener('click', () => {
|
||||||
console.log('点击仓库:', name);
|
|
||||||
currentRepo = result;
|
currentRepo = result;
|
||||||
loadTags(result.is_official ? 'library' : (result.repo_owner || ''), name);
|
loadTags(result.is_official ? 'library' : (result.repo_owner || ''), name);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ func RegisterSearchRoute(r *gin.Engine) {
|
|||||||
|
|
||||||
// 如果是搜索官方镜像
|
// 如果是搜索官方镜像
|
||||||
if !strings.Contains(query, "/") {
|
if !strings.Contains(query, "/") {
|
||||||
query = "library/" + query
|
query = strings.ToLower(query)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("搜索请求: query=%s, page=%d, pageSize=%d\n", query, page, pageSize)
|
fmt.Printf("搜索请求: query=%s, page=%d, pageSize=%d\n", query, page, pageSize)
|
||||||
@@ -270,12 +270,22 @@ func RegisterSearchRoute(r *gin.Engine) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理官方镜像的情况
|
// 过滤搜索结果,只保留相关的镜像
|
||||||
for i := range result.Results {
|
filteredResults := make([]Repository, 0)
|
||||||
if result.Results[i].IsOfficial {
|
searchTerm := strings.ToLower(strings.TrimPrefix(query, "library/"))
|
||||||
result.Results[i].Name = strings.TrimPrefix(result.Results[i].Name, "library/")
|
|
||||||
|
for _, repo := range result.Results {
|
||||||
|
repoName := strings.ToLower(repo.Name)
|
||||||
|
// 如果是精确匹配或者以搜索词开头,或者包含 "searchTerm/searchTerm"
|
||||||
|
if repoName == searchTerm ||
|
||||||
|
strings.HasPrefix(repoName, searchTerm+"/") ||
|
||||||
|
strings.Contains(repoName, "/"+searchTerm) {
|
||||||
|
filteredResults = append(filteredResults, repo)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
result.Results = filteredResults
|
||||||
|
result.Count = len(filteredResults)
|
||||||
|
|
||||||
c.JSON(http.StatusOK, result)
|
c.JSON(http.StatusOK, result)
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user