Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1016a10

Browse files
committed
articles 新的分页
1 parent 3dede90 commit 1016a10

File tree

2 files changed

+31
-58
lines changed

2 files changed

+31
-58
lines changed

src/http/controller/article.go

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/polaris1119/goutils"
1818
"github.com/polaris1119/logger"
1919

20+
"html/template"
2021
. "http"
2122
"model"
2223
)
@@ -45,73 +46,53 @@ func (self ArticleController) RegisterRoute(g *echo.Group) {
4546
func (ArticleController) ReadList(ctx echo.Context) error {
4647
limit := 20
4748

48-
lastId := goutils.MustInt(ctx.QueryParam("lastid"))
49-
articles := logic.DefaultArticle.FindBy(ctx, limit+5, lastId)
49+
curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
50+
paginator := logic.NewPaginator(curPage)
51+
paginator.SetPerPage(limit)
52+
53+
// TODO: 参考的 topics 的处理方式,但是感觉不应该这样做
54+
topArticles := logic.DefaultArticle.FindAll(ctx, paginator, "id DESC", "top=1")
55+
unTopArticles := logic.DefaultArticle.FindAll(ctx, paginator, "id DESC", "top!=1")
56+
articles := append(topArticles, unTopArticles...)
5057
if articles == nil {
5158
logger.Errorln("article controller: find article error")
5259
return ctx.Redirect(http.StatusSeeOther, "/articles")
5360
}
5461

5562
num := len(articles)
5663
if num == 0 {
57-
if lastId == 0 {
64+
if curPage == 1 {
5865
return render(ctx, "articles/list.html", map[string]interface{}{"articles": articles, "activeArticles": "active"})
5966
}
6067
return ctx.Redirect(http.StatusSeeOther, "/articles")
6168
}
6269

63-
// 旧的分页
64-
var (
65-
hasPrev, hasNext bool
66-
prevId, nextId int
67-
)
70+
total := logic.DefaultArticle.Count(ctx, "")
71+
pageHtml := paginator.SetTotal(total).GetPageHtml(ctx.Request().URL().Path())
72+
pageInfo := template.HTML(pageHtml)
6873

69-
if lastId != 0 {
70-
prevId = lastId
74+
// 获取当前用户喜欢对象信息
75+
me, ok := ctx.Get("user").(*model.Me)
76+
var topLikeFlags map[int]int
77+
var unTopLikeFlags map[int]int
78+
likeFlags := map[int]int{}
7179

72-
firstNoTopId := articles[0].Id
73-
for i := 0; i < num; i++ {
74-
if articles[i].Top != 1 {
75-
firstNoTopId = articles[i].Id
76-
break
80+
if ok {
81+
topArticlesNum := len(topArticles)
82+
if topArticlesNum > 0 {
83+
topLikeFlags, _ = logic.DefaultLike.FindUserLikeObjects(ctx, me.Uid, model.TypeArticle, topArticles[0].Id, topArticles[topArticlesNum-1].Id)
84+
for k, v := range topLikeFlags {
85+
likeFlags[k] = v
7786
}
7887
}
79-
// 避免因为文章下线,导致判断错误(所以 > 5)
80-
if prevId-firstNoTopId > 5 {
81-
hasPrev = false
82-
} else {
83-
prevId += limit
84-
hasPrev = true
85-
}
86-
}
8788

88-
if num > limit {
89-
hasNext = true
90-
articles = articles[:limit]
91-
nextId = articles[limit-1].Id
92-
} else {
93-
nextId = articles[num-1].Id
94-
}
95-
96-
pageInfo := map[string]interface{}{
97-
"has_prev": hasPrev,
98-
"prev_id": prevId,
99-
"has_next": hasNext,
100-
"next_id": nextId,
101-
}
102-
103-
// 新分页
104-
//curPage := goutils.MustInt(ctx.QueryParam("p"), 1)
105-
//paginator := logic.NewPaginator(curPage)
106-
//total := logic.DefaultArticle.Count(ctx, "")
107-
//pageHtml := paginator.SetTotal(total).GetPageHtml(ctx.Request().URL().Path())
108-
//pageInfo := template.HTML(pageHtml)
109-
110-
// 获取当前用户喜欢对象信息
111-
me, ok := ctx.Get("user").(*model.Me)
112-
var likeFlags map[int]int
113-
if ok {
114-
likeFlags, _ = logic.DefaultLike.FindUserLikeObjects(ctx, me.Uid, model.TypeArticle, articles[0].Id, nextId)
89+
unTopArticlesNum := len(unTopArticles)
90+
if unTopArticlesNum > 0 {
91+
unTopLikeFlags, _ = logic.DefaultLike.FindUserLikeObjects(ctx, me.Uid, model.TypeArticle, unTopArticles[0].Id, unTopArticles[unTopArticlesNum-1].Id)
92+
for k, v := range unTopLikeFlags {
93+
likeFlags[k] = v
94+
}
95+
}
11596
}
11697

11798
return render(ctx, "articles/list.html", map[string]interface{}{"articles": articles, "activeArticles": "active", "page": pageInfo, "likeflags": likeFlags})

template/articles/list.html

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,6 @@ <h2><a href="/articles/{{.Id}}" target="_blank" title="{{.Title}}">
7575
<div class="row">暂无博文,<a class="btn btn-default btn-sm" href="/articles/new">撰写文章</a></div>
7676
</article>
7777
{{end}}
78-
<ul class="pager">
79-
{{if .page.has_prev}}
80-
<li class="previous"><a href="/articles?lastid={{.page.prev_id}}">&larr; 上一页</a></li>
81-
{{end}}
82-
{{if .page.has_next}}
83-
<li class="next"><a href="/articles?lastid={{.page.next_id}}">下一页 &rarr;</a></li>
84-
{{end}}
85-
</ul>
8678
<div class="sep20"></div>
8779
{{if .page}}
8880
<div class="box_white">

0 commit comments

Comments
 (0)