代码优化

This commit is contained in:
xing 2022-09-17 17:05:43 +08:00
parent 23deffffd1
commit fbf5b64ddf

View File

@ -15,103 +15,135 @@ import (
var PostsCache sync.Map var PostsCache sync.Map
func index(c *gin.Context) { type IndexHandle struct {
page := 1 c *gin.Context
pageSize := 10 page int
status := []interface{}{"publish"} pageSize int
order := c.Query("order") title string
if !helper.IsContainInArr(order, []string{"asc", "desc"}) { titleL string
order = "asc" titleR string
search string
totalPage int
category string
categoryType string
where models.SqlBuilder
orderBy models.SqlBuilder
order string
join models.SqlBuilder
postType []interface{}
status []interface{}
header string
} }
title := ""
header := "" func NewIndexHandle(ctx *gin.Context) *IndexHandle {
postType := []interface{}{"post"} return &IndexHandle{
where := models.SqlBuilder{{ c: ctx,
"post_type", "in", "", page: 1,
}, {"post_status", "in", ""}} pageSize: 10,
p := c.Query("paged") titleL: models.Options["blogname"],
year := c.Param("year") titleR: models.Options["blogdescription"],
where: models.SqlBuilder{
{"post_type", "in", ""},
{"post_status", "in", ""},
},
orderBy: models.SqlBuilder{},
join: models.SqlBuilder{},
postType: []interface{}{"post"},
status: []interface{}{"publish"},
}
}
func (h *IndexHandle) setTitleLR(l, r string) {
h.titleL = l
h.titleR = r
}
func (h *IndexHandle) getTitle() string {
h.title = fmt.Sprintf("%s-%s", h.titleL, h.titleR)
return h.title
}
func (h *IndexHandle) parseParams() {
h.order = h.c.Query("order")
if !helper.IsContainInArr(h.order, []string{"asc", "desc"}) {
h.order = "asc"
}
year := h.c.Param("year")
if year != "" { if year != "" {
where = append(where, []string{ h.where = append(h.where, []string{
"year(post_date)", year, "year(post_date)", year,
}) })
} }
month := c.Param("month") month := h.c.Param("month")
if month != "" { if month != "" {
where = append(where, []string{ h.where = append(h.where, []string{
"month(post_date)", month, "month(post_date)", month,
}) })
ss := fmt.Sprintf("%s年%s月", year, month) ss := fmt.Sprintf("%s年%s月", year, month)
header = fmt.Sprintf("月度归档: <span>%s</span>", ss) h.header = fmt.Sprintf("月度归档: <span>%s</span>", ss)
title = ss h.setTitleLR(ss, models.Options["blogname"])
} }
tt := "" category := h.c.Param("category")
category := c.Param("category")
if category == "" { if category == "" {
category = c.Param("tag") category = h.c.Param("tag")
if category != "" { if category != "" {
tt = "post_tag" h.categoryType = "post_tag"
header = fmt.Sprintf("标签: <span>%s</span>", category) h.header = fmt.Sprintf("标签: <span>%s</span>", category)
title = category
} }
} else { } else {
tt = "category" h.categoryType = "category"
header = fmt.Sprintf("分类: <span>%s</span>", category) h.header = fmt.Sprintf("分类: <span>%s</span>", category)
title = category
} }
s := c.Query("s") h.category = category
if s != "" && strings.Replace(s, " ", "", -1) != "" {
q := helper.StrJoin("%", s, "%")
where = append(where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
postType = append(postType, "page", "attachment")
header = fmt.Sprintf("%s的搜索结果", s)
title = header
} else {
status = append(status, "private")
}
var join models.SqlBuilder
if category != "" { if category != "" {
where = append(where, []string{ h.where = append(h.where, []string{
"d.name", category, "d.name", category,
}, []string{"taxonomy", tt}) }, []string{"taxonomy", h.categoryType})
join = append(join, []string{ h.join = append(h.join, []string{
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id", "a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
}, []string{ }, []string{
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id", "left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
}, []string{ }, []string{
"left join", "wp_terms d", "c.term_id=d.term_id", "left join", "wp_terms d", "c.term_id=d.term_id",
}) })
h.setTitleLR(category, models.Options["blogname"])
} }
s := h.c.Query("s")
if s != "" && strings.Replace(s, " ", "", -1) != "" {
q := helper.StrJoin("%", s, "%")
h.where = append(h.where, []string{
"and", "post_title", "like", q, "",
"or", "post_content", "like", q, "",
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
h.postType = append(h.postType, "page", "attachment")
h.header = fmt.Sprintf("%s的搜索结果", s)
h.setTitleLR(helper.StrJoin(`"`, s, `"`, "的搜索结果"), models.Options["blogname"])
h.search = s
} else {
h.status = append(h.status, "private")
}
p := h.c.Query("paged")
if p == "" { if p == "" {
p = c.Param("page") p = h.c.Param("page")
} }
if p != "" { if p != "" {
if pa, err := strconv.Atoi(p); err == nil { if pa, err := strconv.Atoi(p); err == nil {
page = pa h.page = pa
} }
} }
if page == 1 { if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
title = helper.StrJoin(models.Options["blogname"], "-", models.Options["blogdescription"]) h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), models.Options["blogname"])
}
} }
postIds, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", "", page, pageSize, models.SqlBuilder{{"post_date", order}}, join, postType, status) func (h *IndexHandle) getTotalPage(totalRaws int) int {
defer func() { h.totalPage = int(math.Ceil(float64(totalRaws) / float64(h.pageSize)))
if err != nil { return h.totalPage
c.Error(err)
}
}()
if err != nil {
return
}
if len(postIds) < 1 && category != "" {
title = "未找到页面"
} }
func (h *IndexHandle) queryAndSetPostCache(postIds []models.WpPosts) (err error) {
var all []uint64 var all []uint64
var allPosts []models.WpPosts
var needQuery []interface{} var needQuery []interface{}
for _, wpPosts := range postIds { for _, wpPosts := range postIds {
all = append(all, wpPosts.Id) all = append(all, wpPosts.Id)
@ -164,32 +196,50 @@ func index(c *gin.Context) {
PostsCache.Store(pp.Id, pp) PostsCache.Store(pp.Id, pp)
} }
} }
return
}
for _, id := range all { func index(c *gin.Context) {
post, _ := PostsCache.Load(id) 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)
defer func() {
if err != nil {
c.Error(err)
}
}()
if err != nil {
return
}
if len(postIds) < 1 && h.category != "" {
h.titleL = "未找到页面"
}
err = h.queryAndSetPostCache(postIds)
for i, v := range postIds {
post, _ := PostsCache.Load(v.Id)
pp := post.(*models.WpPosts) pp := post.(*models.WpPosts)
allPosts = append(allPosts, *pp) postIds[i] = *pp
} }
recent, err := recentPosts() recent, err := recentPosts()
archive, err := archives() archive, err := archives()
categoryItems, err := categories() categoryItems, err := categories()
totalPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
q := c.Request.URL.Query().Encode() q := c.Request.URL.Query().Encode()
if q != "" { if q != "" {
q = fmt.Sprintf("?%s", q) q = fmt.Sprintf("?%s", q)
} }
c.HTML(http.StatusOK, "index.html", gin.H{ c.HTML(http.StatusOK, "index.html", gin.H{
"posts": allPosts, "posts": postIds,
"options": models.Options, "options": models.Options,
"recentPosts": recent, "recentPosts": recent,
"archives": archive, "archives": archive,
"categories": categoryItems, "categories": categoryItems,
"totalPage": totalPage, "totalPage": h.getTotalPage(totalRaw),
"queryRaw": q, "queryRaw": q,
"pagination": pagination(page, totalPage, 1, c.Request.URL.Path, q), "pagination": pagination(h.page, h.totalPage, 1, c.Request.URL.Path, q),
"search": s, "search": h.search,
"header": header, "header": h.header,
"title": title, "title": h.getTitle(),
}) })
} }