优化代码
This commit is contained in:
parent
eab6db83eb
commit
26950a37bb
|
@ -17,7 +17,7 @@ func (h *Handle) CalBodyClass() {
|
|||
|
||||
func (h *Handle) BodyClass(class ...string) string {
|
||||
if constraints.Ok != h.Stats {
|
||||
return "error404"
|
||||
class = append(class, "error404")
|
||||
}
|
||||
switch h.Scene {
|
||||
case constraints.Home:
|
||||
|
|
|
@ -40,8 +40,6 @@ func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
|||
Session: sessions.Default(c),
|
||||
GinH: gin.H{},
|
||||
Scene: scene,
|
||||
Code: http.StatusOK,
|
||||
Stats: constraints.Ok,
|
||||
ThemeMods: mods,
|
||||
Scripts: make(map[string][]func(*Handle) string),
|
||||
}
|
||||
|
@ -75,21 +73,42 @@ func (h *Handle) GetPassword() {
|
|||
}
|
||||
}
|
||||
|
||||
func (h *Handle) Render() {
|
||||
func (h *Handle) ExecHandleFns() {
|
||||
for _, fn := range h.HandleFns {
|
||||
fn(h)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handle) PreTemplate() {
|
||||
if h.Templ == "" {
|
||||
h.Templ = str.Join(h.Theme, "/posts/index.gohtml")
|
||||
if h.Scene == constraints.Detail {
|
||||
h.Templ = str.Join(h.Theme, "/posts/detail.gohtml")
|
||||
}
|
||||
}
|
||||
for _, fn := range h.HandleFns {
|
||||
fn(h)
|
||||
}
|
||||
func (h *Handle) PreCodeAndStats() {
|
||||
if h.Stats != 0 && h.Code != 0 {
|
||||
return
|
||||
}
|
||||
switch h.Stats {
|
||||
case constraints.Ok:
|
||||
h.Code = http.StatusOK
|
||||
case constraints.ParamError, constraints.Error404:
|
||||
h.Code = http.StatusNotFound
|
||||
case constraints.InternalErr:
|
||||
h.Code = http.StatusInternalServerError
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handle) Render() {
|
||||
h.PreCodeAndStats()
|
||||
h.PreTemplate()
|
||||
h.ExecHandleFns()
|
||||
h.PushHeadScript(constraints.HeadScript, CalSiteIcon, CalCustomCss)
|
||||
h.PlushComponent("customLogo", CalCustomLogo)
|
||||
h.CalMultipleScript()
|
||||
h.CalBodyClass()
|
||||
|
||||
h.C.HTML(h.Code, h.Templ, h.GinH)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||
"github.com/fthvgb1/wp-go/internal/pkg/cache"
|
||||
|
@ -9,7 +10,6 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
||||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DetailHandle struct {
|
||||
|
@ -38,11 +38,19 @@ func (d *DetailHandle) CheckAndGetPost() (err error) {
|
|||
id := str.ToInteger[uint64](d.C.Param("id"), 0)
|
||||
maxId, err := cache.GetMaxPostId(d.C)
|
||||
logs.ErrPrintln(err, "get max post id")
|
||||
if id > maxId || id <= 0 || err != nil {
|
||||
if id > maxId || id <= 0 {
|
||||
d.Stats = constraints.ParamError
|
||||
err = errors.New("无效的文档id")
|
||||
d.Class = append(d.Class, "error404")
|
||||
}
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
post, err := cache.GetPostById(d.C, id)
|
||||
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
|
||||
d.Stats = constraints.Error404
|
||||
logs.ErrPrintln(err, "获取id失败")
|
||||
err = errors.New(str.Join("无效的文档id "))
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -100,12 +108,6 @@ func (d *DetailHandle) Render() {
|
|||
}
|
||||
|
||||
func (d *DetailHandle) Details() {
|
||||
err := d.BuildDetailData()
|
||||
if err != nil {
|
||||
d.Stats = constraints.Error404
|
||||
d.Code = http.StatusNotFound
|
||||
d.C.HTML(d.Code, d.Templ, d.GinH)
|
||||
return
|
||||
}
|
||||
_ = d.BuildDetailData()
|
||||
d.Render()
|
||||
}
|
||||
|
|
|
@ -12,7 +12,6 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/plugins"
|
||||
"github.com/fthvgb1/wp-go/model"
|
||||
"github.com/fthvgb1/wp-go/plugin/pagination"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type IndexHandle struct {
|
||||
|
@ -58,8 +57,6 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
|
|||
|
||||
q := model.QueryCondition{
|
||||
Where: i.Param.Where,
|
||||
Page: i.Param.Page,
|
||||
Limit: i.Param.PageSize,
|
||||
Order: model.SqlBuilder{{i.Param.OrderBy, i.Param.Order}},
|
||||
Join: i.Param.Join,
|
||||
In: [][]any{i.Param.PostType, i.Param.PostStatus},
|
||||
|
@ -67,11 +64,11 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
|
|||
switch i.Scene {
|
||||
case constraints.Home, constraints.Category, constraints.Tag, constraints.Author:
|
||||
|
||||
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q)
|
||||
posts, totalRaw, err = cache.PostLists(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
||||
|
||||
case constraints.Search:
|
||||
|
||||
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, i.C, q)
|
||||
posts, totalRaw, err = cache.SearchPost(i.C, i.Param.CacheKey, i.C, q, i.Param.Page, i.Param.PageSize)
|
||||
|
||||
case constraints.Archive:
|
||||
|
||||
|
@ -91,7 +88,6 @@ func (i *IndexHandle) Pagination() {
|
|||
q = fmt.Sprintf("?%s", q)
|
||||
}
|
||||
paginations := pagination.NewParsePagination(i.TotalRows, i.Param.PageSize, i.Param.Page, i.Param.PaginationStep, q, i.C.Request.URL.Path)
|
||||
|
||||
i.GinH["pagination"] = pagination.Paginate(i.PageEle, paginations)
|
||||
|
||||
}
|
||||
|
@ -99,13 +95,14 @@ func (i *IndexHandle) Pagination() {
|
|||
func (i *IndexHandle) BuildIndexData(parm *IndexParams) (err error) {
|
||||
err = i.ParseIndex(parm)
|
||||
if err != nil {
|
||||
i.Stats = constraints.ParamError
|
||||
return
|
||||
}
|
||||
posts, totalRows, err := i.GetIndexData()
|
||||
if err != nil && err != sql.ErrNoRows {
|
||||
i.Stats = constraints.Error404
|
||||
return
|
||||
}
|
||||
i.GinH["posts"] = posts
|
||||
i.Posts = posts
|
||||
i.TotalRows = totalRows
|
||||
|
||||
|
@ -135,12 +132,6 @@ func (i *IndexHandle) Render() {
|
|||
}
|
||||
|
||||
func (i *IndexHandle) Indexs() {
|
||||
err := i.BuildIndexData(NewIndexParams(i.C))
|
||||
if err != nil {
|
||||
i.Stats = constraints.Error404
|
||||
i.Code = http.StatusNotFound
|
||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||
return
|
||||
}
|
||||
_ = i.BuildIndexData(NewIndexParams(i.C))
|
||||
i.Render()
|
||||
}
|
||||
|
|
16
internal/theme/twentyseventeen/posts/error.gohtml
Normal file
16
internal/theme/twentyseventeen/posts/error.gohtml
Normal file
|
@ -0,0 +1,16 @@
|
|||
{{template "layout/base" .}}
|
||||
|
||||
{{define "content"}}
|
||||
<div class="site-content-contain">
|
||||
<div id="content" class="site-content">
|
||||
<div class="wrap">
|
||||
<div id="primary" class="content-area">
|
||||
<main id="main" class="site-main">
|
||||
{{template "layout/empty"}}
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
|
|
@ -12,7 +12,6 @@ import (
|
|||
"github.com/fthvgb1/wp-go/internal/theme/common"
|
||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
@ -57,10 +56,8 @@ var listPostsPlugins = func() map[string]common.Plugin[models.Posts, *common.Han
|
|||
func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
|
||||
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
||||
if err != nil {
|
||||
i.Stats = constraints.Error404
|
||||
i.Code = http.StatusNotFound
|
||||
i.CalBodyClass()
|
||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||
i.Templ = str.Join(ThemeName, "/posts/error.gohtml")
|
||||
i.Render()
|
||||
return
|
||||
}
|
||||
i.PostsPlugins = listPostsPlugins
|
||||
|
@ -71,10 +68,8 @@ func index(next common.HandleFn[*common.Handle], i *common.IndexHandle) {
|
|||
func detail(next common.HandleFn[*common.Handle], d *common.DetailHandle) {
|
||||
err := d.BuildDetailData()
|
||||
if err != nil {
|
||||
d.Code = http.StatusNotFound
|
||||
d.Stats = constraints.Error404
|
||||
d.GinH["bodyClass"] = d.BodyClass()
|
||||
d.C.HTML(d.Code, d.Templ, d.GinH)
|
||||
d.Templ = str.Join(ThemeName, "/posts/error.gohtml")
|
||||
d.Render()
|
||||
return
|
||||
}
|
||||
if d.Post.Thumbnail.Path != "" {
|
||||
|
|
Loading…
Reference in New Issue
Block a user