2022-09-01 02:31:11 +00:00
|
|
|
|
package route
|
|
|
|
|
|
|
|
|
|
import (
|
2022-09-15 14:35:32 +00:00
|
|
|
|
"fmt"
|
2022-09-18 03:57:43 +00:00
|
|
|
|
"github.com/gin-contrib/sessions"
|
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-17 09:05:43 +00:00
|
|
|
|
type IndexHandle struct {
|
2022-09-17 09:11:10 +00:00
|
|
|
|
c *gin.Context
|
2022-09-18 03:57:43 +00:00
|
|
|
|
session sessions.Session
|
2022-09-17 09:11:10 +00:00
|
|
|
|
page int
|
|
|
|
|
pageSize int
|
|
|
|
|
title string
|
|
|
|
|
titleL string
|
|
|
|
|
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
|
|
|
|
|
paginationStep int
|
2022-09-17 09:05:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewIndexHandle(ctx *gin.Context) *IndexHandle {
|
|
|
|
|
return &IndexHandle{
|
2022-09-17 09:11:10 +00:00
|
|
|
|
c: ctx,
|
2022-09-18 03:57:43 +00:00
|
|
|
|
session: sessions.Default(ctx),
|
2022-09-17 09:11:10 +00:00
|
|
|
|
page: 1,
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
paginationStep: 1,
|
|
|
|
|
titleL: models.Options["blogname"],
|
|
|
|
|
titleR: models.Options["blogdescription"],
|
2022-09-17 09:05:43 +00:00
|
|
|
|
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")
|
2022-09-16 06:46:51 +00:00
|
|
|
|
if year != "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.where = append(h.where, []string{
|
2022-09-16 06:46:51 +00:00
|
|
|
|
"year(post_date)", year,
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
month := h.c.Param("month")
|
2022-09-16 06:46:51 +00:00
|
|
|
|
if month != "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.where = append(h.where, []string{
|
2022-09-16 06:46:51 +00:00
|
|
|
|
"month(post_date)", month,
|
|
|
|
|
})
|
2022-09-17 13:45:22 +00:00
|
|
|
|
ss := fmt.Sprintf("%s年%s月", year, strings.TrimLeft(month, "0"))
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.header = fmt.Sprintf("月度归档: <span>%s</span>", ss)
|
|
|
|
|
h.setTitleLR(ss, models.Options["blogname"])
|
2022-09-16 06:46:51 +00:00
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
category := h.c.Param("category")
|
2022-09-16 09:59:17 +00:00
|
|
|
|
if category == "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
category = h.c.Param("tag")
|
2022-09-16 09:59:17 +00:00
|
|
|
|
if category != "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.categoryType = "post_tag"
|
|
|
|
|
h.header = fmt.Sprintf("标签: <span>%s</span>", category)
|
2022-09-16 09:59:17 +00:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.categoryType = "category"
|
|
|
|
|
h.header = fmt.Sprintf("分类: <span>%s</span>", category)
|
2022-09-16 09:59:17 +00:00
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.category = category
|
|
|
|
|
|
2022-09-16 06:46:51 +00:00
|
|
|
|
if category != "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.where = append(h.where, []string{
|
2022-09-16 06:46:51 +00:00
|
|
|
|
"d.name", category,
|
2022-09-17 09:05:43 +00:00
|
|
|
|
}, []string{"taxonomy", h.categoryType})
|
|
|
|
|
h.join = append(h.join, []string{
|
2022-09-16 09:59:17 +00:00
|
|
|
|
"a", "left join", "wp_term_relationships b", "a.Id=b.object_id",
|
|
|
|
|
}, []string{
|
|
|
|
|
"left join", "wp_term_taxonomy c", "b.term_taxonomy_id=c.term_taxonomy_id",
|
|
|
|
|
}, []string{
|
|
|
|
|
"left join", "wp_terms d", "c.term_id=d.term_id",
|
|
|
|
|
})
|
2022-09-17 09:05:43 +00:00
|
|
|
|
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")
|
2022-09-16 06:46:51 +00:00
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
p := h.c.Query("paged")
|
2022-09-15 14:35:32 +00:00
|
|
|
|
if p == "" {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
p = h.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 {
|
2022-09-17 09:05:43 +00:00
|
|
|
|
h.page = pa
|
2022-09-15 14:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
|
|
|
|
|
h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), models.Options["blogname"])
|
2022-09-17 07:37:23 +00:00
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
}
|
2022-09-15 14:35:32 +00:00
|
|
|
|
|
2022-09-17 09:05:43 +00:00
|
|
|
|
func (h *IndexHandle) getTotalPage(totalRaws int) int {
|
|
|
|
|
h.totalPage = int(math.Ceil(float64(totalRaws) / float64(h.pageSize)))
|
|
|
|
|
return h.totalPage
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *IndexHandle) queryAndSetPostCache(postIds []models.WpPosts) (err error) {
|
2022-09-14 05:28:31 +00:00
|
|
|
|
var all []uint64
|
|
|
|
|
var needQuery []interface{}
|
2022-09-16 09:59:17 +00:00
|
|
|
|
for _, wpPosts := range postIds {
|
2022-09-14 05:28:31 +00:00
|
|
|
|
all = append(all, wpPosts.Id)
|
|
|
|
|
if _, ok := PostsCache.Load(wpPosts.Id); !ok {
|
|
|
|
|
needQuery = append(needQuery, wpPosts.Id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if len(needQuery) > 0 {
|
2022-09-17 03:16:05 +00:00
|
|
|
|
rawPosts, er := models.Find[models.WpPosts](models.SqlBuilder{{
|
2022-09-14 05:28:31 +00:00
|
|
|
|
"Id", "in", "",
|
2022-09-17 03:16:05 +00:00
|
|
|
|
}}, "a.*,ifnull(d.name,'') category_name,ifnull(taxonomy,'') `taxonomy`", "", 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)
|
2022-09-17 03:16:05 +00:00
|
|
|
|
if er != nil {
|
|
|
|
|
err = er
|
2022-09-14 05:28:31 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
2022-09-16 09:59:17 +00:00
|
|
|
|
postsMap := make(map[uint64]*models.WpPosts)
|
|
|
|
|
for i, post := range rawPosts {
|
|
|
|
|
v, ok := postsMap[post.Id]
|
|
|
|
|
if !ok {
|
|
|
|
|
v = &rawPosts[i]
|
|
|
|
|
}
|
|
|
|
|
if post.Taxonomy == "category" {
|
|
|
|
|
v.Categories = append(v.Categories, post.CategoryName)
|
|
|
|
|
} else if post.Taxonomy == "post_tag" {
|
|
|
|
|
v.Tags = append(v.Tags, post.CategoryName)
|
|
|
|
|
}
|
|
|
|
|
postsMap[post.Id] = v
|
|
|
|
|
}
|
|
|
|
|
for _, pp := range postsMap {
|
|
|
|
|
if len(pp.Categories) > 0 {
|
|
|
|
|
t := make([]string, 0, len(pp.Categories))
|
|
|
|
|
for _, cat := range pp.Categories {
|
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/category/%s" rel="category tag">%s</a>`, cat, cat))
|
|
|
|
|
}
|
|
|
|
|
pp.CategoriesHtml = strings.Join(t, "、")
|
|
|
|
|
}
|
|
|
|
|
if len(pp.Tags) > 0 {
|
|
|
|
|
t := make([]string, 0, len(pp.Tags))
|
|
|
|
|
for _, cat := range pp.Tags {
|
|
|
|
|
t = append(t, fmt.Sprintf(`<a href="/p/tag/%s" rel="tag">%s</a>`, cat, cat))
|
|
|
|
|
}
|
|
|
|
|
pp.TagsHtml = strings.Join(t, "、")
|
|
|
|
|
}
|
|
|
|
|
PostsCache.Store(pp.Id, pp)
|
2022-09-14 05:28:31 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-09-17 09:05:43 +00:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
defer func() {
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if len(postIds) < 1 && h.category != "" {
|
|
|
|
|
h.titleL = "未找到页面"
|
|
|
|
|
}
|
|
|
|
|
err = h.queryAndSetPostCache(postIds)
|
2022-09-14 05:28:31 +00:00
|
|
|
|
|
2022-09-17 09:05:43 +00:00
|
|
|
|
for i, v := range postIds {
|
|
|
|
|
post, _ := PostsCache.Load(v.Id)
|
2022-09-16 09:59:17 +00:00
|
|
|
|
pp := post.(*models.WpPosts)
|
2022-09-17 13:45:22 +00:00
|
|
|
|
px := *pp
|
2022-09-18 03:57:43 +00:00
|
|
|
|
h.formatTitleAndContent(&px)
|
2022-09-17 13:45:22 +00:00
|
|
|
|
postIds[i] = px
|
2022-09-14 05:28:31 +00:00
|
|
|
|
}
|
2022-09-18 03:57:43 +00:00
|
|
|
|
recent, err := h.recentPosts()
|
2022-09-15 14:35:32 +00:00
|
|
|
|
archive, err := archives()
|
|
|
|
|
categoryItems, err := categories()
|
|
|
|
|
q := c.Request.URL.Query().Encode()
|
2022-09-01 02:31:11 +00:00
|
|
|
|
c.HTML(http.StatusOK, "index.html", gin.H{
|
2022-09-17 09:05:43 +00:00
|
|
|
|
"posts": postIds,
|
2022-09-14 13:30:59 +00:00
|
|
|
|
"options": models.Options,
|
|
|
|
|
"recentPosts": recent,
|
|
|
|
|
"archives": archive,
|
2022-09-15 14:35:32 +00:00
|
|
|
|
"categories": categoryItems,
|
2022-09-17 09:05:43 +00:00
|
|
|
|
"totalPage": h.getTotalPage(totalRaw),
|
2022-09-17 09:11:10 +00:00
|
|
|
|
"pagination": pagination(h.page, h.totalPage, h.paginationStep, c.Request.URL.Path, q),
|
2022-09-17 09:05:43 +00:00
|
|
|
|
"search": h.search,
|
|
|
|
|
"header": h.header,
|
|
|
|
|
"title": h.getTitle(),
|
2022-09-01 02:31:11 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2022-09-14 13:30:59 +00:00
|
|
|
|
|
2022-09-18 03:57:43 +00:00
|
|
|
|
func (h *IndexHandle) formatTitleAndContent(post *models.WpPosts) {
|
|
|
|
|
pw := h.session.Get("post_password")
|
|
|
|
|
if post.PostPassword != "" && post.PostPassword != pw {
|
2022-09-17 13:45:22 +00:00
|
|
|
|
if post.PostTitle != "" {
|
|
|
|
|
post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle)
|
|
|
|
|
}
|
|
|
|
|
if post.PostContent != "" {
|
|
|
|
|
format := `
|
2022-09-18 03:57:43 +00:00
|
|
|
|
<form action="/login" class="post-password-form" method="post">
|
2022-09-17 13:45:22 +00:00
|
|
|
|
<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)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 03:57:43 +00:00
|
|
|
|
func (h *IndexHandle) recentPosts() (r []models.WpPosts, err error) {
|
2022-09-14 13:30:59 +00:00
|
|
|
|
r, err = models.Find[models.WpPosts](models.SqlBuilder{{
|
|
|
|
|
"post_type", "post",
|
2022-09-17 13:45:22 +00:00
|
|
|
|
}, {"post_status", "publish"}}, "ID,post_title,post_password", "", models.SqlBuilder{{"post_date", "desc"}}, nil, 5)
|
|
|
|
|
for i := 0; i < len(r); i++ {
|
2022-09-18 03:57:43 +00:00
|
|
|
|
h.formatTitleAndContent(&r[i])
|
2022-09-17 13:45:22 +00:00
|
|
|
|
}
|
2022-09-14 13:30:59 +00:00
|
|
|
|
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 {
|
2022-09-16 14:32:05 +00:00
|
|
|
|
start = 1
|
2022-09-15 14:35:32 +00:00
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
}
|