From 46088d1ed6b4ba534f29360832cace38fbd185f6 Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 17 Sep 2022 09:42:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=90=9C=E7=B4=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- route/actions.go | 19 ++++++++++++++++++- templates/index/index.html | 5 +++++ templates/layout/sidebar.html | 4 ++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/route/actions.go b/route/actions.go index 240963a..aec1787 100644 --- a/route/actions.go +++ b/route/actions.go @@ -18,10 +18,12 @@ var PostsCache sync.Map func index(c *gin.Context) { page := 1 pageSize := 10 + status := []interface{}{"publish"} order := c.Query("order") if !helper.IsContainInArr(order, []string{"asc", "desc"}) { order = "asc" } + header := "" where := models.SqlBuilder{{ "post_type", "post", }, {"post_status", "in", ""}} @@ -37,6 +39,7 @@ func index(c *gin.Context) { where = append(where, []string{ "month(post_date)", month, }) + header = fmt.Sprintf("月度归档: %s年%s月", year, month) } tt := "" category := c.Param("category") @@ -44,9 +47,22 @@ func index(c *gin.Context) { category = c.Param("tag") if category != "" { tt = "post_tag" + header = fmt.Sprintf("标签: %s", category) } } else { tt = "category" + header = fmt.Sprintf("分类: %s", category) + } + s := c.Query("s") + if s != "" && strings.Replace(s, " ", "", -1) != "" { + q := helper.StrJoin("%", s, "%") + where = append(where, []string{ + "and", "post_title", "like", q, "", + "or", "post_content", "like", q, "", + }) + header = fmt.Sprintf("%s的搜索结果", s) + } else { + status = append(status, "private") } var join models.SqlBuilder if category != "" { @@ -70,7 +86,6 @@ func index(c *gin.Context) { } } - status := []interface{}{"publish", "private"} postIds, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, pageSize, models.SqlBuilder{{"post_date", order}}, join, status) defer func() { if err != nil { @@ -156,6 +171,8 @@ func index(c *gin.Context) { "totalPage": totalPage, "queryRaw": q, "pagination": pagination(page, totalPage, 1, c.Request.URL.Path, q), + "search": s, + "header": header, }) } diff --git a/templates/index/index.html b/templates/index/index.html index 6c333ba..69ce685 100644 --- a/templates/index/index.html +++ b/templates/index/index.html @@ -2,6 +2,11 @@ {{define "content" }}
+ {{if .header}} + + {{end}} {{ range $k,$v:=.posts}}
diff --git a/templates/layout/sidebar.html b/templates/layout/sidebar.html index fec0030..b0e15be 100644 --- a/templates/layout/sidebar.html +++ b/templates/layout/sidebar.html @@ -1,10 +1,10 @@ {{define "layout/sidebar" }}