修复bug 完善代码

This commit is contained in:
xing 2023-02-09 20:49:33 +08:00
parent 55a9e90ec9
commit b495dda30f
8 changed files with 142 additions and 58 deletions

View File

@ -32,7 +32,7 @@ func Detail(c *gin.Context) {
"post": post,
}
isApproveComment := false
status := plugins.Ok
stats := plugins.Ok
pw := sessions.Default(c).Get("post_password")
defer func() {
@ -40,7 +40,7 @@ func Detail(c *gin.Context) {
if err != nil {
code = http.StatusNotFound
c.Error(err)
status = plugins.Error
stats = plugins.Error
return
}
if isApproveComment == true {
@ -54,7 +54,7 @@ func Detail(c *gin.Context) {
Password: "",
Scene: plugins.Detail,
Code: code,
Stats: status,
Stats: stats,
})
}()
id := str.ToInteger[uint64](c.Param("id"), 0)
@ -62,10 +62,12 @@ func Detail(c *gin.Context) {
maxId, err := cache.GetMaxPostId(c)
logs.ErrPrintln(err, "get max post id")
if id > maxId || id <= 0 || err != nil {
stats = plugins.Empty404
return
}
post, err = cache.GetPostById(c, id)
if post.Id == 0 || err != nil || post.PostStatus != "publish" {
stats = plugins.Empty404
return
}
showComment := false

View File

@ -185,7 +185,10 @@ func (h *indexHandle) parseParams() (err error) {
})
h.setTitleLR(h.category, wpconfig.Options.Value("blogname"))
}
s := h.c.Query("s")
s, ok := h.c.GetQuery("s")
if ok {
h.scene = plugins.Search
}
if s != "" && strings.Replace(s, " ", "", -1) != "" {
q := str.Join("%", s, "%")
h.where = append(h.where, []string{
@ -194,7 +197,7 @@ func (h *indexHandle) parseParams() (err error) {
"or", "post_excerpt", "like", q, "",
}, []string{"post_password", ""})
h.postType = append(h.postType, "page", "attachment")
h.header = fmt.Sprintf("%s的搜索结果", s)
h.header = fmt.Sprintf("<span>%s</span>的搜索结果", s)
h.setTitleLR(str.Join(`"`, s, `"`, "的搜索结果"), wpconfig.Options.Value("blogname"))
h.search = s
h.scene = plugins.Search
@ -229,8 +232,6 @@ func Index(c *gin.Context) {
"recentPosts": recent,
"archives": archive,
"categories": categoryItems,
"search": h.search,
"header": h.header,
"recentComments": recentComments,
"posts": posts,
}
@ -266,6 +267,8 @@ func Index(c *gin.Context) {
return
}
ginH["title"] = h.getTitle()
ginH["search"] = h.search
ginH["header"] = h.header
if c.Param("month") != "" {
posts, totalRaw, err = cache.GetMonthPostIds(c, c.Param("year"), c.Param("month"), h.page, h.pageSize, h.order)
if err != nil {

View File

@ -21,7 +21,7 @@
这儿似乎什么都没有,试试搜索?
{{end}}
</p>
<form role="search" method="get" class="search-form" action="https://www.xloyy.com/">
<form role="search" method="get" class="search-form" action="/">
<label>
<span class="screen-reader-text">搜索:</span>
<input type="search" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">

View File

@ -0,0 +1,68 @@
package twentyfifteen
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/plugins"
"github.com/fthvgb1/wp-go/internal/theme/common"
"github.com/fthvgb1/wp-go/plugin/pagination"
)
const ThemeName = "twentyfifteen"
type handle struct {
common.Handle
templ string
}
func Hook(h2 common.Handle) {
h := handle{
Handle: h2,
templ: "twentyfifteen/posts/index.gohtml",
}
//h.GinH["HeaderImage"] = h.getHeaderImage(h.C)
if h.Stats == plugins.Empty404 {
h.C.HTML(h.Code, h.templ, h.GinH)
return
}
if h.Scene == plugins.Detail {
h.Detail()
return
}
h.Index()
}
var plugin = common.Plugins()
func (h handle) Index() {
if h.Stats != plugins.Empty404 {
h.GinH["posts"] = slice.Map(
h.GinH["posts"].([]models.Posts),
common.PluginFn[models.Posts](plugin, h.Handle, common.DigestsAndOthers(h.C)))
p, ok := h.GinH["pagination"]
if ok {
pp, ok := p.(pagination.ParsePagination)
if ok {
h.GinH["pagination"] = pagination.Paginate(plugins.TwentyFifteenPagination(), pp)
}
}
}
//h.GinH["bodyClass"] = h.bodyClass()
h.C.HTML(h.Code, h.templ, h.GinH)
}
func (h handle) Detail() {
//h.GinH["bodyClass"] = h.bodyClass()
//host, _ := wpconfig.Options.Load("siteurl")
if h.GinH["comments"] != nil {
comments := h.GinH["comments"].([]models.Comments)
dep := h.GinH["maxDep"].(int)
h.GinH["comments"] = plugins.FormatComments(h.C, plugins.CommentRender(), comments, dep)
}
h.templ = "twentyfifteen/posts/detail.gohtml"
h.C.HTML(h.Code, h.templ, h.GinH)
}

View File

@ -1,37 +1,48 @@
{{define "layout/empty"}}
<section id="primary" class="content-area">
<main id="main" class="site-main">
<div class="wrap">
<div id="primary" class="content-area">
<main id="main" class="site-main">
<section class="error-404 not-found">
<header class="page-header">
<h1 class="page-title">
{{if .search}}
未找到
{{else}}
有点尴尬诶!该页无法显示。
{{end}}
</h1>
</header><!-- .page-header -->
<section class="no-results not-found">
<header class="page-header">
<h1 class="page-title">
{{if .search}}
未找到
{{else}}
有点尴尬诶!该页无法显示。
{{end}}
</h1>
</header><!-- .page-header -->
<div class="page-content">
<div class="page-content">
<p>{{if .search}}
抱歉,没有符合您搜索条件的结果。请换其它关键词再试。
{{else}}
这儿似乎什么都没有,试试搜索?
{{end}}
</p>
<form role="search" method="get" class="search-form" action="/">
<label for="search-form-1">
<span class="screen-reader-text">搜索:</span>
</label>
<input type="search" id="search-form-1" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">
<button type="submit" class="search-submit">
<svg class="icon icon-search" aria-hidden="true" role="img"> <use href="#icon-search" xlink:href="#icon-search"></use> </svg>
<span class="screen-reader-text">搜索</span>
</button>
</form>
</div><!-- .page-content -->
</section><!-- .no-results -->
<p>{{if .search}}
抱歉,没有符合您搜索条件的结果。请换其它关键词再试。
{{else}}
这儿似乎什么都没有,试试搜索?
{{end}}
</p>
<form role="search" method="get" class="search-form" action="https://www.xloyy.com/">
<label>
<span class="screen-reader-text">搜索:</span>
<input type="search" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">
</label>
<input type="submit" class="search-submit screen-reader-text" value="搜索">
</form>
</main><!-- .site-main -->
</div><!-- .page-content -->
</section><!-- .no-results -->
</div>
</main><!-- .site-main -->
</section>
{{if .search }}
<aside id="secondary" class="widget-area" aria-label="博客边栏">
{{template "layout/sidebar" .}}
</aside>
{{end}}
</div>
{{end}}

View File

@ -4,7 +4,7 @@
<label for="search-form-1">
<span class="screen-reader-text">搜索:</span>
</label>
<input type="search" id="search-form-1" class="search-field" placeholder="搜索…" value="" name="s">
<input type="search" id="search-form-1" class="search-field" placeholder="搜索…" value="{{.search}}" name="s">
<button type="submit" class="search-submit">
<svg class="icon icon-search" aria-hidden="true" role="img">
<use href="#icon-search" xlink:href="#icon-search"></use>

View File

@ -7,22 +7,15 @@
<div class="wrap">
<header class="page-header">
<h2 class="page-title">文章</h2>
{{if .header}}
<h1 class="page-title">{{.header | unescaped}}</h1>
{{else}}
<h2 class="page-title">文章</h2>
{{end}}
</header>
<div id="primary" class="content-area">
<main id="main" class="site-main">
{{if .header}}
<header class="page-header">
<h1 class="page-title">
{{if .search}}
{{.header}}
{{else}}
{{.header |unescaped}}
{{end}}
</h1>
</header>
{{end}}
{{ range $k,$v:=.posts}}
<article id="post-{{$v.Id}}"
class="post-{{$v.Id}} post {{if $v.Thumbnail.Path}}has-post-thumbnail{{end}} type-post status-publish format-standard hentry category">

View File

@ -37,10 +37,6 @@ func Hook(cHandle common.Handle) {
templ: "twentyseventeen/posts/index.gohtml",
}
h.GinH["HeaderImage"] = h.getHeaderImage(h.C)
if h.Stats == plugins.Empty404 {
h.C.HTML(h.Code, h.templ, h.GinH)
return
}
if h.Scene == plugins.Detail {
h.Detail()
return
@ -75,6 +71,10 @@ func (h handle) Index() {
func (h handle) Detail() {
post := h.GinH["post"].(models.Posts)
h.GinH["bodyClass"] = h.bodyClass()
if h.Stats == plugins.Empty404 {
h.C.HTML(h.Code, h.templ, h.GinH)
return
}
//host, _ := wpconfig.Options.Load("siteurl")
host := ""
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail")
@ -84,13 +84,17 @@ func (h handle) Detail() {
img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
post.Thumbnail = img
h.GinH["post"] = post
comments := h.GinH["comments"].([]models.Comments)
dep := h.GinH["maxDep"].(int)
h.GinH["comments"] = plugins.FormatComments(h.C, comment{}, comments, dep)
if h.GinH["comments"] != nil {
comments := h.GinH["comments"].([]models.Comments)
dep := h.GinH["maxDep"].(int)
h.GinH["comments"] = plugins.FormatComments(h.C, commentFormat, comments, dep)
}
h.templ = "twentyseventeen/posts/detail.gohtml"
h.C.HTML(h.Code, h.templ, h.GinH)
}
var commentFormat = comment{}
type comment struct {
plugins.CommonCommentFormat
}
@ -136,6 +140,9 @@ func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
func (h handle) bodyClass() string {
s := ""
if h.Stats == plugins.Empty404 {
return "error404"
}
switch h.Scene {
case plugins.Search:
s = "search-no-results"