2023-03-01 05:17:12 +00:00
|
|
|
package wp
|
2023-02-08 15:49:48 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-03 11:11:27 +00:00
|
|
|
"fmt"
|
2023-02-28 15:38:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/maps"
|
2023-02-08 15:49:48 +00:00
|
|
|
"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"
|
2023-02-11 15:51:07 +00:00
|
|
|
"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"
|
2023-02-08 15:49:48 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2023-02-11 15:51:07 +00:00
|
|
|
"net/http"
|
2023-02-24 16:56:52 +00:00
|
|
|
"strings"
|
2023-02-08 15:49:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handle struct {
|
2023-03-11 15:59:00 +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
|
|
|
|
handleFns map[int][]HandleCall
|
|
|
|
err error
|
|
|
|
abort bool
|
|
|
|
componentsArgs map[string]any
|
|
|
|
componentFilterFn map[string][]func(*Handle, string) string
|
2023-03-01 05:17:12 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 15:49:28 +00:00
|
|
|
type HandlePlugins map[string]HandleFn[*Handle]
|
2023-02-28 15:38:23 +00:00
|
|
|
|
|
|
|
// Components Order 为执行顺序,降序执行
|
|
|
|
type Components struct {
|
2023-03-12 06:25:22 +00:00
|
|
|
Str string
|
|
|
|
Fn func(*Handle) string
|
2023-02-28 15:38:23 +00:00
|
|
|
Order int
|
|
|
|
}
|
|
|
|
|
|
|
|
type HandleFn[T any] func(T)
|
|
|
|
|
|
|
|
type HandlePipeFn[T any] func(HandleFn[T], T)
|
|
|
|
|
|
|
|
type HandleCall struct {
|
|
|
|
Fn HandleFn[*Handle]
|
|
|
|
Order int
|
|
|
|
}
|
|
|
|
|
2023-03-12 06:25:22 +00:00
|
|
|
func (h *Handle) ComponentFilterFn(name string) ([]func(*Handle, string) string, bool) {
|
|
|
|
fn, ok := h.componentFilterFn[name]
|
|
|
|
return fn, ok
|
2023-03-11 15:59:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) PushComponentFilterFn(name string, fns ...func(*Handle, string) string) {
|
|
|
|
h.componentFilterFn[name] = append(h.componentFilterFn[name], fns...)
|
|
|
|
}
|
|
|
|
func (h *Handle) ComponentFilterFnHook(name, s string) string {
|
|
|
|
calls, ok := h.componentFilterFn[name]
|
|
|
|
if ok {
|
|
|
|
return slice.Reduce(calls, func(fn func(*Handle, string) string, r string) string {
|
|
|
|
return fn(h, r)
|
|
|
|
}, s)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-03-02 15:49:28 +00:00
|
|
|
func (h *Handle) Abort() {
|
|
|
|
h.abort = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) CommonThemeMods() wpconfig.ThemeMods {
|
|
|
|
return h.themeMods
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:38:23 +00:00
|
|
|
func (h *Handle) Err() error {
|
|
|
|
return h.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) SetErr(err error) {
|
|
|
|
h.err = err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) Password() string {
|
|
|
|
return h.password
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) SetTempl(templ string) {
|
|
|
|
h.templ = templ
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) Scene() int {
|
|
|
|
return h.scene
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) SetDatas(GinH gin.H) {
|
|
|
|
maps.Merge(h.ginH, GinH)
|
|
|
|
}
|
|
|
|
func (h *Handle) SetData(k string, v any) {
|
|
|
|
h.ginH[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) PushClass(class ...string) {
|
|
|
|
h.class = append(h.class, class...)
|
2023-02-08 15:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-09 14:36:41 +00:00
|
|
|
func GetComponentsArgs[T any](h *Handle, k string, defaults T) T {
|
|
|
|
v, ok := h.componentsArgs[k]
|
|
|
|
if ok {
|
|
|
|
vv, ok := v.(T)
|
|
|
|
if ok {
|
|
|
|
return vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return defaults
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushComponentsArgsForSlice[T any](h *Handle, name string, v ...T) {
|
|
|
|
val, ok := h.componentsArgs[name]
|
|
|
|
if !ok {
|
|
|
|
var vv []T
|
|
|
|
vv = append(vv, v...)
|
|
|
|
h.componentsArgs[name] = vv
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, ok := val.([]T)
|
|
|
|
if ok {
|
|
|
|
vv = append(vv, v...)
|
|
|
|
h.componentsArgs[name] = vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func SetComponentsArgsForMap[K comparable, V any](h *Handle, name string, key K, v V) {
|
|
|
|
val, ok := h.componentsArgs[name]
|
|
|
|
if !ok {
|
|
|
|
vv := make(map[K]V)
|
|
|
|
vv[key] = v
|
|
|
|
h.componentsArgs[name] = vv
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, ok := val.(map[K]V)
|
|
|
|
if ok {
|
|
|
|
vv[key] = v
|
|
|
|
h.componentsArgs[name] = vv
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func MergeComponentsArgsForMap[K comparable, V any](h *Handle, name string, m map[K]V) {
|
|
|
|
val, ok := h.componentsArgs[name]
|
|
|
|
if !ok {
|
|
|
|
h.componentsArgs[name] = m
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vv, ok := val.(map[K]V)
|
|
|
|
if ok {
|
|
|
|
h.componentsArgs[name] = maps.Merge(vv, m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) SetComponentsArgs(key string, value any) {
|
|
|
|
h.componentsArgs[key] = value
|
|
|
|
}
|
|
|
|
|
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失败")
|
2023-02-11 15:51:07 +00:00
|
|
|
return &Handle{
|
2023-03-11 15:59:00 +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),
|
|
|
|
handleFns: make(map[int][]HandleCall),
|
|
|
|
componentsArgs: make(map[string]any),
|
|
|
|
componentFilterFn: make(map[string][]func(*Handle, string) string),
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
2023-02-09 08:08:18 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) NewCacheComponent(name string, order int, fn func(handle *Handle) string) Components {
|
2023-03-12 06:25:22 +00:00
|
|
|
return Components{Str: h.CacheStr(name, fn), Order: order}
|
2023-02-28 07:17:16 +00:00
|
|
|
}
|
|
|
|
|
2023-03-02 12:36:58 +00:00
|
|
|
func (h *Handle) PushHandleFn(statsOrScene int, fns ...HandleCall) {
|
|
|
|
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], fns...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) PushGroupHandleFn(statsOrScene, order int, fns ...HandleFn[*Handle]) {
|
|
|
|
var calls []HandleCall
|
|
|
|
for _, fn := range fns {
|
|
|
|
calls = append(calls, HandleCall{fn, order})
|
|
|
|
}
|
|
|
|
h.handleFns[statsOrScene] = append(h.handleFns[statsOrScene], calls...)
|
2023-02-23 15:42:51 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
|
|
|
|
h.ginH[name] = h.CacheStr(name, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) CacheStr(name string, fn func(*Handle) string) string {
|
2023-03-06 15:43:58 +00:00
|
|
|
return reload.GetAnyValBy(name, func() string {
|
|
|
|
return fn(h)
|
|
|
|
})
|
2023-02-18 16:14:33 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 07:17:16 +00:00
|
|
|
func (h *Handle) PushHeadScript(fn ...Components) {
|
2023-03-02 12:36:58 +00:00
|
|
|
h.PushComponents(constraints.HeadScript, fn...)
|
|
|
|
}
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) PushGroupHeadScript(order int, str ...string) {
|
2023-03-12 06:25:22 +00:00
|
|
|
h.PushGroupComponentStrs(constraints.HeadScript, order, str...)
|
2023-02-24 16:56:52 +00:00
|
|
|
}
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) PushCacheGroupHeadScript(key string, order int, fns ...func(*Handle) string) {
|
2023-03-11 15:59:00 +00:00
|
|
|
h.PushGroupCacheComponentFn(constraints.HeadScript, key, order, fns...)
|
2023-03-04 06:44:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 07:17:16 +00:00
|
|
|
func (h *Handle) PushFooterScript(fn ...Components) {
|
2023-03-02 12:36:58 +00:00
|
|
|
h.PushComponents(constraints.FooterScript, fn...)
|
|
|
|
}
|
|
|
|
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) PushGroupFooterScript(order int, fns ...string) {
|
2023-03-12 06:25:22 +00:00
|
|
|
h.PushGroupComponentStrs(constraints.FooterScript, order, fns...)
|
2023-02-08 15:49:48 +00:00
|
|
|
}
|
|
|
|
|
2023-03-04 06:44:51 +00:00
|
|
|
func (h *Handle) componentKey(name string) string {
|
|
|
|
return fmt.Sprintf("theme_%d_%s", h.scene, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) PushCacheGroupFooterScript(key string, order int, fns ...func(*Handle) string) {
|
2023-03-11 15:59:00 +00:00
|
|
|
h.PushGroupCacheComponentFn(constraints.FooterScript, key, order, fns...)
|
2023-03-04 06:44:51 +00:00
|
|
|
}
|
2023-03-11 15:59:00 +00:00
|
|
|
func (h *Handle) PushGroupCacheComponentFn(name, key string, order int, fns ...func(*Handle) string) {
|
2023-03-04 06:44:51 +00:00
|
|
|
v := reload.GetStrBy(key, "\n", h, fns...)
|
2023-03-12 06:25:22 +00:00
|
|
|
h.PushGroupComponentStrs(name, order, v)
|
2023-03-04 06:44:51 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:11:25 +00:00
|
|
|
func (h *Handle) GetPassword() {
|
|
|
|
pw := h.Session.Get("post_password")
|
|
|
|
if pw != nil {
|
2023-02-28 15:38:23 +00:00
|
|
|
h.password = pw.(string)
|
2023-02-11 15:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 14:58:35 +00:00
|
|
|
func (h *Handle) ExecHandleFns() {
|
2023-02-28 15:38:23 +00:00
|
|
|
calls, ok := h.handleFns[h.Stats]
|
2023-03-01 05:17:12 +00:00
|
|
|
var fns []HandleCall
|
2023-02-28 11:19:24 +00:00
|
|
|
if ok {
|
2023-03-01 05:17:12 +00:00
|
|
|
fns = append(fns, calls...)
|
2023-02-28 11:19:24 +00:00
|
|
|
}
|
2023-03-02 12:36:58 +00:00
|
|
|
calls, ok = h.handleFns[h.scene]
|
2023-02-28 12:51:02 +00:00
|
|
|
if ok {
|
2023-03-02 12:36:58 +00:00
|
|
|
fns = append(fns, calls...)
|
2023-03-01 05:17:12 +00:00
|
|
|
}
|
2023-03-02 12:36:58 +00:00
|
|
|
calls, ok = h.handleFns[constraints.AllStats]
|
|
|
|
if ok {
|
|
|
|
fns = append(fns, calls...)
|
|
|
|
}
|
|
|
|
slice.Sort(fns, func(i, j HandleCall) bool {
|
2023-03-01 05:17:12 +00:00
|
|
|
return i.Order > j.Order
|
|
|
|
})
|
|
|
|
for _, fn := range fns {
|
|
|
|
fn.Fn(h)
|
|
|
|
if h.abort {
|
|
|
|
break
|
2023-02-28 12:51:02 +00:00
|
|
|
}
|
2023-02-27 14:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handle) PreTemplate() {
|
2023-02-28 15:38:23 +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-23 09:26:20 +00:00
|
|
|
}
|
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() {
|
2023-03-03 15:09:39 +00:00
|
|
|
h.CommonComponents()
|
|
|
|
h.ExecHandleFns()
|
|
|
|
}
|
2023-02-28 15:59:17 +00:00
|
|
|
|
2023-03-03 15:09:39 +00:00
|
|
|
func (h *Handle) CommonComponents() {
|
2023-03-04 06:44:51 +00:00
|
|
|
h.AddCacheComponent("customLogo", CalCustomLogo)
|
|
|
|
h.PushCacheGroupHeadScript("siteIconAndCustomCss", 0, CalSiteIcon, CalCustomCss)
|
2023-02-28 15:59:17 +00:00
|
|
|
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
|
|
|
|
h.CalMultipleComponents()
|
|
|
|
h.CalBodyClass()
|
2023-03-02 13:16:43 +00:00
|
|
|
}, 10), NewHandleFn(func(h *Handle) {
|
2023-03-01 05:17:12 +00:00
|
|
|
h.C.HTML(h.Code, h.templ, h.ginH)
|
|
|
|
}, 0))
|
2023-02-09 15:15:10 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 07:17:16 +00:00
|
|
|
func (h *Handle) PushComponents(name string, components ...Components) {
|
2023-03-03 11:11:27 +00:00
|
|
|
k := h.componentKey(name)
|
|
|
|
h.components[k] = append(h.components[k], components...)
|
2023-02-28 07:17:16 +00:00
|
|
|
}
|
|
|
|
|
2023-03-12 06:25:22 +00:00
|
|
|
func (h *Handle) PushGroupComponentStrs(name string, order int, fns ...string) {
|
2023-03-02 12:36:58 +00:00
|
|
|
var calls []Components
|
|
|
|
for _, fn := range fns {
|
2023-03-12 06:25:22 +00:00
|
|
|
calls = append(calls, Components{
|
|
|
|
Str: fn,
|
|
|
|
Order: order,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
k := h.componentKey(name)
|
|
|
|
h.components[k] = append(h.components[k], calls...)
|
|
|
|
}
|
|
|
|
func (h *Handle) PushGroupComponentFns(name string, order int, fns ...func(*Handle) string) {
|
|
|
|
var calls []Components
|
|
|
|
for _, fn := range fns {
|
|
|
|
calls = append(calls, Components{
|
|
|
|
Fn: fn,
|
|
|
|
Order: order,
|
|
|
|
})
|
2023-03-02 12:36:58 +00:00
|
|
|
}
|
2023-03-03 11:11:27 +00:00
|
|
|
k := h.componentKey(name)
|
|
|
|
h.components[k] = append(h.components[k], calls...)
|
|
|
|
}
|
|
|
|
|
2023-02-28 07:17:16 +00:00
|
|
|
func (h *Handle) CalMultipleComponents() {
|
2023-02-28 15:38:23 +00:00
|
|
|
for k, ss := range h.components {
|
2023-03-04 06:44:51 +00:00
|
|
|
slice.Sort(ss, func(i, j Components) bool {
|
|
|
|
return i.Order > j.Order
|
|
|
|
})
|
|
|
|
v := strings.Join(slice.FilterAndMap(ss, func(t Components) (string, bool) {
|
2023-03-12 06:25:22 +00:00
|
|
|
s := t.Str
|
|
|
|
if s == "" && t.Fn != nil {
|
|
|
|
s = t.Fn(h)
|
|
|
|
}
|
2023-03-04 06:44:51 +00:00
|
|
|
return s, s != ""
|
|
|
|
}), "\n")
|
|
|
|
kk := strings.Split(k, "_")
|
|
|
|
key := kk[len(kk)-1]
|
2023-03-03 11:11:27 +00:00
|
|
|
h.ginH[key] = v
|
2023-02-24 16:56:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:51:02 +00:00
|
|
|
func NewHandleFn(fn HandleFn[*Handle], order int) HandleCall {
|
|
|
|
return HandleCall{Fn: fn, Order: order}
|
|
|
|
}
|
|
|
|
|
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) {
|
2023-02-28 15:38:23 +00:00
|
|
|
switch h.scene {
|
2023-02-24 11:34:19 +00:00
|
|
|
case constraints.Detail:
|
|
|
|
h.Detail.Render()
|
|
|
|
default:
|
|
|
|
h.Index.Render()
|
|
|
|
}
|
|
|
|
}
|