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

View File

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

View File

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

View File

@ -4,7 +4,7 @@
<label for="search-form-1"> <label for="search-form-1">
<span class="screen-reader-text">搜索:</span> <span class="screen-reader-text">搜索:</span>
</label> </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"> <button type="submit" class="search-submit">
<svg class="icon icon-search" aria-hidden="true" role="img"> <svg class="icon icon-search" aria-hidden="true" role="img">
<use href="#icon-search" xlink:href="#icon-search"></use> <use href="#icon-search" xlink:href="#icon-search"></use>

View File

@ -7,22 +7,15 @@
<div class="wrap"> <div class="wrap">
<header class="page-header"> <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> </header>
<div id="primary" class="content-area"> <div id="primary" class="content-area">
<main id="main" class="site-main"> <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}} {{ range $k,$v:=.posts}}
<article id="post-{{$v.Id}}" <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"> 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", templ: "twentyseventeen/posts/index.gohtml",
} }
h.GinH["HeaderImage"] = h.getHeaderImage(h.C) 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 { if h.Scene == plugins.Detail {
h.Detail() h.Detail()
return return
@ -75,6 +71,10 @@ func (h handle) Index() {
func (h handle) Detail() { func (h handle) Detail() {
post := h.GinH["post"].(models.Posts) post := h.GinH["post"].(models.Posts)
h.GinH["bodyClass"] = h.bodyClass() 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, _ := wpconfig.Options.Load("siteurl")
host := "" host := ""
img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail") 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) img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset)
post.Thumbnail = img post.Thumbnail = img
h.GinH["post"] = post h.GinH["post"] = post
comments := h.GinH["comments"].([]models.Comments) if h.GinH["comments"] != nil {
dep := h.GinH["maxDep"].(int) comments := h.GinH["comments"].([]models.Comments)
h.GinH["comments"] = plugins.FormatComments(h.C, comment{}, comments, dep) dep := h.GinH["maxDep"].(int)
h.GinH["comments"] = plugins.FormatComments(h.C, commentFormat, comments, dep)
}
h.templ = "twentyseventeen/posts/detail.gohtml" h.templ = "twentyseventeen/posts/detail.gohtml"
h.C.HTML(h.Code, h.templ, h.GinH) h.C.HTML(h.Code, h.templ, h.GinH)
} }
var commentFormat = comment{}
type comment struct { type comment struct {
plugins.CommonCommentFormat plugins.CommonCommentFormat
} }
@ -136,6 +140,9 @@ func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) {
func (h handle) bodyClass() string { func (h handle) bodyClass() string {
s := "" s := ""
if h.Stats == plugins.Empty404 {
return "error404"
}
switch h.Scene { switch h.Scene {
case plugins.Search: case plugins.Search:
s = "search-no-results" s = "search-no-results"