diff --git a/go.mod b/go.mod index 8958307..56478be 100644 --- a/go.mod +++ b/go.mod @@ -13,6 +13,7 @@ require ( github.com/jmoiron/sqlx v1.3.5 github.com/soxfmr/gomail v0.0.0-20200806033254-80bf84e583f0 golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 + golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 gopkg.in/yaml.v2 v2.4.0 ) diff --git a/go.sum b/go.sum index 8ad4c55..428d1a8 100644 --- a/go.sum +++ b/go.sum @@ -31,8 +31,8 @@ github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGF github.com/goccy/go-json v0.9.11 h1:/pAaQDLHEoCq/5FFmSKBswWmK6H0e8g4159Kc/X/nqk= github.com/goccy/go-json v0.9.11/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= @@ -92,6 +92,8 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7 h1:WJywXQVIb56P2kAvXeMGTIgQ1ZHQxR60+F9dLsodECc= golang.org/x/crypto v0.0.0-20220924013350-4ba4fb4dd9e7/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9 h1:frX3nT9RkKybPnjyI+yvZh6ZucTZatCCEm9D47sZ2zo= +golang.org/x/exp v0.0.0-20230203172020-98cc5a0785f9/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= @@ -111,7 +113,6 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/helper/number/number.go b/helper/number/number.go index 9e55f0a..e493eae 100644 --- a/helper/number/number.go +++ b/helper/number/number.go @@ -4,19 +4,11 @@ package number import ( "fmt" + "golang.org/x/exp/constraints" "math/rand" ) -type IntNumber interface { - ~int | ~int64 | ~int32 | ~int8 | ~int16 | - ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 -} - -type Number interface { - IntNumber | ~float64 | ~float32 -} - -func Range[T IntNumber](start, end, step T) []T { +func Range[T constraints.Integer](start, end, step T) []T { if step == 0 { panic("step can't be 0") } @@ -36,12 +28,12 @@ func Range[T IntNumber](start, end, step T) []T { } // Rand 都为闭区间 [start,end] -func Rand[T IntNumber](start, end T) T { +func Rand[T constraints.Integer](start, end T) T { end++ return T(rand.Int63n(int64(end-start))) + start } -func Min[T Number](a ...T) T { +func Min[T constraints.Integer | constraints.Float](a ...T) T { min := a[0] for _, t := range a { if min > t { @@ -51,7 +43,7 @@ func Min[T Number](a ...T) T { return min } -func Max[T Number](a ...T) T { +func Max[T constraints.Integer | constraints.Float](a ...T) T { max := a[0] for _, t := range a { if max < t { @@ -61,7 +53,7 @@ func Max[T Number](a ...T) T { return max } -func Sum[T Number](a ...T) T { +func Sum[T constraints.Integer | constraints.Float](a ...T) T { s := T(0) for _, t := range a { s += t @@ -69,28 +61,28 @@ func Sum[T Number](a ...T) T { return s } -func Add[T Number](i, j T) T { +func Add[T constraints.Integer | constraints.Float](i, j T) T { return i + j } -func Sub[T Number](i, j T) T { +func Sub[T constraints.Integer | constraints.Float](i, j T) T { return i - j } -func ToString[T Number](n T) string { +func ToString[T constraints.Integer | constraints.Float](n T) string { return fmt.Sprintf("%v", n) } -func Abs[T Number](n T) T { +func Abs[T constraints.Integer | constraints.Float](n T) T { if n >= 0 { return n } return -n } -func Mul[T Number](i, j T) T { +func Mul[T constraints.Integer | constraints.Float](i, j T) T { return i * j } -func Divide[T Number](i, j T) T { +func Divide[T constraints.Integer | constraints.Float](i, j T) T { return i / j } diff --git a/internal/pkg/cache/headerImages.go b/internal/pkg/cache/headerImages.go index 5341a48..7d617e9 100644 --- a/internal/pkg/cache/headerImages.go +++ b/internal/pkg/cache/headerImages.go @@ -23,7 +23,7 @@ func getHeaderImages(a ...any) (r []models.PostThumbnail, err error) { mods, ok := wpconfig.Options.Load(fmt.Sprintf("theme_mods_%s", theme)) if ok && mods != "" { meta, er := plugins.UnPHPSerialize[plugins.HeaderImageMeta](mods) - if er != nil { + if er != nil || meta.HeaderImage == "" { err = er return } diff --git a/internal/theme/twentyseventeen/layout/base.gohtml b/internal/theme/twentyseventeen/layout/base.gohtml index d4d3f32..d01a1f2 100644 --- a/internal/theme/twentyseventeen/layout/base.gohtml +++ b/internal/theme/twentyseventeen/layout/base.gohtml @@ -28,13 +28,15 @@

{{"blogdescription"| getOption}}

+ {{if eq .scene 1}} + + + 向下滚动到内容 + + {{end}} - - - 向下滚动到内容 - diff --git a/internal/theme/twentyseventeen/twentyseventeen.go b/internal/theme/twentyseventeen/twentyseventeen.go index 972ab9d..fe7a5eb 100644 --- a/internal/theme/twentyseventeen/twentyseventeen.go +++ b/internal/theme/twentyseventeen/twentyseventeen.go @@ -8,7 +8,6 @@ import ( "github.com/fthvgb1/wp-go/internal/pkg/logs" "github.com/fthvgb1/wp-go/internal/pkg/models" "github.com/fthvgb1/wp-go/internal/plugins" - "github.com/fthvgb1/wp-go/internal/wpconfig" "github.com/fthvgb1/wp-go/plugin/pagination" "github.com/gin-gonic/gin" "strings" @@ -26,69 +25,68 @@ var paginate = func() plugins.PageEle { return p }() -func Hook(status int, c *gin.Context, h gin.H, scene, stats int) { - templ := "twentyseventeen/posts/index.gohtml" - h["HeaderImage"] = getHeaderImage(c) +type handle struct { + c *gin.Context + ginH gin.H + scene int + status int + stats int + templ string +} + +func Hook(status int, c *gin.Context, ginH gin.H, scene, stats int) { + h := handle{ + c: c, + ginH: ginH, + scene: scene, + status: status, + stats: stats, + templ: "twentyseventeen/posts/index.gohtml", + } + ginH["HeaderImage"] = h.getHeaderImage(c) if stats == plugins.Empty404 { - c.HTML(status, templ, h) + c.HTML(status, h.templ, ginH) + return } - if _, ok := plugins.IndexSceneMap[scene]; ok { - posts := h["posts"].([]models.Posts) - p, ok := h["pagination"] + if scene == plugins.Detail { + h.detail() + return + } + h.index() + +} + +func (h handle) index() { + posts := h.ginH["posts"].([]models.Posts) + p, ok := h.ginH["pagination"] + if ok { + pp, ok := p.(pagination.ParsePagination) if ok { - pp, ok := p.(pagination.ParsePagination) - if ok { - h["pagination"] = pagination.Paginate(paginate, pp) - } + h.ginH["pagination"] = pagination.Paginate(paginate, pp) } - d := 0 - s := "" - if scene == plugins.Search { - if len(posts) > 0 { - d = 1 - } else { - d = 0 - } - } else if scene == plugins.Category { - cat := c.Param("category") - _, cate := slice.SearchFirst(cache.Categories(c), func(my models.TermsMy) bool { - return my.Name == cat - }) - d = int(cate.Terms.TermId) - if cate.Slug[0] != '%' { - s = cate.Slug - } - } else if scene == plugins.Tag { - cat := c.Param("tag") - _, cate := slice.SearchFirst(cache.Tags(c), func(my models.TermsMy) bool { - return my.Name == cat - }) - d = int(cate.Terms.TermId) - if cate.Slug[0] != '%' { - s = cate.Slug - } - } - h["bodyClass"] = bodyClass(scene, d, s) - h["posts"] = postThumbnail(posts, scene) - } else if scene == plugins.Detail { - post := h["post"].(models.Posts) - h["bodyClass"] = bodyClass(scene, int(post.Id)) - //host, _ := wpconfig.Options.Load("siteurl") - host := "" - img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail") - img.Width = img.OriginAttachmentData.Width - img.Height = img.OriginAttachmentData.Height - img.Sizes = "100vw" - img.Srcset = fmt.Sprintf("%s %dw, %s", img.Path, img.Width, img.Srcset) - post.Thumbnail = img - h["post"] = post - comments := h["comments"].([]models.Comments) - dep := h["maxDep"].(int) - h["comments"] = plugins.FormatComments(c, comment{}, comments, dep) - templ = "twentyseventeen/posts/detail.gohtml" } - c.HTML(status, templ, h) - return + h.ginH["bodyClass"] = h.bodyClass() + h.ginH["posts"] = h.postThumbnail(posts, h.scene) + h.c.HTML(h.status, h.templ, h.ginH) +} + +func (h handle) detail() { + post := h.ginH["post"].(models.Posts) + h.ginH["bodyClass"] = h.bodyClass() + //host, _ := wpconfig.Options.Load("siteurl") + host := "" + img := plugins.Thumbnail(post.Thumbnail.OriginAttachmentData, "thumbnail", host, "thumbnail", "post-thumbnail") + img.Width = img.OriginAttachmentData.Width + img.Height = img.OriginAttachmentData.Height + img.Sizes = "100vw" + 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) + h.templ = "twentyseventeen/posts/detail.gohtml" + h.c.HTML(h.status, h.templ, h.ginH) } type comment struct { @@ -109,7 +107,7 @@ func (c comment) FormatLi(ctx *gin.Context, m models.Comments, depth int, isTls return plugins.FormatLi(templ, ctx, m, depth, isTls, eo, parent) } -func postThumbnail(posts []models.Posts, scene int) []models.Posts { +func (h handle) postThumbnail(posts []models.Posts, scene int) []models.Posts { return slice.Map(posts, func(t models.Posts) models.Posts { if t.Thumbnail.Path != "" { if slice.IsContained(scene, []int{plugins.Home, plugins.Archive, plugins.Search}) { @@ -122,33 +120,50 @@ func postThumbnail(posts []models.Posts, scene int) []models.Posts { }) } -func getHeaderImage(c *gin.Context) (r models.PostThumbnail) { +func (h handle) getHeaderImage(c *gin.Context) (r models.PostThumbnail) { r.Path = "/wp-content/themes/twentyseventeen/assets/images/header.jpg" r.Width = 2000 r.Height = 1200 - t, _ := wpconfig.Options.Load("template") - hs, err := cache.GetHeaderImages(c, t) + hs, err := cache.GetHeaderImages(c, ThemeName) if err != nil { logs.ErrPrintln(err, "获取页眉背景图失败") } else if len(hs) > 0 && err == nil { _, r = slice.Rand(hs) + } r.Sizes = "100vw" return } -func bodyClass(scene, d int, a ...any) string { +func (h handle) bodyClass() string { s := "" - if scene == plugins.Search { - if d > 0 { + switch h.scene { + case plugins.Search: + if len(h.ginH["posts"].([]models.Posts)) > 0 { s = "search-results" } else { s = "search-no-results" } - } else if scene == plugins.Category || scene == plugins.Tag { - s = fmt.Sprintf("category-%d %v", d, a[0]) - } else if scene == plugins.Detail { - s = fmt.Sprintf("postid-%d", d) + case plugins.Category: + cat := h.c.Param("category") + _, cate := slice.SearchFirst(cache.Categories(h.c), func(my models.TermsMy) bool { + return my.Name == cat + }) + if cate.Slug[0] != '%' { + s = cate.Slug + } + s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s) + case plugins.Tag: + cat := h.c.Param("tag") + _, cate := slice.SearchFirst(cache.Tags(h.c), func(my models.TermsMy) bool { + return my.Name == cat + }) + if cate.Slug[0] != '%' { + s = cate.Slug + } + s = fmt.Sprintf("category-%d %v", cate.Terms.TermId, s) + case plugins.Detail: + s = fmt.Sprintf("postid-%d", h.ginH["post"].(models.Posts).Id) } return map[int]string{ plugins.Home: "home blog ", @@ -157,5 +172,5 @@ func bodyClass(scene, d int, a ...any) string { plugins.Tag: str.Join("archive category page-two-column ", s), plugins.Search: str.Join("search ", s), plugins.Detail: str.Join("post-template-default single single-post single-format-standard ", s), - }[scene] + }[h.scene] } diff --git a/model/query.go b/model/query.go index 596faa4..0d47169 100644 --- a/model/query.go +++ b/model/query.go @@ -3,7 +3,7 @@ package model import ( "context" "fmt" - "github.com/fthvgb1/wp-go/helper/number" + "golang.org/x/exp/constraints" "math/rand" "strings" "time" @@ -87,7 +87,7 @@ func SimplePagination[T Model](ctx context.Context, where ParseWhere, fields, gr return } -func FindOneById[T Model, I number.IntNumber](ctx context.Context, id I) (T, error) { +func FindOneById[T Model, I constraints.Integer](ctx context.Context, id I) (T, error) { var r T sq := fmt.Sprintf("select * from `%s` where `%s`=?", r.Table(), r.PrimaryKey()) err := globalBb.Get(ctx, &r, sq, id)