调整代码,index
This commit is contained in:
parent
efeeb8b675
commit
6d1fde655d
|
@ -21,10 +21,10 @@ var commonClass = map[int]string{
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) CalBodyClass() {
|
func (h *Handle) CalBodyClass() {
|
||||||
h.GinH["bodyClass"] = h.bodyClass(h.Class...)
|
h.GinH["bodyClass"] = h.BodyClass(h.Class...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) bodyClass(class ...string) string {
|
func (h *Handle) BodyClass(class ...string) string {
|
||||||
s := ""
|
s := ""
|
||||||
if constraints.Ok != h.Stats {
|
if constraints.Ok != h.Stats {
|
||||||
return "error404"
|
return "error404"
|
||||||
|
|
|
@ -2,6 +2,7 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/fthvgb1/wp-go/helper/slice"
|
"github.com/fthvgb1/wp-go/helper/slice"
|
||||||
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
||||||
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
"github.com/fthvgb1/wp-go/internal/cmd/reload"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
||||||
|
@ -23,7 +24,7 @@ type Handle struct {
|
||||||
Templ string
|
Templ string
|
||||||
Class []string
|
Class []string
|
||||||
ThemeMods wpconfig.ThemeMods
|
ThemeMods wpconfig.ThemeMods
|
||||||
Plugins []HandlePluginFn[*Handle]
|
HandleFns []func(*Handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
|
||||||
|
@ -61,19 +62,32 @@ func (h *Handle) GetPassword() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handle) ExecHandlePlugin() {
|
func (h *Handle) Render() {
|
||||||
if len(h.Plugins) > 0 {
|
if h.Templ == "" {
|
||||||
HandlePlugin(h.Plugins, h)
|
h.Templ = str.Join(h.Theme, "/posts/index.gohtml")
|
||||||
|
if h.Scene == constraints.Detail {
|
||||||
|
h.Templ = str.Join(h.Theme, "/posts/detail.gohtml")
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
h.AutoCal("siteIcon", h.CalSiteIcon)
|
||||||
|
h.AutoCal("customLogo", h.CalCustomLogo)
|
||||||
|
h.AutoCal("customCss", h.CalCustomCss)
|
||||||
|
h.CalBodyClass()
|
||||||
|
for _, fn := range h.HandleFns {
|
||||||
|
fn(h)
|
||||||
|
}
|
||||||
|
h.C.HTML(h.Code, h.Templ, h.GinH)
|
||||||
}
|
}
|
||||||
|
|
||||||
type HandleFn[T any] func(T)
|
type HandleFn[T any] func(T)
|
||||||
|
|
||||||
type HandlePluginFn[T any] func(HandleFn[T], T) HandleFn[T]
|
type HandlePipeFn[T any] func(HandleFn[T], T)
|
||||||
|
|
||||||
// HandlePlugin 方便把功能写在其它包里
|
// HandlePipe 方便把功能写在其它包里
|
||||||
func HandlePlugin[T any](fns []HandlePluginFn[T], h T) HandleFn[T] {
|
func HandlePipe[T any](fns []HandlePipeFn[T], initial func(T)) HandleFn[T] {
|
||||||
return slice.ReverseReduce(fns, func(t HandlePluginFn[T], r HandleFn[T]) HandleFn[T] {
|
return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) {
|
||||||
return t(r, h)
|
return func(t T) {
|
||||||
}, func(t T) {})
|
next(f, t)
|
||||||
|
}
|
||||||
|
}, initial)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,26 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type IndexHandle struct {
|
||||||
|
*Handle
|
||||||
|
Param *IndexParams
|
||||||
|
Posts []models.Posts
|
||||||
|
PageEle pagination.Elements
|
||||||
|
TotalRows int
|
||||||
|
PostsPlugins map[string]Plugin[models.Posts, *Handle]
|
||||||
|
Pipes []HandlePipeFn[*IndexHandle]
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewIndexHandle(handle *Handle) *IndexHandle {
|
||||||
|
return &IndexHandle{Handle: handle}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i *IndexHandle) Pipe(calls ...HandlePipeFn[*IndexHandle]) {
|
||||||
|
HandlePipe[*IndexHandle](append(calls, i.Pipes...), func(i *IndexHandle) {
|
||||||
|
i.Render()
|
||||||
|
})(i)
|
||||||
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
|
func (i *IndexHandle) ParseIndex(parm *IndexParams) (err error) {
|
||||||
i.Param = parm
|
i.Param = parm
|
||||||
switch i.Scene {
|
switch i.Scene {
|
||||||
|
@ -64,6 +84,9 @@ func (i *IndexHandle) GetIndexData() (posts []models.Posts, totalRaw int, err er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) Pagination() {
|
func (i *IndexHandle) Pagination() {
|
||||||
|
if i.PageEle == nil {
|
||||||
|
i.PageEle = plugins.TwentyFifteenPagination()
|
||||||
|
}
|
||||||
q := i.C.Request.URL.Query().Encode()
|
q := i.C.Request.URL.Query().Encode()
|
||||||
if q != "" {
|
if q != "" {
|
||||||
q = fmt.Sprintf("?%s", q)
|
q = fmt.Sprintf("?%s", q)
|
||||||
|
@ -108,18 +131,8 @@ func (i *IndexHandle) ExecPostsPlugin(calls ...func(*models.Posts)) {
|
||||||
|
|
||||||
func (i *IndexHandle) Render() {
|
func (i *IndexHandle) Render() {
|
||||||
i.ExecPostsPlugin()
|
i.ExecPostsPlugin()
|
||||||
if i.PageEle == nil {
|
|
||||||
i.PageEle = plugins.TwentyFifteenPagination()
|
|
||||||
}
|
|
||||||
i.Pagination()
|
i.Pagination()
|
||||||
i.AutoCal("siteIcon", i.CalSiteIcon)
|
i.Handle.Render()
|
||||||
i.AutoCal("customLogo", i.CalCustomLogo)
|
|
||||||
i.AutoCal("customCss", i.CalCustomCss)
|
|
||||||
i.CalBodyClass()
|
|
||||||
if i.Templ == "" {
|
|
||||||
i.Templ = fmt.Sprintf("%s/posts/index.gohtml", i.Theme)
|
|
||||||
}
|
|
||||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *IndexHandle) Indexs() {
|
func (i *IndexHandle) Indexs() {
|
||||||
|
@ -130,6 +143,5 @@ func (i *IndexHandle) Indexs() {
|
||||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i.ExecHandlePlugin()
|
|
||||||
i.Render()
|
i.Render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,10 +11,8 @@ import (
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
"github.com/fthvgb1/wp-go/internal/pkg/config"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/dao"
|
"github.com/fthvgb1/wp-go/internal/pkg/dao"
|
||||||
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
||||||
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
||||||
"github.com/fthvgb1/wp-go/model"
|
"github.com/fthvgb1/wp-go/model"
|
||||||
"github.com/fthvgb1/wp-go/plugin/pagination"
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -55,19 +53,6 @@ type IndexParams struct {
|
||||||
BlogName string
|
BlogName string
|
||||||
}
|
}
|
||||||
|
|
||||||
type IndexHandle struct {
|
|
||||||
*Handle
|
|
||||||
Param *IndexParams
|
|
||||||
Posts []models.Posts
|
|
||||||
PageEle pagination.Elements
|
|
||||||
TotalRows int
|
|
||||||
PostsPlugins map[string]Plugin[models.Posts, *Handle]
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIndexHandle(handle *Handle) *IndexHandle {
|
|
||||||
return &IndexHandle{Handle: handle}
|
|
||||||
}
|
|
||||||
|
|
||||||
var months = slice.SimpleToMap(number.Range(1, 12, 1), func(v int) int {
|
var months = slice.SimpleToMap(number.Range(1, 12, 1), func(v int) int {
|
||||||
return v
|
return v
|
||||||
})
|
})
|
||||||
|
|
|
@ -37,15 +37,6 @@ func newHandle(h *common.Handle) *handle {
|
||||||
return &handle{Handle: h}
|
return &handle{Handle: h}
|
||||||
}
|
}
|
||||||
|
|
||||||
type indexHandle struct {
|
|
||||||
*common.IndexHandle
|
|
||||||
h *handle
|
|
||||||
}
|
|
||||||
|
|
||||||
func newIndexHandle(iHandle *common.IndexHandle) *indexHandle {
|
|
||||||
return &indexHandle{IndexHandle: iHandle, h: newHandle(iHandle.Handle)}
|
|
||||||
}
|
|
||||||
|
|
||||||
type detailHandle struct {
|
type detailHandle struct {
|
||||||
*common.DetailHandle
|
*common.DetailHandle
|
||||||
h *handle
|
h *handle
|
||||||
|
@ -63,7 +54,7 @@ func Hook(h *common.Handle) {
|
||||||
newDetailHandle(common.NewDetailHandle(h)).Detail()
|
newDetailHandle(common.NewDetailHandle(h)).Detail()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newIndexHandle(common.NewIndexHandle(h)).Index()
|
common.NewIndexHandle(h).Pipe(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {
|
var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {
|
||||||
|
@ -72,19 +63,18 @@ var pluginFns = func() map[string]common.Plugin[models.Posts, *common.Handle] {
|
||||||
})
|
})
|
||||||
}()
|
}()
|
||||||
|
|
||||||
func (i *indexHandle) Index() {
|
func index(next common.HandleFn[*common.IndexHandle], i *common.IndexHandle) {
|
||||||
i.Templ = "twentyseventeen/posts/index.gohtml"
|
|
||||||
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
err := i.BuildIndexData(common.NewIndexParams(i.C))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
i.Stats = constraints.Error404
|
i.Stats = constraints.Error404
|
||||||
i.Code = http.StatusNotFound
|
i.Code = http.StatusNotFound
|
||||||
i.GinH["bodyClass"] = i.h.bodyClass()
|
i.CalBodyClass()
|
||||||
i.C.HTML(i.Code, i.Templ, i.GinH)
|
i.C.HTML(i.Code, i.Templ, i.GinH)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
i.PostsPlugins = pluginFns
|
i.PostsPlugins = pluginFns
|
||||||
i.PageEle = paginate
|
i.PageEle = paginate
|
||||||
i.Render()
|
next(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *detailHandle) Detail() {
|
func (d *detailHandle) Detail() {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (w SqlBuilder) parseField(ss []string, s *strings.Builder) {
|
func (w SqlBuilder) parseWhereField(ss []string, s *strings.Builder) {
|
||||||
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
if strings.Contains(ss[0], ".") && !strings.Contains(ss[0], "(") {
|
||||||
x := slice.Map(strings.Split(ss[0], "."), func(t string) string {
|
x := slice.Map(strings.Split(ss[0], "."), func(t string) string {
|
||||||
return str.Join("`", t, "`")
|
return str.Join("`", t, "`")
|
||||||
|
@ -81,11 +81,11 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
s.WriteString(ss[0])
|
s.WriteString(ss[0])
|
||||||
s.WriteString(" and ")
|
s.WriteString(" and ")
|
||||||
case 2:
|
case 2:
|
||||||
w.parseField(ss, &s)
|
w.parseWhereField(ss, &s)
|
||||||
s.WriteString("=? and ")
|
s.WriteString("=? and ")
|
||||||
args = append(args, ss[1])
|
args = append(args, ss[1])
|
||||||
case 3, 4:
|
case 3, 4:
|
||||||
w.parseField(ss, &s)
|
w.parseWhereField(ss, &s)
|
||||||
s.WriteString(ss[1])
|
s.WriteString(ss[1])
|
||||||
if w.parseIn(ss, &s, &c, &args, in) {
|
if w.parseIn(ss, &s, &c, &args, in) {
|
||||||
s.WriteString(" and ")
|
s.WriteString(" and ")
|
||||||
|
@ -115,7 +115,7 @@ func (w SqlBuilder) ParseWhere(in *[][]any) (string, []any, error) {
|
||||||
if i == 0 {
|
if i == 0 {
|
||||||
s.WriteString("( ")
|
s.WriteString("( ")
|
||||||
}
|
}
|
||||||
w.parseField(ss[start+1:end], &s)
|
w.parseWhereField(ss[start+1:end], &s)
|
||||||
s.WriteString(ss[start+2])
|
s.WriteString(ss[start+2])
|
||||||
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
|
if w.parseIn(ss[start+1:end], &s, &c, &args, in) {
|
||||||
s.WriteString(" and ")
|
s.WriteString(" and ")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user