代码目录优化

This commit is contained in:
xing 2022-09-18 12:34:48 +08:00
parent f21dae1058
commit dce85a16be
7 changed files with 91 additions and 73 deletions

55
actions/common/common.go Normal file
View File

@ -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 := `
<form action="/login" class="post-password-form" method="post">
<p>此内容受密码保护如需查阅请在下列字段中输入您的密码</p>
<p><label for="pwbox-%d">密码 <input name="post_password" id="pwbox-%d" type="password" size="20"></label> <input type="submit" name="Submit" value="提交"></p>
</form>`
post.PostContent = fmt.Sprintf(format, post.Id, post.Id)
}
}

7
actions/detail.go Normal file
View File

@ -0,0 +1,7 @@
package actions
import "github.com/gin-gonic/gin"
func Detail(c *gin.Context) {
}

View File

@ -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 := `
<form action="/login" class="post-password-form" method="post">
<p>此内容受密码保护如需查阅请在下列字段中输入您的密码</p>
<p><label for="pwbox-%d">密码 <input name="post_password" id="pwbox-%d" type="password" size="20"></label> <input type="submit" name="Submit" value="提交"></p>
</form>`
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

View File

@ -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 == "" {

View File

@ -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
}

View File

@ -2,5 +2,5 @@ package templates
import "embed"
//go:embed index layout
//go:embed posts layout
var TemplateFs embed.FS