代码目录优化
This commit is contained in:
parent
f21dae1058
commit
dce85a16be
55
actions/common/common.go
Normal file
55
actions/common/common.go
Normal 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
7
actions/detail.go
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package actions
|
||||||
|
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
|
func Detail(c *gin.Context) {
|
||||||
|
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package route
|
package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github/fthvgb1/wp-go/actions/common"
|
||||||
"github/fthvgb1/wp-go/helper"
|
"github/fthvgb1/wp-go/helper"
|
||||||
"github/fthvgb1/wp-go/models"
|
"github/fthvgb1/wp-go/models"
|
||||||
"math"
|
"math"
|
||||||
|
@ -204,7 +205,7 @@ func (h *IndexHandle) queryAndSetPostCache(postIds []models.WpPosts) (err error)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func index(c *gin.Context) {
|
func Index(c *gin.Context) {
|
||||||
h := NewIndexHandle(c)
|
h := NewIndexHandle(c)
|
||||||
h.parseParams()
|
h.parseParams()
|
||||||
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](h.where, "ID", "", h.page, h.pageSize, h.orderBy, h.join, h.postType, h.status)
|
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 = "未找到页面"
|
h.titleL = "未找到页面"
|
||||||
}
|
}
|
||||||
err = h.queryAndSetPostCache(postIds)
|
err = h.queryAndSetPostCache(postIds)
|
||||||
|
pw := h.session.Get("post_password")
|
||||||
for i, v := range postIds {
|
for i, v := range postIds {
|
||||||
post, _ := PostsCache.Load(v.Id)
|
post, _ := PostsCache.Load(v.Id)
|
||||||
pp := post.(*models.WpPosts)
|
pp := post.(*models.WpPosts)
|
||||||
px := *pp
|
px := *pp
|
||||||
h.formatTitleAndContent(&px)
|
if px.PostPassword != "" && pw != px.PostPassword {
|
||||||
|
common.PasswdProject(&px)
|
||||||
|
}
|
||||||
postIds[i] = px
|
postIds[i] = px
|
||||||
}
|
}
|
||||||
recent, err := h.recentPosts()
|
recent, err := common.RecentPosts()
|
||||||
archive, err := archives()
|
for i, post := range recent {
|
||||||
categoryItems, err := categories()
|
if post.PostPassword != "" && pw != post.PostPassword {
|
||||||
|
common.PasswdProject(&recent[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
archive, err := common.Archives()
|
||||||
|
categoryItems, err := common.Categories()
|
||||||
q := c.Request.URL.Query().Encode()
|
q := c.Request.URL.Query().Encode()
|
||||||
c.HTML(http.StatusOK, "index.html", gin.H{
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
||||||
"posts": postIds,
|
"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) {
|
func pagination(currentPage, totalPage, step int, path, query string) (html string) {
|
||||||
if totalPage < 2 {
|
if totalPage < 2 {
|
||||||
return
|
return
|
|
@ -1,4 +1,4 @@
|
||||||
package route
|
package actions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
|
@ -6,7 +6,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func login(c *gin.Context) {
|
func Login(c *gin.Context) {
|
||||||
password := c.PostForm("post_password")
|
password := c.PostForm("post_password")
|
||||||
ref := c.Request.Referer()
|
ref := c.Request.Referer()
|
||||||
if ref == "" {
|
if ref == "" {
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/gin-contrib/sessions"
|
"github.com/gin-contrib/sessions"
|
||||||
"github.com/gin-contrib/sessions/cookie"
|
"github.com/gin-contrib/sessions/cookie"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
|
"github/fthvgb1/wp-go/actions"
|
||||||
"github/fthvgb1/wp-go/middleware"
|
"github/fthvgb1/wp-go/middleware"
|
||||||
"github/fthvgb1/wp-go/static"
|
"github/fthvgb1/wp-go/static"
|
||||||
"github/fthvgb1/wp-go/templates"
|
"github/fthvgb1/wp-go/templates"
|
||||||
|
@ -39,13 +40,16 @@ func SetupRouter() *gin.Engine {
|
||||||
}))
|
}))
|
||||||
store := cookie.NewStore([]byte("secret"))
|
store := cookie.NewStore([]byte("secret"))
|
||||||
r.Use(sessions.Sessions("go-wp", store))
|
r.Use(sessions.Sessions("go-wp", store))
|
||||||
r.GET("/", index)
|
r.GET("/", actions.Index)
|
||||||
r.GET("/page/:page", index)
|
r.GET("/page/:page", actions.Index)
|
||||||
r.GET("/p/category/:category", index)
|
r.GET("/p/category/:category", actions.Index)
|
||||||
r.GET("/p/tag/:tag", index)
|
r.GET("/p/category/:category/page/:page", actions.Index)
|
||||||
r.GET("/p/date/:year/:month", index)
|
r.GET("/p/tag/:tag", actions.Index)
|
||||||
r.GET("/p/date/:year/:month/page/:page", index)
|
r.GET("/p/tag/:tag/page/:page", actions.Index)
|
||||||
r.POST("/login", login)
|
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
|
return r
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,5 +2,5 @@ package templates
|
||||||
|
|
||||||
import "embed"
|
import "embed"
|
||||||
|
|
||||||
//go:embed index layout
|
//go:embed posts layout
|
||||||
var TemplateFs embed.FS
|
var TemplateFs embed.FS
|
||||||
|
|
Loading…
Reference in New Issue
Block a user