From 8ab85040a5e897536679023163046974d7112151 Mon Sep 17 00:00:00 2001 From: xing Date: Fri, 16 Sep 2022 14:46:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=88=86=E9=A1=B5=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/model.go | 21 +++++--- route/actions.go | 82 ++++++++++++++++++++++++-------- route/route.go | 3 ++ templates/layout/pagination.html | 2 + templates/layout/sidebar.html | 2 +- 5 files changed, 83 insertions(+), 27 deletions(-) diff --git a/models/model.go b/models/model.go index 2a0a397..15e6736 100644 --- a/models/model.go +++ b/models/model.go @@ -25,29 +25,38 @@ func (w SqlBuilder) ParseWhere(in ...[]interface{}) (string, []interface{}) { c := 0 for _, ss := range w { if len(ss) == 2 { - s.WriteString("`") - if strings.Contains(ss[0], ".") { + if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") { + s.WriteString("`") sx := strings.Split(ss[0], ".") s.WriteString(sx[0]) s.WriteString("`.`") 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 { s.WriteString(ss[0]) } - s.WriteString("`=? and ") + s.WriteString("=? and ") args = append(args, ss[1]) } if len(ss) >= 3 { - s.WriteString("`") - if strings.Contains(ss[0], ".") { + if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") { + s.WriteString("`") sx := strings.Split(ss[0], ".") s.WriteString(sx[0]) s.WriteString("`.`") 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 { s.WriteString(ss[0]) } - s.WriteString("`") s.WriteString(ss[1]) if ss[1] == "in" && len(in) > 0 { s.WriteString(" (") diff --git a/route/actions.go b/route/actions.go index bdce0f6..ba18d1c 100644 --- a/route/actions.go +++ b/route/actions.go @@ -7,6 +7,7 @@ import ( "github/fthvgb1/wp-go/models" "math" "net/http" + "regexp" "strconv" "strings" "sync" @@ -21,7 +22,28 @@ func index(c *gin.Context) { if !helper.IsContainInArr(order, []string{"asc", "desc"}) { order = "desc" } + where := models.SqlBuilder{{ + "post_type", "post", + }, {"post_status", "in", ""}} 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 == "" { p = c.Param("page") } @@ -32,9 +54,7 @@ func index(c *gin.Context) { } status := []interface{}{"publish", "private"} - posts, totalRaw, err := models.SimplePagination[models.WpPosts](models.SqlBuilder{{ - "post_type", "post", - }, {"post_status", "in", ""}}, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status) + posts, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status) defer func() { if err != nil { c.Error(err) @@ -91,7 +111,7 @@ func index(c *gin.Context) { "categories": categoryItems, "totalPage": totalPage, "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 } -func pagination(currentPage, totalPage, step int, query string) (html string) { - html = "" +func pagination(currentPage, totalPage, step int, path, query string) (html string) { + if totalPage < 2 { + return + } + pathx := path + if !strings.Contains(path, "/page/") { + pathx = fmt.Sprintf("%s%s", path, "/page/1") + } s := strings.Builder{} if currentPage > totalPage { currentPage = totalPage } - + r := regexp.MustCompile(`(/page)/(\d+)`) start := currentPage - step end := currentPage + step if start < 1 { @@ -145,20 +171,21 @@ func pagination(currentPage, totalPage, step int, query string) (html string) { } if currentPage > 1 { pp := "" - if currentPage > 2 { - pp = fmt.Sprintf("page/%d", currentPage-1) + if currentPage >= 2 { + pp = replacePage(r, pathx, currentPage-1) } - s.WriteString(fmt.Sprintf(``, pp, query)) + s.WriteString(fmt.Sprintf(``, pp, query)) } if currentPage >= step+2 { d := "" if currentPage > step+2 { d = `` } + e := replacePage(r, path, 1) s.WriteString(fmt.Sprintf(` -1 +1 %s -`, query, d)) +`, e, query, d)) } if totalPage < end { end = totalPage @@ -173,10 +200,7 @@ func pagination(currentPage, totalPage, step int, query string) (html string) { `, page) } else { - d := fmt.Sprintf("/page/%d", page) - if currentPage > page && page == 1 { - d = "/" - } + d := replacePage(r, pathx, page) h = fmt.Sprintf(` %d @@ -185,14 +209,32 @@ func pagination(currentPage, totalPage, step int, query string) (html string) { s.WriteString(h) } - if totalPage > currentPage+step+2 { + if totalPage >= currentPage+step+1 { + if totalPage > currentPage+step+1 { + s.WriteString(``) + } + dd := replacePage(r, pathx, totalPage) s.WriteString(fmt.Sprintf(` - -%d`, totalPage, query, totalPage)) +%d`, dd, query, totalPage)) } if currentPage < totalPage { - s.WriteString(fmt.Sprintf(``, currentPage+1, query)) + dd := replacePage(r, pathx, currentPage+1) + s.WriteString(fmt.Sprintf(``, dd, query)) } html = s.String() 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 +} diff --git a/route/route.go b/route/route.go index 57052de..9b51d26 100644 --- a/route/route.go +++ b/route/route.go @@ -36,6 +36,9 @@ func SetupRouter() *gin.Engine { loadTemplates(r, "**/*") r.GET("/", 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 } diff --git a/templates/layout/pagination.html b/templates/layout/pagination.html index ebaf778..0d6d34a 100644 --- a/templates/layout/pagination.html +++ b/templates/layout/pagination.html @@ -1,8 +1,10 @@ {{define "layout/page"}} +{{if .pagination}} +{{end}} {{end}} \ No newline at end of file diff --git a/templates/layout/sidebar.html b/templates/layout/sidebar.html index df1fc2f..fec0030 100644 --- a/templates/layout/sidebar.html +++ b/templates/layout/sidebar.html @@ -41,7 +41,7 @@