wp-go/internal/actions/index.go

303 lines
8.4 KiB
Go
Raw Normal View History

2022-09-18 04:34:48 +00:00
package actions
2022-09-01 02:31:11 +00:00
import (
"errors"
2022-09-15 14:35:32 +00:00
"fmt"
"github.com/fthvgb1/wp-go/helper/maps"
"github.com/fthvgb1/wp-go/helper/number"
2023-01-21 11:31:23 +00:00
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
2023-01-19 13:02:39 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/cache"
2023-01-22 10:01:04 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/config"
"github.com/fthvgb1/wp-go/internal/pkg/dao"
2023-01-18 15:20:49 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/logs"
2023-01-18 15:04:12 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme"
"github.com/fthvgb1/wp-go/internal/theme/common"
2023-01-18 15:04:12 +00:00
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/fthvgb1/wp-go/model"
"github.com/fthvgb1/wp-go/plugin/pagination"
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:35:32 +00:00
"math"
2022-09-01 02:31:11 +00:00
"net/http"
2022-09-14 05:28:31 +00:00
"strconv"
2022-09-15 14:35:32 +00:00
"strings"
2022-12-28 06:24:59 +00:00
"sync/atomic"
"time"
2022-09-01 02:31:11 +00:00
)
2022-09-18 14:06:27 +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
2022-11-17 03:22:29 +00:00
author string
2022-09-17 09:11:10 +00:00
totalPage int
category string
categoryType string
2023-01-12 12:42:16 +00:00
where model.SqlBuilder
2022-09-28 08:23:20 +00:00
orderBy string
2022-09-17 09:11:10 +00:00
order string
2023-01-12 12:42:16 +00:00
join model.SqlBuilder
2022-09-20 14:37:31 +00:00
postType []any
postStatus []any
2022-09-17 09:11:10 +00:00
header string
paginationStep int
scene int
stats int
2022-09-17 09:05:43 +00:00
}
2022-09-18 14:06:27 +00:00
func newIndexHandle(ctx *gin.Context) *indexHandle {
size := str.ToInteger(wpconfig.Options.Value("posts_per_page"), 10)
2022-09-18 14:06:27 +00:00
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: size,
2022-09-17 09:11:10 +00:00
paginationStep: 1,
2022-11-17 08:29:39 +00:00
titleL: wpconfig.Options.Value("blogname"),
titleR: wpconfig.Options.Value("blogdescription"),
2023-01-12 12:42:16 +00:00
where: model.SqlBuilder{
2022-09-17 09:05:43 +00:00
{"post_type", "in", ""},
{"post_status", "in", ""},
},
orderBy: "post_date",
join: model.SqlBuilder{},
postType: []any{"post"},
postStatus: []any{"publish"},
scene: plugins.Home,
stats: plugins.Ok,
2022-09-17 09:05:43 +00:00
}
}
var months = slice.SimpleToMap(number.Range(1, 12, 1), func(v int) int {
return v
})
2022-09-18 14:06:27 +00:00
func (h *indexHandle) setTitleLR(l, r string) {
2022-09-17 09:05:43 +00:00
h.titleL = l
h.titleR = r
}
2022-09-18 14:06:27 +00:00
func (h *indexHandle) getTitle() string {
2022-09-17 09:05:43 +00:00
h.title = fmt.Sprintf("%s-%s", h.titleL, h.titleR)
return h.title
}
2022-09-28 08:23:20 +00:00
func (h *indexHandle) getSearchKey() string {
2022-11-17 03:22:29 +00:00
return fmt.Sprintf("action:%s|%s|%s|%s|%s|%s|%d|%d", h.author, h.search, h.orderBy, h.order, h.category, h.categoryType, h.page, h.pageSize)
2022-09-28 08:23:20 +00:00
}
var orders = map[string]struct{}{"asc": {}, "desc": {}}
2023-01-22 10:01:04 +00:00
2022-11-17 03:22:29 +00:00
func (h *indexHandle) parseParams() (err error) {
2022-09-17 09:05:43 +00:00
h.order = h.c.Query("order")
if !maps.IsExists(orders, h.order) {
2023-02-05 13:06:04 +00:00
order := config.GetConfig().PostOrder
2022-09-17 09:05:43 +00:00
h.order = "asc"
if order != "" && maps.IsExists(orders, order) {
2023-01-22 10:01:04 +00:00
h.order = order
}
2022-09-17 09:05:43 +00:00
}
year := h.c.Param("year")
2022-09-16 06:46:51 +00:00
if year != "" {
y := str.ToInteger(year, -1)
if y > time.Now().Year() || y <= 1970 {
return errors.New(str.Join("year err : ", 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 != "" {
m := str.ToInteger(month, -1)
if !maps.IsExists(months, m) {
return errors.New(str.Join("months err ", 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)
2022-11-17 08:29:39 +00:00
h.setTitleLR(ss, wpconfig.Options.Value("blogname"))
2022-09-23 13:46:34 +00:00
h.scene = plugins.Archive
2022-09-16 06:46:51 +00:00
}
2022-09-17 09:05:43 +00:00
category := h.c.Param("category")
if category != "" {
2023-01-26 16:52:59 +00:00
h.scene = plugins.Category
if !maps.IsExists(cache.AllCategoryTagsNames(h.c, plugins.Category), category) {
return errors.New(str.Join("not exists category ", category))
}
2022-09-17 09:05:43 +00:00
h.categoryType = "category"
h.header = fmt.Sprintf("分类: <span>%s</span>", category)
h.category = category
2022-09-16 09:59:17 +00:00
}
tag := h.c.Param("tag")
if tag != "" {
h.scene = plugins.Tag
if !maps.IsExists(cache.AllCategoryTagsNames(h.c, plugins.Tag), tag) {
return errors.New(str.Join("not exists tag ", tag))
}
h.categoryType = "post_tag"
h.header = fmt.Sprintf("标签: <span>%s</span>", tag)
h.category = tag
}
2022-11-17 03:22:29 +00:00
username := h.c.Param("author")
if username != "" {
allUsername, er := cache.GetAllUsername(h.c)
if er != nil {
err = er
return
}
if !maps.IsExists(allUsername, username) {
err = errors.New(str.Join("user ", username, " is not exists"))
return
}
2023-01-19 13:02:39 +00:00
user, er := cache.GetUserByName(h.c, username)
2022-11-17 03:22:29 +00:00
if er != nil {
err = er
return
}
h.author = username
h.where = append(h.where, []string{
"post_author", "=", strconv.FormatUint(user.Id, 10), "int",
})
}
if h.category != "" {
2022-09-17 09:05:43 +00:00
h.where = append(h.where, []string{
"d.name", h.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",
})
h.setTitleLR(h.category, wpconfig.Options.Value("blogname"))
2022-09-17 09:05:43 +00:00
}
2023-02-09 12:49:33 +00:00
s, ok := h.c.GetQuery("s")
if ok {
h.scene = plugins.Search
}
2022-09-17 09:05:43 +00:00
if s != "" && strings.Replace(s, " ", "", -1) != "" {
2023-01-21 11:31:23 +00:00
q := str.Join("%", s, "%")
2022-09-17 09:05:43 +00:00
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")
2023-02-09 12:49:33 +00:00
h.header = fmt.Sprintf("<span>%s</span>的搜索结果", s)
2023-01-21 11:31:23 +00:00
h.setTitleLR(str.Join(`"`, s, `"`, "的搜索结果"), wpconfig.Options.Value("blogname"))
2022-09-17 09:05:43 +00:00
h.search = s
2022-09-23 13:46:34 +00:00
h.scene = plugins.Search
2022-09-16 06:46:51 +00:00
}
h.page = str.ToInteger(h.c.Param("page"), 1)
2023-01-12 12:42:16 +00:00
total := int(atomic.LoadInt64(&dao.TotalRaw))
2022-12-28 06:24:59 +00:00
if total > 0 && total < (h.page-1)*h.pageSize {
2022-10-04 03:13:14 +00:00
h.page = 1
}
2022-09-17 09:05:43 +00:00
if h.page > 1 && (h.category != "" || h.search != "" || month != "") {
2022-11-17 08:29:39 +00:00
h.setTitleLR(fmt.Sprintf("%s-第%d页", h.titleL, h.page), wpconfig.Options.Value("blogname"))
2022-09-17 07:37:23 +00:00
}
2022-11-17 03:22:29 +00:00
return
2022-09-17 09:05:43 +00:00
}
2022-09-15 14:35:32 +00:00
2022-09-18 14:06:27 +00:00
func (h *indexHandle) getTotalPage(totalRaws int) int {
2022-09-17 09:05:43 +00:00
h.totalPage = int(math.Ceil(float64(totalRaws) / float64(h.pageSize)))
return h.totalPage
}
2022-09-18 04:34:48 +00:00
func Index(c *gin.Context) {
2022-09-18 14:06:27 +00:00
h := newIndexHandle(c)
var posts []models.Posts
2022-11-17 03:22:29 +00:00
var totalRaw int
var err error
2023-01-19 13:02:39 +00:00
archive := cache.Archives(c)
2023-02-09 07:43:20 +00:00
recent := slice.Map(cache.RecentPosts(c, 5), common.ProjectTitle)
2023-02-05 12:33:33 +00:00
categoryItems := cache.CategoriesTags(c, plugins.Category)
2023-01-19 13:02:39 +00:00
recentComments := cache.RecentComments(c, 5)
2022-09-19 12:15:10 +00:00
ginH := gin.H{
"err": err,
2022-09-21 13:49:14 +00:00
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"recentComments": recentComments,
2023-02-06 07:59:44 +00:00
"posts": posts,
2022-09-19 12:15:10 +00:00
}
pw := h.session.Get("post_password")
pws, ok := pw.(string)
if !ok {
pws = ""
}
2022-11-17 03:22:29 +00:00
defer func() {
code := http.StatusOK
2022-11-17 03:22:29 +00:00
if err != nil {
code = http.StatusNotFound
if h.stats == plugins.InternalErr {
code = http.StatusInternalServerError
c.Error(err)
return
}
2022-11-17 03:22:29 +00:00
c.Error(err)
h.stats = plugins.Error
2022-11-17 03:22:29 +00:00
}
2023-01-20 11:58:45 +00:00
t := theme.GetTemplateName()
theme.Hook(t, common.Handle{
C: c,
GinH: ginH,
Password: pws,
Scene: h.scene,
Code: code,
Stats: h.stats,
})
2022-11-17 03:22:29 +00:00
}()
err = h.parseParams()
if err != nil {
return
}
ginH["title"] = h.getTitle()
2023-02-09 12:49:33 +00:00
ginH["search"] = h.search
ginH["header"] = h.header
2022-09-27 13:52:15 +00:00
if c.Param("month") != "" {
posts, totalRaw, err = cache.GetMonthPostIds(c, c.Param("year"), c.Param("month"), h.page, h.pageSize, h.order)
2022-09-27 13:52:15 +00:00
if err != nil {
return
}
2022-09-28 12:02:43 +00:00
} else if h.search != "" {
posts, totalRaw, err = cache.SearchPost(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.postStatus)
2022-09-27 13:52:15 +00:00
} else {
posts, totalRaw, err = cache.PostLists(c, h.getSearchKey(), c, h.where, h.page, h.pageSize, model.SqlBuilder{{h.orderBy, h.order}}, h.join, h.postType, h.postStatus)
2022-09-27 13:52:15 +00:00
}
2022-09-17 09:05:43 +00:00
if err != nil {
h.stats = plugins.Error
2023-01-18 15:20:49 +00:00
logs.ErrPrintln(err, "获取数据错误")
2022-09-17 09:05:43 +00:00
return
}
2023-01-18 15:20:49 +00:00
if len(posts) < 1 && h.category != "" {
2022-09-17 09:05:43 +00:00
h.titleL = "未找到页面"
h.stats = plugins.Empty404
2022-09-17 09:05:43 +00:00
}
2022-09-15 14:35:32 +00:00
q := c.Request.URL.Query().Encode()
2022-09-27 13:52:15 +00:00
if q != "" {
q = fmt.Sprintf("?%s", q)
}
ginH["posts"] = posts
2022-09-19 12:15:10 +00:00
ginH["totalPage"] = h.getTotalPage(totalRaw)
2023-01-18 15:04:12 +00:00
ginH["currentPage"] = h.page
2022-09-19 12:15:10 +00:00
ginH["title"] = h.getTitle()
2023-01-25 18:26:36 +00:00
ginH["scene"] = h.scene
2023-01-19 14:02:45 +00:00
ginH["pagination"] = pagination.NewParsePagination(totalRaw, h.pageSize, h.page, h.paginationStep, q, c.Request.URL.Path)
2022-09-01 02:31:11 +00:00
}