wp-go/internal/theme/common/common.go

175 lines
4.1 KiB
Go
Raw Normal View History

package common
import (
"github.com/fthvgb1/wp-go/helper/slice"
2023-02-23 09:26:20 +00:00
str "github.com/fthvgb1/wp-go/helper/strings"
2023-02-18 16:14:33 +00:00
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
2023-02-09 15:15:10 +00:00
"github.com/fthvgb1/wp-go/internal/pkg/logs"
2023-02-18 15:35:39 +00:00
"github.com/fthvgb1/wp-go/internal/wpconfig"
2023-02-10 13:23:30 +00:00
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
"net/http"
2023-02-24 16:56:52 +00:00
"strings"
)
type Handle struct {
2023-02-28 07:17:16 +00:00
Index *IndexHandle
Detail *DetailHandle
C *gin.Context
Theme string
Session sessions.Session
GinH gin.H
Password string
Scene int
Code int
Stats int
Templ string
Class []string
Components map[string][]Components
ThemeMods wpconfig.ThemeMods
2023-02-28 11:19:24 +00:00
HandleFns map[int][]HandleFn[*Handle]
2023-02-28 12:29:18 +00:00
Error error
}
2023-02-14 11:47:47 +00:00
func NewHandle(c *gin.Context, scene int, theme string) *Handle {
2023-02-18 15:35:39 +00:00
mods, err := wpconfig.GetThemeMods(theme)
logs.ErrPrintln(err, "获取mods失败")
return &Handle{
2023-02-28 07:17:16 +00:00
C: c,
Theme: theme,
Session: sessions.Default(c),
GinH: gin.H{},
Scene: scene,
Stats: constraints.Ok,
ThemeMods: mods,
Components: make(map[string][]Components),
2023-02-28 11:44:19 +00:00
HandleFns: make(map[int][]HandleFn[*Handle]),
}
2023-02-09 08:08:18 +00:00
}
2023-02-28 07:17:16 +00:00
// Components Order 为执行顺序,降序执行
type Components struct {
Fn func(*Handle) string
Order int
}
func NewComponents(fn func(*Handle) string, order int) Components {
return Components{Fn: fn, Order: order}
}
2023-02-28 11:19:24 +00:00
func (h *Handle) PushHandleFn(stats int, fns ...HandleFn[*Handle]) {
h.HandleFns[stats] = append(h.HandleFns[stats], fns...)
}
2023-02-28 07:17:16 +00:00
func (h *Handle) AddComponent(name string, fn func(*Handle) string) {
2023-02-18 16:14:33 +00:00
v, ok := reload.GetStr(name)
if !ok {
v = fn(h)
2023-02-18 16:14:33 +00:00
reload.SetStr(name, v)
}
h.GinH[name] = v
}
2023-02-28 07:17:16 +00:00
func (h *Handle) PushHeadScript(fn ...Components) {
h.Components[constraints.HeadScript] = append(h.Components[constraints.HeadScript], fn...)
2023-02-24 16:56:52 +00:00
}
2023-02-28 07:17:16 +00:00
func (h *Handle) PushFooterScript(fn ...Components) {
h.Components[constraints.FooterScript] = append(h.Components[constraints.FooterScript], fn...)
}
2023-02-22 14:11:25 +00:00
func (h *Handle) GetPassword() {
pw := h.Session.Get("post_password")
if pw != nil {
h.Password = pw.(string)
}
}
2023-02-27 14:58:35 +00:00
func (h *Handle) ExecHandleFns() {
2023-02-28 11:19:24 +00:00
calls, ok := h.HandleFns[h.Stats]
if ok {
for _, call := range calls {
call(h)
}
}
for _, fn := range h.HandleFns[constraints.AllStats] {
2023-02-27 14:58:35 +00:00
fn(h)
}
}
func (h *Handle) PreTemplate() {
2023-02-23 09:26:20 +00:00
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")
}
2023-02-22 14:11:25 +00:00
}
2023-02-27 14:58:35 +00:00
}
func (h *Handle) PreCodeAndStats() {
if h.Stats != 0 && h.Code != 0 {
return
2023-02-23 10:21:51 +00:00
}
2023-02-27 14:58:35 +00:00
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()
2023-02-28 07:17:16 +00:00
h.PushHeadScript(Components{CalSiteIcon, 10}, Components{CalCustomCss, -1})
h.AddComponent("customLogo", CalCustomLogo)
h.CalMultipleComponents()
2023-02-23 09:26:20 +00:00
h.CalBodyClass()
h.C.HTML(h.Code, h.Templ, h.GinH)
2023-02-09 15:15:10 +00:00
}
2023-02-28 07:17:16 +00:00
func (h *Handle) PushComponents(name string, components ...Components) {
h.Components[name] = append(h.Components[name], components...)
}
func (h *Handle) CalMultipleComponents() {
for k, ss := range h.Components {
2023-02-24 16:56:52 +00:00
v, ok := reload.GetStr(k)
if !ok {
2023-02-28 07:17:16 +00:00
slice.SortSelf(ss, func(i, j Components) bool {
return i.Order > j.Order
})
v = strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
s := t.Fn(h)
2023-02-24 16:56:52 +00:00
return s, s != ""
}), "\n")
reload.SetStr(k, v)
}
h.GinH[k] = v
}
}
2023-02-22 14:11:25 +00:00
type HandleFn[T any] func(T)
2023-02-23 09:26:20 +00:00
type HandlePipeFn[T any] func(HandleFn[T], T)
2023-02-22 14:11:25 +00:00
2023-02-23 09:26:20 +00:00
// HandlePipe 方便把功能写在其它包里
2023-02-23 12:22:42 +00:00
func HandlePipe[T any](initial func(T), fns ...HandlePipeFn[T]) HandleFn[T] {
2023-02-23 09:26:20 +00:00
return slice.ReverseReduce(fns, func(next HandlePipeFn[T], f func(t T)) func(t T) {
return func(t T) {
next(f, t)
}
}, initial)
2023-02-09 04:57:18 +00:00
}
2023-02-24 11:34:19 +00:00
func Render(h *Handle) {
switch h.Scene {
case constraints.Detail:
h.Detail.Render()
default:
h.Index.Render()
}
}