分页器

This commit is contained in:
xing 2022-09-15 22:35:32 +08:00
parent 046f2e2514
commit 912ec05876
10 changed files with 212 additions and 53 deletions

View File

@ -22,3 +22,12 @@ func StructColumn[T any, M any](arr []M, field string) (r []T) {
} }
return return
} }
func RangeSlice[T ~int | ~uint | ~int64 | ~int8 | ~int16 | ~int32 | ~uint64](start, end, step T) []T {
r := make([]T, 0, int(end/step+1))
for i := start; i <= end; {
r = append(r, i)
i = i + step
}
return r
}

View File

@ -73,3 +73,42 @@ func TestStructColumn(t *testing.T) {
}) })
} }
} }
func TestRangeSlice(t *testing.T) {
type args struct {
start int
end int
step int
}
tests := []struct {
name string
args args
want []int
}{
{
name: "t1",
args: args{
start: 1,
end: 5,
step: 1,
},
want: []int{1, 2, 3, 4, 5},
},
{
name: "t2",
args: args{
start: 0,
end: 5,
step: 2,
},
want: []int{0, 2, 4},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RangeSlice(tt.args.start, tt.args.end, tt.args.step); !reflect.DeepEqual(got, tt.want) {
t.Errorf("RangeSlice() = %v, want %v", got, tt.want)
}
})
}
}

View File

