完善分页器
This commit is contained in:
parent
cbbb7b8e03
commit
8ab85040a5
|
@ -25,29 +25,38 @@ func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) {
|
||||||
c := 0
|
c := 0
|
||||||
for _, ss := range w {
|
for _, ss := range w {
|
||||||
if len(ss) == 2 {
|
if len(ss) == 2 {
|
||||||
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
s.WriteString("`")
|
||||||
if strings.Contains(ss[0], ".") {
|
|
||||||
sx := strings.Split(ss[0], ".")
|
sx := strings.Split(ss[0], ".")
|
||||||
s.WriteString(sx[0])
|
s.WriteString(sx[0])
|
||||||
s.WriteString("`.`")
|
s.WriteString("`.`")
|
||||||
s.WriteString(sx[1])
|
s.WriteString(sx[1])
|
||||||
|
s.WriteString("`")
|
||||||
|
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
|
s.WriteString("`")
|
||||||
|
s.WriteString(ss[0])
|
||||||
|
s.WriteString("`")
|
||||||
} else {
|
} else {
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
}
|
}
|
||||||
s.WriteString("`=? and ")
|
s.WriteString("=? and ")
|
||||||
args = append(args, ss[1])
|
args = append(args, ss[1])
|
||||||
}
|
}
|
||||||
if len(ss) >= 3 {
|
if len(ss) >= 3 {
|
||||||
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
s.WriteString("`")
|
s.WriteString("`")
|
||||||
if strings.Contains(ss[0], ".") {
|
|
||||||
sx := strings.Split(ss[0], ".")
|
sx := strings.Split(ss[0], ".")
|
||||||
s.WriteString(sx[0])
|
s.WriteString(sx[0])
|
||||||
s.WriteString("`.`")
|
s.WriteString("`.`")
|
||||||
s.WriteString(sx[1])
|
s.WriteString(sx[1])
|
||||||
|
s.WriteString("`")
|
||||||
|
} else if !strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
|
s.WriteString("`")
|
||||||
|
s.WriteString(ss[0])
|
||||||
|
s.WriteString("`")
|
||||||
} else {
|
} else {
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
}
|
}
|
||||||
s.WriteString("`")
|
|
||||||
s.WriteString(ss[1])
|
s.WriteString(ss[1])
|
||||||
if ss[1] == "in" && len(in) > 0 {
|
if ss[1] == "in" && len(in) > 0 {
|
||||||
s.WriteString(" (")
|
s.WriteString(" (")
|
||||||
|
|
|
@ -7,6 +7,7 @@ import (
|
||||||
"github/fthvgb1/wp-go/models"
|
"github/fthvgb1/wp-go/models"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -21,7 +22,28 @@ func index(c *gin.Context) {
|
||||||
if !helper.IsContainInArr(order, []string{"asc", "desc"}) {
|
if !helper.IsContainInArr(order, []string{"asc", "desc"}) {
|
||||||
order = "desc"
|
order = "desc"
|
||||||
}
|
}
|
||||||
|
where := models.SqlBuilder{{
|
||||||
|
"post_type", "post",
|
||||||
|
}, {"post_status", "in", ""}}
|
||||||
p := c.Query("paged")
|
p := c.Query("paged")
|
||||||
|
year := c.Param("year")
|
||||||
|
if year != "" {
|
||||||
|
where = append(where, []string{
|
||||||
|
"year(post_date)", year,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
month := c.Param("month")
|
||||||
|
if month != "" {
|
||||||
|
where = append(where, []string{
|
||||||
|
"month(post_date)", month,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
category := c.Param("category")
|
||||||
|
if category != "" {
|
||||||
|
/*where = append(where, []string{
|
||||||
|
"d.name", category,
|
||||||
|
})*/
|
||||||
|
}
|
||||||
if p == "" {
|
if p == "" {
|
||||||
p = c.Param("page")
|
p = c.Param("page")
|
||||||
}
|
}
|
||||||
|
@ -32,9 +54,7 @@ func index(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
status := []interface{}{"publish", "private"}
|
status := []interface{}{"publish", "private"}
|
||||||
posts, totalRaw, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{
|
posts, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status)
|
||||||
"post_type", "post",
|
|
||||||
}, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status)
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Error(err)
|
c.Error(err)
|
||||||
|
@ -91,7 +111,7 @@ func index(c *gin.Context) {
|
||||||
"categories": categoryItems,
|
"categories": categoryItems,
|
||||||
"totalPage": totalPage,
|
"totalPage": totalPage,
|
||||||
"queryRaw": q,
|
"queryRaw": q,
|
||||||
"pagination": pagination(page, totalPage, 1, q),
|
"pagination": pagination(page, totalPage, 2, c.Request.URL.Path, q),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,13 +151,19 @@ func archives() (r []models.PostArchive, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func pagination(currentPage, totalPage, step int, query string) (html string) {
|
func pagination(currentPage, totalPage, step int, path, query string) (html string) {
|
||||||
html = ""
|
if totalPage < 2 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
pathx := path
|
||||||
|
if !strings.Contains(path, "/page/") {
|
||||||
|
pathx = fmt.Sprintf("%s%s", path, "/page/1")
|
||||||
|
}
|
||||||
s := strings.Builder{}
|
s := strings.Builder{}
|
||||||
if currentPage > totalPage {
|
if currentPage > totalPage {
|
||||||
currentPage = totalPage
|
currentPage = totalPage
|
||||||
}
|
}
|
||||||
|
r := regexp.MustCompile(`(/page)/(\d+)`)
|
||||||
start := currentPage - step
|
start := currentPage - step
|
||||||
end := currentPage + step
|
end := currentPage + step
|
||||||
if start < 1 {
|
if start < 1 {
|
||||||
|
@ -145,20 +171,21 @@ func pagination(currentPage, totalPage, step int, query string) (html string) {
|
||||||
}
|
}
|
||||||
if currentPage > 1 {
|
if currentPage > 1 {
|
||||||
pp := ""
|
pp := ""
|
||||||
if currentPage > 2 {
|
if currentPage >= 2 {
|
||||||
pp = fmt.Sprintf("page/%d", currentPage-1)
|
pp = replacePage(r, pathx, currentPage-1)
|
||||||
}
|
}
|
||||||
s.WriteString(fmt.Sprintf(`<a class="prev page-numbers" href="/%s%s">上一页</a>`, pp, query))
|
s.WriteString(fmt.Sprintf(`<a class="prev page-numbers" href="%s%s">上一页</a>`, pp, query))
|
||||||
}
|
}
|
||||||
if currentPage >= step+2 {
|
if currentPage >= step+2 {
|
||||||
d := ""
|
d := ""
|
||||||
if currentPage > step+2 {
|
if currentPage > step+2 {
|
||||||
d = `<span class="page-numbers dots">…</span>`
|
d = `<span class="page-numbers dots">…</span>`
|
||||||
}
|
}
|
||||||
|
e := replacePage(r, path, 1)
|
||||||
s.WriteString(fmt.Sprintf(`
|
s.WriteString(fmt.Sprintf(`
|
||||||
<a class="page-numbers" href="/%s"><span class="meta-nav screen-reader-text">页 </span>1</a>
|
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text">页 </span>1</a>
|
||||||
%s
|
%s
|
||||||
`, query, d))
|
`, e, query, d))
|
||||||
}
|
}
|
||||||
if totalPage < end {
|
if totalPage < end {
|
||||||
end = totalPage
|
end = totalPage
|
||||||
|
@ -173,10 +200,7 @@ func pagination(currentPage, totalPage, step int, query string) (html string) {
|
||||||
`, page)
|
`, page)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
d := fmt.Sprintf("/page/%d", page)
|
d := replacePage(r, pathx, page)
|
||||||
if currentPage > page && page == 1 {
|
|
||||||
d = "/"
|
|
||||||
}
|
|
||||||
h = fmt.Sprintf(`
|
h = fmt.Sprintf(`
|
||||||
<a class="page-numbers" href="%s%s">
|
<a class="page-numbers" href="%s%s">
|
||||||
<span class="meta-nav screen-reader-text">页 </span>%d</a>
|
<span class="meta-nav screen-reader-text">页 </span>%d</a>
|
||||||
|
@ -185,14 +209,32 @@ func pagination(currentPage, totalPage, step int, query string) (html string) {
|
||||||
s.WriteString(h)
|
s.WriteString(h)
|
||||||
|
|
||||||
}
|
}
|
||||||
if totalPage > currentPage+step+2 {
|
if totalPage >= currentPage+step+1 {
|
||||||
|
if totalPage > currentPage+step+1 {
|
||||||
|
s.WriteString(`<span class="page-numbers dots">…</span>`)
|
||||||
|
}
|
||||||
|
dd := replacePage(r, pathx, totalPage)
|
||||||
s.WriteString(fmt.Sprintf(`
|
s.WriteString(fmt.Sprintf(`
|
||||||
<span class="page-numbers dots">…</span>
|
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text">页 </span>%d</a>`, dd, query, totalPage))
|
||||||
<a class="page-numbers" href="/page/%d%s"><span class="meta-nav screen-reader-text">页 </span>%d</a>`, totalPage, query, totalPage))
|
|
||||||
}
|
}
|
||||||
if currentPage < totalPage {
|
if currentPage < totalPage {
|
||||||
s.WriteString(fmt.Sprintf(`<a class="next page-numbers" href="/page/%d%s">下一页</a>`, currentPage+1, query))
|
dd := replacePage(r, pathx, currentPage+1)
|
||||||
|
s.WriteString(fmt.Sprintf(`<a class="next page-numbers" href="%s%s">下一页</a>`, dd, query))
|
||||||
}
|
}
|
||||||
html = s.String()
|
html = s.String()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func replacePage(r *regexp.Regexp, path string, page int) (src string) {
|
||||||
|
if page == 1 {
|
||||||
|
src = r.ReplaceAllString(path, "")
|
||||||
|
} else {
|
||||||
|
s := fmt.Sprintf("$1/%d", page)
|
||||||
|
src = r.ReplaceAllString(path, s)
|
||||||
|
}
|
||||||
|
src = strings.Replace(src, "//", "/", -1)
|
||||||
|
if src == "" {
|
||||||
|
src = "/"
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -36,6 +36,9 @@ func SetupRouter() *gin.Engine {
|
||||||
loadTemplates(r, "**/*")
|
loadTemplates(r, "**/*")
|
||||||
r.GET("/", index)
|
r.GET("/", index)
|
||||||
r.GET("/page/:page", index)
|
r.GET("/page/:page", index)
|
||||||
|
r.GET("/p/category/:category", index)
|
||||||
|
r.GET("/p/date/:year/:month", index)
|
||||||
|
r.GET("/p/date/:year/:month/page/:page", index)
|
||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
{{define "layout/page"}}
|
{{define "layout/page"}}
|
||||||
|
{{if .pagination}}
|
||||||
<nav class="navigation pagination" aria-label="文章">
|
<nav class="navigation pagination" aria-label="文章">
|
||||||
<h2 class="screen-reader-text">文章导航</h2>
|
<h2 class="screen-reader-text">文章导航</h2>
|
||||||
<div class="nav-links">
|
<div class="nav-links">
|
||||||
|
@ -6,3 +7,4 @@
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
{{end}}
|
|
@ -41,7 +41,7 @@
|
||||||
<nav aria-label="分类">
|
<nav aria-label="分类">
|
||||||
<ul>
|
<ul>
|
||||||
{{range $k,$v := .categories}}
|
{{range $k,$v := .categories}}
|
||||||
<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-{{$v.WpTerms.TermId}}"><a href="/p/category/{{$v.Name}}">{{$v.Name}}</a>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user