Skip to content

Commit

Permalink
fix related archive number
Browse files Browse the repository at this point in the history
  • Loading branch information
fesiong committed Dec 18, 2023
1 parent c9c5a6c commit e8b2d41
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 55 deletions.
56 changes: 29 additions & 27 deletions controller/apiTag.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ func ApiArchiveList(ctx iris.Context) {
return tx
}, 0, limit, offset)
} else {
newLimit := int(math.Ceil(float64(limit) / 2))
archives, _, _ = currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
halfLimit := int(math.Ceil(float64(limit) / 2))
archives1, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
Expand All @@ -344,37 +344,39 @@ func ApiArchiveList(ctx iris.Context) {
tx = tx.Where("archives.`id` > ?", archiveId).Order("archives.id ASC")
return tx
}, 0, limit, offset)
if limit-len(archives) < newLimit {
newLimit = limit - len(archives)
}
if newLimit > 0 {
archives2, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
archives2, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id = ?", categoryId)
} else {
tx = tx.Where("`category_id` = ?", categoryId)
}
if len(excludeCategoryIds) > 0 {
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id = ?", categoryId)
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id NOT IN (?)", excludeCategoryIds)
} else {
tx = tx.Where("`category_id` = ?", categoryId)
}
if len(excludeCategoryIds) > 0 {
if currentSite.Content.MultiCategory == 1 {
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id NOT IN (?)", excludeCategoryIds)
} else {
tx = tx.Where("`category_id` NOT IN (?)", excludeCategoryIds)
}
tx = tx.Where("`category_id` NOT IN (?)", excludeCategoryIds)
}
tx = tx.Where("archives.`id` < ?", archiveId).Order("archives.id DESC")
return tx
}, 0, newLimit, offset)
//列表不返回content
if len(archives2) > 0 {
archives = append(archives2, archives...)
}
// 如果数量超过,则截取
if len(archives) > limit {
archives = archives[:limit]
tx = tx.Where("archives.`id` < ?", archiveId).Order("archives.id DESC")
return tx
}, 0, limit, offset)
if len(archives1)+len(archives2) > limit {
if len(archives1) > halfLimit && len(archives2) > halfLimit {
archives1 = archives1[:halfLimit]
archives2 = archives2[:halfLimit]
} else if len(archives1) > len(archives2) {
archives1 = archives1[:limit-len(archives2)]
} else if len(archives2) > len(archives1) {
archives2 = archives2[:limit-len(archives1)]
}
}
archives = append(archives2, archives1...)
// 如果数量超过,则截取
if len(archives) > limit {
archives = archives[:limit]
}
}
} else {
extraFields := map[uint]map[string]*model.CustomField{}
Expand Down
58 changes: 30 additions & 28 deletions tags/archiveList.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,8 @@ func (node *tagArchiveListNode) Execute(ctx *pongo2.ExecutionContext, writer pon
return tx
}, 0, limit, offset)
} else {
newLimit := int(math.Ceil(float64(limit) / 2))
archives, _, _ = currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
halfLimit := int(math.Ceil(float64(limit) / 2))
archives1, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
Expand All @@ -268,39 +268,41 @@ func (node *tagArchiveListNode) Execute(ctx *pongo2.ExecutionContext, writer pon
}
}
tx = tx.Where("archives.`id` > ?", archiveId).Order("archives.id ASC")
return tx
return tx.Debug()
}, 0, limit, offset)
if limit-len(archives) < newLimit {
newLimit = limit - len(archives)
}
if newLimit > 0 {
archives2, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
archives2, _, _ := currentSite.GetArchiveList(func(tx *gorm.DB) *gorm.DB {
tx = tx.Where("`module_id` = ? AND `status` = 1", moduleId)
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id = ?", categoryId)
} else {
tx = tx.Where("`category_id` = ?", categoryId)
}
if len(excludeCategoryIds) > 0 {
if currentSite.Content.MultiCategory == 1 {
// 多分类支持
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id = ?", categoryId)
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id NOT IN (?)", excludeCategoryIds)
} else {
tx = tx.Where("`category_id` = ?", categoryId)
}
if len(excludeCategoryIds) > 0 {
if currentSite.Content.MultiCategory == 1 {
tx = tx.Joins("STRAIGHT_JOIN archive_categories ON archives.id = archive_categories.archive_id and archive_categories.category_id NOT IN (?)", excludeCategoryIds)
} else {
tx = tx.Where("`category_id` NOT IN (?)", excludeCategoryIds)
}
tx = tx.Where("`category_id` NOT IN (?)", excludeCategoryIds)
}
tx = tx.Where("archives.`id` < ?", archiveId).Order("archives.id DESC")
return tx
}, 0, newLimit, offset)
//列表不返回content
if len(archives2) > 0 {
archives = append(archives2, archives...)
}
// 如果数量超过,则截取
if len(archives) > limit {
archives = archives[:limit]
tx = tx.Where("archives.`id` < ?", archiveId).Order("archives.id DESC")
return tx.Debug()
}, 0, limit, offset)
if len(archives1)+len(archives2) > limit {
if len(archives1) > halfLimit && len(archives2) > halfLimit {
archives1 = archives1[:halfLimit]
archives2 = archives2[:halfLimit]
} else if len(archives1) > len(archives2) {
archives1 = archives1[:limit-len(archives2)]
} else if len(archives2) > len(archives1) {
archives2 = archives2[:limit-len(archives1)]
}
}
archives = append(archives2, archives1...)
// 如果数量超过,则截取
if len(archives) > limit {
archives = archives[:limit]
}
}
} else {
var fulltextSearch bool
Expand Down
16 changes: 16 additions & 0 deletions template/default/article/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ <h1>{% archiveDetail with name="Title" %}</h1>
{% endif %}
{% endnextArchive %}
</div>
<div>
<h2>Related</h2>
<ul>
{% archiveList related with type="related" limit="4" %}
{% for item in related %}
<li>
<div class="list-item">
<div class="list-item-text">
<h3><a href="{{item.Link}}">{{item.Title}}</a></h3>
</div>
</div>
</li>
{% endfor %}
{% endarchiveList %}
</ul>
</div>
</div>
<!-- content -->
<!-- sidebar -->
Expand Down

0 comments on commit e8b2d41

Please sign in to comment.