@ -1,7 +1,8 @@
package models package models
var Options = make(map[string]string) var Options = make(map[string]string)
var TermsIds []uint64 var Terms = map[uint64]WpTerms{}
var TermTaxonomy = map[uint64]WpTermTaxonomy{}
func InitOptions() error { func InitOptions() error {
ops, err := SimpleFind[WpOptions](SqlBuilder{{"autoload", "yes"}}, "option_name, option_value") ops, err := SimpleFind[WpOptions](SqlBuilder{{"autoload", "yes"}}, "option_name, option_value")
@ -21,17 +22,19 @@ func InitOptions() error {
} }
func InitTerms() (err error) { func InitTerms() (err error) {
var themes []interface{} terms, err := Find[WpTerms](nil, "*", "", nil, nil, 0)
themes = append(themes, "wp_theme") if err != nil {
var name []interface{} return err
name = append(name, "twentyfifteen") }
terms, err := Find[WpTerms](SqlBuilder{{
"tt.taxonomy", "in", "",
}, {"t.name", "in", ""}}, "t.term_id", "", nil, SqlBuilder{{
"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id",
}}, 1, themes, name)
for _, wpTerms := range terms { for _, wpTerms := range terms {
TermsIds = append(TermsIds, wpTerms.TermId) Terms[wpTerms.TermId] = wpTerms
}
termTax, err := Find[WpTermTaxonomy](nil, "*", "", nil, nil, 0)
if err != nil {
return err
}
for _, taxonomy := range termTax {
TermTaxonomy[taxonomy.TermTaxonomyId] = taxonomy
} }
return return
} }

View File

@ -89,9 +89,9 @@ func (w SqlBuilder) parseOrderBy() string {
s := strings.Builder{} s := strings.Builder{}
for _, ss := range w { for _, ss := range w {
if len(ss) == 2 && ss[0] != "" && helper.IsContainInArr(ss[1], []string{"asc", "desc"}) { if len(ss) == 2 && ss[0] != "" && helper.IsContainInArr(ss[1], []string{"asc", "desc"}) {
s.WriteString(" `") s.WriteString(" ")
s.WriteString(ss[0]) s.WriteString(ss[0])
s.WriteString("` ") s.WriteString(" ")
s.WriteString(ss[1]) s.WriteString(ss[1])
s.WriteString(",") s.WriteString(",")
} }
@ -215,7 +215,12 @@ func Select[T Model](sql string, params ...interface{}) ([]T, error) {
func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) { func Find[T Model](where ParseWhere, fields, group string, order SqlBuilder, join SqlBuilder, limit int, in ...[]interface{}) (r []T, err error) {
var rr T var rr T
w, args := where.ParseWhere(in...) w := ""
var args []interface{}
if where != nil {
w, args = where.ParseWhere(in...)
}
j := join.parseJoin() j := join.parseJoin()
groupBy := "" groupBy := ""
if group != "" { if group != "" {

View File

@ -13,3 +13,15 @@ func (t WpTerms) PrimaryKey() string {
func (t WpTerms) Table() string { func (t WpTerms) Table() string {
return "wp_terms" return "wp_terms"
} }
type WpTermsMy struct {
WpTerms
WpTermTaxonomy
}
func (t WpTermsMy) PrimaryKey() string {
return "term_id"
}
func (t WpTermsMy) Table() string {
return "wp_terms"
}

View File

@ -1,10 +1,13 @@
package route package route
import ( import (
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github/fthvgb1/wp-go/models" "github/fthvgb1/wp-go/models"
"math"
"net/http" "net/http"
"strconv" "strconv"
"strings"
"sync" "sync"
) )
@ -14,13 +17,24 @@ func index(c *gin.Context) {
page := 1 page := 1
pageSize := 10 pageSize := 10
p := c.Query("paged") p := c.Query("paged")
if pa, err := strconv.Atoi(p); err != nil { if p == "" {
p = c.Param("page")
}
if p != "" {
if pa, err := strconv.Atoi(p); err == nil {
page = pa page = pa
} }
}
status := []interface{}{"publish", "private"} status := []interface{}{"publish", "private"}
posts, _, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{ posts, totalRaw, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{
"post_type", "post", "post_type", "post",
}, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", "desc"}}, nil, status) }, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", "desc"}}, nil, status)
defer func() {
if err != nil {
c.Error(err)
}
}()
if err != nil { if err != nil {
return return
} }
@ -56,14 +70,23 @@ func index(c *gin.Context) {
pp := post.(models.WpPosts) pp := post.(models.WpPosts)
allPosts = append(allPosts, pp) allPosts = append(allPosts, pp)
} }
recent, _ := recentPosts() recent, err := recentPosts()
archive, _ := archives() archive, err := archives()
categoryItems, err := categories()
totalPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
q := c.Request.URL.Query().Encode()
if q != "" {
q = fmt.Sprintf("?%s", q)
}
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"posts": allPosts, "posts": allPosts,
"options": models.Options, "options": models.Options,
"recentPosts": recent, "recentPosts": recent,
"archives": archive, "archives": archive,
"categories": categoryItems,
"totalPage": totalPage,
"queryRaw": q,
"pagination": pagination(page, totalPage, 1, q),
}) })
} }
@ -74,9 +97,97 @@ func recentPosts() (r []models.WpPosts, err error) {
return return
} }
func categories() (terms []models.WpTermsMy, err error) {
var in = []interface{}{"category"}
terms, err = models.Find[models.WpTermsMy](models.SqlBuilder{
{"tt.count", ">", "0", "int"},
{"tt.taxonomy", "in", ""},
}, "t.term_id", "", models.SqlBuilder{
{"t.name", "asc"},
}, models.SqlBuilder{
{"t", "inner join", "wp_term_taxonomy tt", "t.term_id = tt.term_id"},
}, 0, in)
for i := 0; i < len(terms); i++ {
if v, ok := models.Terms[terms[i].WpTerms.TermId]; ok {
terms[i].WpTerms = v
}
if v, ok := models.TermTaxonomy[terms[i].WpTerms.TermId]; ok {
terms[i].WpTermTaxonomy = v
}
}
return
}
func archives() (r []models.PostArchive, err error) { func archives() (r []models.PostArchive, err error) {
r, err = models.Find[models.PostArchive](models.SqlBuilder{ r, err = models.Find[models.PostArchive](models.SqlBuilder{
{"post_type", "post"}, {"post_status", "publish"}, {"post_type", "post"}, {"post_status", "publish"},
}, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0) }, "YEAR(post_date) AS `year`, MONTH(post_date) AS `month`, count(ID) as posts", "year,month", models.SqlBuilder{{"year", "desc"}, {"month", "desc"}}, nil, 0)
return return
} }
func pagination(currentPage, totalPage, step int, query string) (html string) {
html = ""
s := strings.Builder{}
if currentPage > totalPage {
currentPage = totalPage
}
start := currentPage - step
end := currentPage + step
if start < 1 {
start = currentPage
}
if currentPage > 1 {
pp := ""
if currentPage > 2 {
pp = fmt.Sprintf("page/%d", currentPage-1)
}
s.WriteString(fmt.Sprintf(`<a class="prev page-numbers" href="/%s%s">上一页</a>`, pp, query))
}
if currentPage >= step+2 {
d := ""
if currentPage > step+2 {
d = `<span class="page-numbers dots">…</span>`
}
s.WriteString(fmt.Sprintf(`
<a class="page-numbers" href="/%s"><span class="meta-nav screen-reader-text"> </span>1</a>
%s
`, query, d))
}
if totalPage < end {
end = totalPage
}
for page := start; page <= end; page++ {
h := ""
if currentPage == page {
h = fmt.Sprintf(`
<span aria-current="page" class="page-numbers current">
<span class="meta-nav screen-reader-text"> </span>%d</span>
`, page)
} else {
d := fmt.Sprintf("/page/%d", page)
if currentPage > page && page == 1 {
d = "/"
}
h = fmt.Sprintf(`
<a class="page-numbers" href="%s%s">
<span class="meta-nav screen-reader-text"> </span>%d</a>
`, d, query, page)
}
s.WriteString(h)
}
if totalPage > currentPage+step+2 {
s.WriteString(fmt.Sprintf(`
<span class="page-numbers dots"></span>
<a class="page-numbers" href="/page/%d%s"><span class="meta-nav screen-reader-text"> </span>%d</a>`, totalPage, query, totalPage))
}
if currentPage < totalPage {
s.WriteString(fmt.Sprintf(`<a class="next page-numbers" href="/page/%d%s">下一页</a>`, currentPage+1, query))
}
html = s.String()
return
}

