2022-09-01 02:31:11 +00:00
|
|
|
package route
|
|
|
|
|
|
|
|
import (
|
2022-09-15 14:35:32 +00:00
|
|
|
"fmt"
|
2022-09-01 02:31:11 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2022-09-15 14:44:45 +00:00
|
|
|
"github/fthvgb1/wp-go/helper"
|
2022-09-14 05:28:31 +00:00
|
|
|
"github/fthvgb1/wp-go/models"
|
2022-09-15 14:35:32 +00:00
|
|
|
"math"
|
2022-09-01 02:31:11 +00:00
|
|
|
"net/http"
|
2022-09-16 06:46:51 +00:00
|
|
|
"regexp"
|
2022-09-14 05:28:31 +00:00
|
|
|
"strconv"
|
2022-09-15 14:35:32 +00:00
|
|
|
"strings"
|
2022-09-14 05:28:31 +00:00
|
|
|
"sync"
|
2022-09-01 02:31:11 +00:00
|
|
|
)
|
|
|
|
|
2022-09-14 05:28:31 +00:00
|
|
|
var PostsCache sync.Map
|
|
|
|
|
2022-09-01 02:31:11 +00:00
|
|
|
func index(c *gin.Context) {
|
2022-09-14 05:28:31 +00:00
|
|
|
page := 1
|
|
|
|
pageSize := 10
|
2022-09-15 14:44:45 +00:00
|
|
|
order := c.Query("order")
|
|
|
|
if !helper.IsContainInArr(order, []string{"asc", "desc"}) {
|
|
|
|
order = "desc"
|
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
where := models.SqlBuilder{{
|
|
|
|
"post_type", "post",
|
|
|
|
}, {"post_status", "in", ""}}
|
2022-09-14 05:28:31 +00:00
|
|
|
p := c.Query("paged")
|
2022-09-16 06:46:51 +00:00
|
|
|
year := c.Param("year")
|
|
|
|
if year != "" {
|
|
|
|
where = append(where, []string{
|
|
|
|
"year(post_date)", year,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
month := c.Param("month")
|
|
|
|
if month != "" {
|
|
|
|
where = append(where, []string{
|
|
|
|
"month(post_date)", month,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
category := c.Param("category")
|
|
|
|
if category != "" {
|
|
|
|
/*where = append(where, []string{
|
|
|
|
"d.name", category,
|
|
|
|
})*/
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
if p == "" {
|
|
|
|
p = c.Param("page")
|
2022-09-14 05:28:31 +00:00
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
if p != "" {
|
|
|
|
if pa, err := strconv.Atoi(p); err == nil {
|
|
|
|
page = pa
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-14 05:28:31 +00:00
|
|
|
status := []interface{}{"publish", "private"}
|
2022-09-16 06:46:51 +00:00
|
|
|
posts, totalRaw, err := models.SimplePagination[models.WpPosts](where, "ID", page, pageSize, models.SqlBuilder{{"post_date", order}}, nil, status)
|
2022-09-15 14:35:32 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
c.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2022-09-14 05:28:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var all []uint64
|
|
|
|
var allPosts []models.WpPosts
|
|
|
|
var needQuery []interface{}
|
|
|
|
for _, wpPosts := range posts {
|
|
|
|
all = append(all, wpPosts.Id)
|
|
|
|
if _, ok := PostsCache.Load(wpPosts.Id); !ok {
|
|
|
|
needQuery = append(needQuery, wpPosts.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(needQuery) > 0 {
|
|
|
|
rawPosts, err := models.Find[models.WpPosts](models.SqlBuilder{{
|
|
|
|
"Id", "in", "",
|
2022-09-14 13:30:59 +00:00
|
|
|
}}, "a.*,d.name category_name", "", nil, models.SqlBuilder{{
|
2022-09-14 05:28:31 +00:00
|
|
|
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
|
|
|
|
}, {
|
|
|
|
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
|
|
|
|
}, {
|
|
|
|
"left join", "wp_terms d", "c.term_id=d.term_id",
|
|
|
|
}}, 0, needQuery)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, post := range rawPosts {
|
|
|
|
PostsCache.Store(post.Id, post)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, id := range all {
|
|
|
|
post, _ := PostsCache.Load(id)
|
|
|
|
pp := post.(models.WpPosts)
|
|
|
|
allPosts = append(allPosts, pp)
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
recent, err := recentPosts()
|
|
|
|
archive, err := archives()
|
|
|
|
categoryItems, err := categories()
|
|
|
|
totalPage := int(math.Ceil(float64(totalRaw) / float64(pageSize)))
|
|
|
|
q := c.Request.URL.Query().Encode()
|
|
|
|
if q != "" {
|
|
|
|
q = fmt.Sprintf("?%s", q)
|
|
|
|
}
|
2022-09-01 02:31:11 +00:00
|
|
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
2022-09-14 13:30:59 +00:00
|
|
|
"posts": allPosts,
|
|
|
|
"options": models.Options,
|
|
|
|
"recentPosts": recent,
|
|
|
|
"archives": archive,
|
2022-09-15 14:35:32 +00:00
|
|
|
"categories": categoryItems,
|
|
|
|
"totalPage": totalPage,
|
|
|
|
"queryRaw": q,
|
2022-09-16 06:46:51 +00:00
|
|
|
"pagination": pagination(page, totalPage, 2, c.Request.URL.Path, q),
|
2022-09-01 02:31:11 +00:00
|
|
|
})
|
|
|
|
}
|
2022-09-14 13:30:59 +00:00
|
|
|
|
|
|
|
func recentPosts() (r []models.WpPosts, err error) {
|
|
|
|
r, err = models.Find[models.WpPosts](models.SqlBuilder{{
|
|
|
|
"post_type", "post",
|
|
|
|
}, {"post_status", "publish"}}, "ID,post_title", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-15 14:35:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-14 13:30:59 +00:00
|
|
|
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
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
|
2022-09-16 06:46:51 +00:00
|
|
|
func pagination(currentPage, totalPage, step int, path, query string) (html string) {
|
|
|
|
if totalPage < 2 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pathx := path
|
|
|
|
if !strings.Contains(path, "/page/") {
|
|
|
|
pathx = fmt.Sprintf("%s%s", path, "/page/1")
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
s := strings.Builder{}
|
|
|
|
if currentPage > totalPage {
|
|
|
|
currentPage = totalPage
|
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
r := regexp.MustCompile(`(/page)/(\d+)`)
|
2022-09-15 14:35:32 +00:00
|
|
|
start := currentPage - step
|
|
|
|
end := currentPage + step
|
|
|
|
if start < 1 {
|
|
|
|
start = currentPage
|
|
|
|
}
|
|
|
|
if currentPage > 1 {
|
|
|
|
pp := ""
|
2022-09-16 06:46:51 +00:00
|
|
|
if currentPage >= 2 {
|
|
|
|
pp = replacePage(r, pathx, currentPage-1)
|
2022-09-15 14:35:32 +00:00
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
s.WriteString(fmt.Sprintf(`<a class="prev page-numbers" href="%s%s">上一页</a>`, pp, query))
|
2022-09-15 14:35:32 +00:00
|
|
|
}
|
|
|
|
if currentPage >= step+2 {
|
|
|
|
d := ""
|
|
|
|
if currentPage > step+2 {
|
|
|
|
d = `<span class="page-numbers dots">…</span>`
|
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
e := replacePage(r, path, 1)
|
2022-09-15 14:35:32 +00:00
|
|
|
s.WriteString(fmt.Sprintf(`
|
2022-09-16 06:46:51 +00:00
|
|
|
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text">页 </span>1</a>
|
2022-09-15 14:35:32 +00:00
|
|
|
%s
|
2022-09-16 06:46:51 +00:00
|
|
|
`, e, query, d))
|
2022-09-15 14:35:32 +00:00
|
|
|
}
|
|
|
|
if totalPage < end {
|
|
|
|
end = totalPage
|
|
|
|
}
|
|
|
|
|
|
|
|
for page := start; page <= end; page++ {
|
|
|
|
h := ""
|
|
|
|
if currentPage == page {
|
|
|
|
h = fmt.Sprintf(`
|
|
|
|
<span aria-current="page" class="page-numbers current">
|
|
|
|
<span class="meta-nav screen-reader-text">页 </span>%d</span>
|
|
|
|
`, page)
|
|
|
|
|
|
|
|
} else {
|
2022-09-16 06:46:51 +00:00
|
|
|
d := replacePage(r, pathx, page)
|
2022-09-15 14:35:32 +00:00
|
|
|
h = fmt.Sprintf(`
|
|
|
|
<a class="page-numbers" href="%s%s">
|
|
|
|
<span class="meta-nav screen-reader-text">页 </span>%d</a>
|
|
|
|
`, d, query, page)
|
|
|
|
}
|
|
|
|
s.WriteString(h)
|
|
|
|
|
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
if totalPage >= currentPage+step+1 {
|
|
|
|
if totalPage > currentPage+step+1 {
|
|
|
|
s.WriteString(`<span class="page-numbers dots">…</span>`)
|
|
|
|
}
|
|
|
|
dd := replacePage(r, pathx, totalPage)
|
2022-09-15 14:35:32 +00:00
|
|
|
s.WriteString(fmt.Sprintf(`
|
2022-09-16 06:46:51 +00:00
|
|
|
<a class="page-numbers" href="%s%s"><span class="meta-nav screen-reader-text">页 </span>%d</a>`, dd, query, totalPage))
|
2022-09-15 14:35:32 +00:00
|
|
|
}
|
|
|
|
if currentPage < totalPage {
|
2022-09-16 06:46:51 +00:00
|
|
|
dd := replacePage(r, pathx, currentPage+1)
|
|
|
|
s.WriteString(fmt.Sprintf(`<a class="next page-numbers" href="%s%s">下一页</a>`, dd, query))
|
2022-09-15 14:35:32 +00:00
|
|
|
}
|
|
|
|
html = s.String()
|
|
|
|
return
|
|
|
|
}
|
2022-09-16 06:46:51 +00:00
|
|
|
|
|
|
|
func replacePage(r *regexp.Regexp, path string, page int) (src string) {
|
|
|
|
if page == 1 {
|
|
|
|
src = r.ReplaceAllString(path, "")
|
|
|
|
} else {
|
|
|
|
s := fmt.Sprintf("$1/%d", page)
|
|
|
|
src = r.ReplaceAllString(path, s)
|
|
|
|
}
|
|
|
|
src = strings.Replace(src, "//", "/", -1)
|
|
|
|
if src == "" {
|
|
|
|
src = "/"
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|