From dce85a16be3d752fdea0bd1ca4dcf4488c285a90 Mon Sep 17 00:00:00 2001 From: xing Date: Sun, 18 Sep 2022 12:34:48 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=9B=AE=E5=BD=95=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/common.go | 55 +++++++++++++++++++ actions/detail.go | 7 +++ route/actions.go => actions/index.go | 78 ++++++--------------------- {route => actions}/login.go | 4 +- route/route.go | 18 ++++--- templates/{index => posts}/index.html | 0 templates/templatefs.go | 2 +- 7 files changed, 91 insertions(+), 73 deletions(-) create mode 100644 actions/common/common.go create mode 100644 actions/detail.go rename route/actions.go => actions/index.go (80%) rename {route => actions}/login.go (90%) rename templates/{index => posts}/index.html (100%) diff --git a/actions/common/common.go b/actions/common/common.go new file mode 100644 index 0000000..cdaac6d --- /dev/null +++ b/actions/common/common.go @@ -0,0 +1,55 @@ +package common + +import ( + "fmt" + "github/fthvgb1/wp-go/models" +) + +func Archives() (r []models.PostArchive, err error) { + r, err = models.Find[models.PostArchive](models.SqlBuilder{ + {"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) + 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 RecentPosts() (r []models.WpPosts, err error) { + r, err = models.Find[models.WpPosts](models.SqlBuilder{{ + "post_type", "post", + }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) + return +} + +func PasswdProject(post *models.WpPosts) { + if post.PostTitle != "" { + post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) + } + if post.PostContent != "" { + format := ` +
+

此内容受密码保护。如需查阅,请在下列字段中输入您的密码。

+

+
` + post.PostContent = fmt.Sprintf(format, post.Id, post.Id) + } +} diff --git a/actions/detail.go b/actions/detail.go new file mode 100644 index 0000000..79274e2 --- /dev/null +++ b/actions/detail.go @@ -0,0 +1,7 @@ +package actions + +import "github.com/gin-gonic/gin" + +func Detail(c *gin.Context) { + +} diff --git a/route/actions.go b/actions/index.go similarity index 80% rename from route/actions.go rename to actions/index.go index 0f6b29f..eae2daa 100644 --- a/route/actions.go +++ b/actions/index.go @@ -1,9 +1,10 @@ -package route +package actions import ( "fmt" "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + "github/fthvgb1/wp-go/actions/common" "github/fthvgb1/wp-go/helper" "github/fthvgb1/wp-go/models" "math" @@ -204,7 +205,7 @@ func (h *IndexHandle) queryAndSetPostCache(postIds []models.WpPosts) (err error) return } -func index(c *gin.Context) { +func Index(c *gin.Context) { h := NewIndexHandle(c) h.parseParams() postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status) @@ -220,17 +221,24 @@ func index(c *gin.Context) { h.titleL = "未找到页面" } err = h.queryAndSetPostCache(postIds) - + pw := h.session.Get("post_password") for i, v := range postIds { post, _ := PostsCache.Load(v.Id) pp := post.(*models.WpPosts) px := *pp - h.formatTitleAndContent(&px) + if px.PostPassword != "" && pw != px.PostPassword { + common.PasswdProject(&px) + } postIds[i] = px } - recent, err := h.recentPosts() - archive, err := archives() - categoryItems, err := categories() + recent, err := common.RecentPosts() + for i, post := range recent { + if post.PostPassword != "" && pw != post.PostPassword { + common.PasswdProject(&recent[i]) + } + } + archive, err := common.Archives() + categoryItems, err := common.Categories() q := c.Request.URL.Query().Encode() c.HTML(http.StatusOK, "index.html", gin.H{ "posts": postIds, @@ -246,62 +254,6 @@ func index(c *gin.Context) { }) } -func (h *IndexHandle) formatTitleAndContent(post *models.WpPosts) { - pw := h.session.Get("post_password") - if post.PostPassword != "" && post.PostPassword != pw { - if post.PostTitle != "" { - post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) - } - if post.PostContent != "" { - format := ` -
-

此内容受密码保护。如需查阅,请在下列字段中输入您的密码。

-

-
` - post.PostContent = fmt.Sprintf(format, post.Id, post.Id) - } - } -} - -func (h *IndexHandle) recentPosts() (r []models.WpPosts, err error) { - r, err = models.Find[models.WpPosts](models.SqlBuilder{{ - "post_type", "post", - }, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5) - for i := 0; i < len(r); i++ { - h.formatTitleAndContent(&r[i]) - } - 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) { - r, err = models.Find[models.PostArchive](models.SqlBuilder{ - {"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) - return -} - func pagination(currentPage, totalPage, step int, path, query string) (html string) { if totalPage < 2 { return diff --git a/route/login.go b/actions/login.go similarity index 90% rename from route/login.go rename to actions/login.go index 3544d60..5d192af 100644 --- a/route/login.go +++ b/actions/login.go @@ -1,4 +1,4 @@ -package route +package actions import ( "github.com/gin-contrib/sessions" @@ -6,7 +6,7 @@ import ( "strings" ) -func login(c *gin.Context) { +func Login(c *gin.Context) { password := c.PostForm("post_password") ref := c.Request.Referer() if ref == "" { diff --git a/route/route.go b/route/route.go index 0bf6b2e..f2634f5 100644 --- a/route/route.go +++ b/route/route.go @@ -4,6 +4,7 @@ import ( "github.com/gin-contrib/sessions" "github.com/gin-contrib/sessions/cookie" "github.com/gin-gonic/gin" + "github/fthvgb1/wp-go/actions" "github/fthvgb1/wp-go/middleware" "github/fthvgb1/wp-go/static" "github/fthvgb1/wp-go/templates" @@ -39,13 +40,16 @@ func SetupRouter() *gin.Engine { })) store := cookie.NewStore([]byte("secret")) r.Use(sessions.Sessions("go-wp", store)) - r.GET("/", index) - r.GET("/page/:page", index) - r.GET("/p/category/:category", index) - r.GET("/p/tag/:tag", index) - r.GET("/p/date/:year/:month", index) - r.GET("/p/date/:year/:month/page/:page", index) - r.POST("/login", login) + r.GET("/", actions.Index) + r.GET("/page/:page", actions.Index) + r.GET("/p/category/:category", actions.Index) + r.GET("/p/category/:category/page/:page", actions.Index) + r.GET("/p/tag/:tag", actions.Index) + r.GET("/p/tag/:tag/page/:page", actions.Index) + r.GET("/p/date/:year/:month", actions.Index) + r.GET("/p/date/:year/:month/page/:page", actions.Index) + r.POST("/login", actions.Login) + r.GET("/p/:id", actions.Detail) return r } diff --git a/templates/index/index.html b/templates/posts/index.html similarity index 100% rename from templates/index/index.html rename to templates/posts/index.html diff --git a/templates/templatefs.go b/templates/templatefs.go index f74fbcf..c4c6c92 100644 --- a/templates/templatefs.go +++ b/templates/templatefs.go @@ -2,5 +2,5 @@ package templates import "embed" -//go:embed index layout +//go:embed posts layout var TemplateFs embed.FS