View File

@ -31,6 +31,7 @@ func SetupRouter() *gin.Engine {
})) }))
loadTemplates(r, "**/*") loadTemplates(r, "**/*")
r.GET("/", index) r.GET("/", index)
r.GET("/page/:page", index)
return r return r
} }

View File

@ -38,18 +38,7 @@
</article><!-- #post-{{$v.Id}} --> </article><!-- #post-{{$v.Id}} -->
{{end}} {{end}}
{{template "layout/page" .}}
<nav class="navigation pagination" aria-label="文章">
<h2 class="screen-reader-text">文章导航</h2>
<div class="nav-links"><span aria-current="page" class="page-numbers current"><span
class="meta-nav screen-reader-text">页 </span>1</span>
<a class="page-numbers" href="/page/2"><span
class="meta-nav screen-reader-text">页 </span>2</a>
<span class="page-numbers dots"></span>
<a class="page-numbers" href="/page/37"><span
class="meta-nav screen-reader-text">页 </span>37</a>
<a class="next page-numbers" href="/page/2">下一页</a></div>
</nav>
</main><!-- .site-main --> </main><!-- .site-main -->
</div> </div>
{{end}} {{end}}

View File

@ -0,0 +1,8 @@
{{define "layout/page"}}
<nav class="navigation pagination" aria-label="文章">
<h2 class="screen-reader-text">文章导航</h2>
<div class="nav-links">
{{.pagination|unescaped}}
</div>
</nav>
{{end}}

View File

@ -40,27 +40,9 @@
<aside id="categories-2" class="widget widget_categories"><h2 class="widget-title">分类</h2> <aside id="categories-2" class="widget widget_categories"><h2 class="widget-title">分类</h2>
<nav aria-label="分类"> <nav aria-label="分类">
<ul> <ul>
<li class="cat-item cat-item-28"><a href="https://www.xloyy.com/p/category/docker">docker</a> {{range $k,$v := .categories}}
</li> <li class="cat-item cat-item-{{$v.WpTerms.TermId}}"><a href="https://www.xloyy.com/p/category/{{$v.Name}}">{{$v.Name}}</a>
<li class="cat-item cat-item-2"><a href="https://www.xloyy.com/p/category/linux">linux</a> {{end}}
</li>
<li class="cat-item cat-item-20"><a href="https://www.xloyy.com/p/category/mysql">mysql</a>
</li>
<li class="cat-item cat-item-14"><a href="https://www.xloyy.com/p/category/php">php</a>
</li>
<li class="cat-item cat-item-15"><a href="https://www.xloyy.com/p/category/web">web</a>
</li>
<li class="cat-item cat-item-24"><a
href="https://www.xloyy.com/p/category/%e4%b8%bb%e6%9c%ba%e6%b8%b8%e6%88%8f">主机游戏</a>
</li>
<li class="cat-item cat-item-16"><a href="https://www.xloyy.com/p/category/%e5%85%b6%e5%ae%83">其它</a>
</li>
<li class="cat-item cat-item-25"><a href="https://www.xloyy.com/p/category/%e6%97%a5%e8%ae%b0">日记</a>
</li>
<li class="cat-item cat-item-1"><a href="https://www.xloyy.com/p/category/uncategorized">未分类</a>
</li>
<li class="cat-item cat-item-26"><a href="https://www.xloyy.com/p/category/%e6%9d%82%e8%b0%88">杂谈</a>
</li>
</ul> </ul>
</nav> </nav>