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

233 lines
5.0 KiB
Go
Raw Normal View History

package common
import (
2023-02-28 15:38:23 +00:00
"github.com/fthvgb1/wp-go/helper/maps"
"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
2023-02-28 15:38:23 +00:00
theme string
2023-02-28 07:17:16 +00:00
Session sessions.Session
2023-02-28 15:38:23 +00:00
ginH gin.H
password string
scene int
2023-02-28 07:17:16 +00:00
Code int
Stats int
2023-02-28 15:38:23 +00:00
templ string
class []string
components map[string][]Components
themeMods wpconfig.ThemeMods
handleFns map[int][]HandleCall
err error
}
func (h *Handle) CommonThemeMods() wpconfig.ThemeMods {
return h.themeMods
}
// Components Order 为执行顺序,降序执行
type Components struct {
Fn func(*Handle) string
Order int
}
type HandleFn[T any] func(T)
type HandlePipeFn[T any] func(HandleFn[T], T)
type HandleCall struct {
Fn HandleFn[*Handle]
Order int
}
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-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,
2023-02-28 15:38:23 +00:00
theme: theme,
2023-02-28 07:17:16 +00:00
Session: sessions.Default(c),
2023-02-28 15:38:23 +00:00
ginH: gin.H{},
scene: scene,
2023-02-28 07:17:16 +00:00
Stats: constraints.Ok,
2023-02-28 15:38:23 +00:00
themeMods: mods,
components: make(map[string][]Components),
handleFns: make(map[int][]HandleCall),
}
2023-02-09 08:08:18 +00:00
}
2023-02-28 07:17:16 +00:00
func NewComponents(fn func(*Handle) string, order int) Components {
return Components{Fn: fn, Order: order}
}
2023-02-28 12:51:02 +00:00
func (h *Handle) PushHandleFn(stats int, fns ...HandleCall) {
2023-02-28 15:38:23 +00:00
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)
}
2023-02-28 15:38:23 +00:00
h.ginH[name] = v
2023-02-18 16:14:33 +00:00
}
2023-02-28 07:17:16 +00:00
func (h *Handle) PushHeadScript(fn ...Components) {
2023-02-28 15:38:23 +00:00
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) {
2023-02-28 15:38:23 +00:00
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 {
2023-02-28 15:38:23 +00:00
h.password = pw.(string)
}
}
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-02-28 11:19:24 +00:00
if ok {
2023-02-28 12:51:02 +00:00
slice.SortSelf(calls, func(i, j HandleCall) bool {
return i.Order > j.Order
})
2023-02-28 11:19:24 +00:00
for _, call := range calls {
2023-02-28 12:51:02 +00:00
call.Fn(h)
2023-02-28 11:19:24 +00:00
}
}
2023-02-28 15:38:23 +00:00
fns, ok := h.handleFns[constraints.AllStats]
2023-02-28 12:51:02 +00:00
if ok {
for _, fn := range fns {
fn.Fn(h)
}
2023-02-27 14:58:35 +00:00
}
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() {
h.PreCodeAndStats()
h.PreTemplate()
2023-02-28 07:17:16 +00:00
h.AddComponent("customLogo", CalCustomLogo)
2023-02-28 15:59:17 +00:00
h.PushHeadScript(Components{CalSiteIcon, 100}, Components{CalCustomCss, 0})
h.PushHandleFn(constraints.AllStats, NewHandleFn(func(h *Handle) {
h.CalMultipleComponents()
h.CalBodyClass()
}, 5))
h.ExecHandleFns()
2023-02-28 15:38:23 +00:00
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) {
2023-02-28 15:38:23 +00:00
h.components[name] = append(h.components[name], components...)
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-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)
}
2023-02-28 15:38:23 +00:00
h.ginH[k] = 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()
}
}