接入后台近期文章设置

This commit is contained in:
xing 2023-03-06 23:43:58 +08:00
parent 240a41f1eb
commit 096514a677
14 changed files with 120 additions and 95 deletions

View File

@ -10,11 +10,16 @@ var calls []func()
var str = safety.NewMap[string, string]()
func GetStr(name string) (string, bool) {
return str.Load(name)
}
func SetStr(name, val string) {
str.Store(name, val)
var anyMap = safety.NewMap[string, any]()
func GetAnyValBy[T any](k string, fn func() T) T {
v, ok := anyMap.Load(k)
if ok {
return v.(T)
}
vv := fn()
anyMap.Store(k, vv)
return vv
}
func GetStrBy[T any](key, delimiter string, t T, fn ...func(T) string) string {
@ -52,5 +57,6 @@ func Reload() {
for _, call := range calls {
call()
}
anyMap.Flush()
str.Flush()
}

View File

@ -3,9 +3,12 @@ package cache
import (
"context"
"fmt"
"github.com/fthvgb1/wp-go/helper/number"
"github.com/fthvgb1/wp-go/helper/slice"
str "github.com/fthvgb1/wp-go/helper/strings"
"github.com/fthvgb1/wp-go/internal/pkg/logs"
"github.com/fthvgb1/wp-go/internal/pkg/models"
"github.com/fthvgb1/wp-go/internal/wpconfig"
"github.com/gin-gonic/gin"
"time"
)
@ -44,9 +47,8 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) {
func RecentPosts(ctx context.Context, n int) (r []models.Posts) {
nn := n
if nn <= 5 {
nn = 10
}
feedNum := str.ToInteger(wpconfig.GetOption("posts_per_rss"), 10)
nn = number.Max(n, feedNum)
r, err := recentPostsCaches.GetCache(ctx, time.Second, ctx, nn)
if n < len(r) {
r = r[:n]

View File

@ -129,8 +129,8 @@ func RecentPosts(a ...any) (r []models.Posts, err error) {
{"post_type", "post"},
{"post_status", "publish"},
}),
model.Fields("ID,post_title,post_password"),
model.Order(model.SqlBuilder{{"post_date", "desc"}}),
model.Fields("ID,post_title,post_password,post_date_gmt"),
model.Order([][]string{{"post_date", "desc"}}),
model.Limit(num),
))
return

View File

@ -33,7 +33,7 @@ func commonTemplate(t *multipTemplate.MultipleFsTemplate) {
for _, main := range m {
file := filepath.Base(main)
dir := strings.Split(main, "/")[0]
templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml"))
templ := template.Must(template.New(file).Funcs(t.FuncMap).ParseFS(t.Fs, main, filepath.Join(dir, "layout/*.gohtml"), "wp/template.gohtml", "wp/*/*/*.gohtml"))
t.SetTemplate(main, templ)
}
}

View File

@ -13,6 +13,9 @@ var comFn = template.FuncMap{
"dateCh": func(t time.Time) any {
return t.Format("2006年 01月 02日")
},
"timeFormat": func(t time.Time, format string) any {
return t.Format(format)
},
"getOption": func(k string) string {
return wpconfig.GetOption(k)
},

View File

@ -9,19 +9,7 @@
<input type="submit" class="search-submit screen-reader-text" value="搜索">
</form>
</aside>
<aside id="recent-posts-2" class="widget widget_recent_entries">
<h2 class="widget-title">近期文章</h2>
<nav aria-label="近期文章">
<ul>
{{range $k,$v:=.recentPosts}}
<li>
<a href="/p/{{$v.Id}}">{{$v.PostTitle}}</a>
</li>
{{end}}
</ul>
</nav>
</aside>
{{template "common/recent-posts" .}}
<aside id="recent-comments-2" class="widget widget_recent_comments">
<h2 class="widget-title">近期评论</h2>
<nav aria-label="近期评论">

View File

@ -13,19 +13,7 @@
</button>
</form>
</section>
<section id="recent-posts-2" class="widget widget_recent_entries">
<h2 class="widget-title">近期文章</h2>
<nav aria-label="近期文章">
<ul>
{{range $k,$v:=.recentPosts}}
<li>
<a href="/p/{{$v.Id}}">{{$v.PostTitle}}</a>
</li>
{{end}}
</ul>
</nav>
</section>
{{template "common/recent-posts" .}}
<section id="recent-comments-2" class="widget widget_recent_comments"><h2 class="widget-title">近期评论</h2>
<nav aria-label="近期评论">
<ul id="recentcomments">

View File

@ -0,0 +1,38 @@
{{define "common/archives"}}
<section id="archives-2" class="widget widget_archive">
<h2 class="widget-title">{{.archivesConfig.title}}</h2>
{{ if eq .archivesConfig.dropdown 1}}
<label class="screen-reader-text" for="archives-dropdown-2">{{.archivesConfig.title}}</label>
<select id="archives-dropdown-2" name="archive-dropdown">
<option value="">选择月份</option>
{{range $k,$v := .archives}}
<option {{if and (eq $.archiveYear $v.Year) (eq $.archiveMonth $v.Month) }}selected{{end}} value="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}"> {{$v.Year}}年{{$v.Month}}月 {{if eq $.archivesConfig.count 1}}&nbsp;({{$v.Posts}}){{end}} </option>
{{end}}
</select>
<script>
/* <![CDATA[ */
(function() {
const dropdown = document.getElementById("archives-dropdown-2");
function onSelectChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
document.location.href = this.options[ this.selectedIndex ].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
{{else}}
<nav aria-label="{{.archiveTitle}}">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}}</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
</section>
{{end}}

View File

@ -0,0 +1,18 @@
{{define "common/recent-posts"}}
<aside id="recent-posts-2" class="widget widget_recent_entries">
<h2 class="widget-title">{{.recentPostsConfig.title}}</h2>
<nav aria-label="{{.recentPostsConfig.title}}">
<ul>
{{range $k,$v:=.recentPosts}}
<li>
<a href="/p/{{$v.Id}}">{{$v.PostTitle}}</a>
{{ if $.recentPostsConfig.show_date}}
<span class="post-date">{{ timeFormat $v.PostDateGmt "2006年01月02日"}}</span>
{{end}}
</li>
{{end}}
</ul>
</nav>
</aside>
{{end}}

View File

@ -23,42 +23,3 @@
{{.customLogo|unescaped}}
{{end}}
{{end}}
{{define "common/archives"}}
<section id="archives-2" class="widget widget_archive">
<h2 class="widget-title">{{.archiveTitle}}</h2>
{{ if eq .archiveDropdown 1}}
<label class="screen-reader-text" for="archives-dropdown-2">{{.archiveTitle}}</label>
<select id="archives-dropdown-2" name="archive-dropdown">
<option value="">选择月份</option>
{{range $k,$v := .archives}}
<option {{if and (eq $.archiveYear $v.Year) (eq $.archiveMonth $v.Month) }}selected{{end}} value="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}"> {{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}} </option>
{{end}}
</select>
<script>
/* <![CDATA[ */
(function() {
const dropdown = document.getElementById("archives-dropdown-2");
function onSelectChange() {
if ( dropdown.options[ dropdown.selectedIndex ].value !== '' ) {
document.location.href = this.options[ this.selectedIndex ].value;
}
}
dropdown.onchange = onSelectChange;
})();
/* ]]> */
</script>
{{else}}
<nav aria-label="{{.archiveTitle}}">
<ul>
{{range $k,$v := .archives}}
<li><a href="/p/date/{{$v.Year}}/{{$v.Month|printf "%02s"}}">{{$v.Year}}年{{$v.Month}}月 {{if eq $.showArchiveCount 1}}&nbsp;({{$v.Posts}}){{end}}</a>
</li>
{{end}}
</ul>
</nav>
{{end}}
</section>
{{end}}

View File

@ -2,18 +2,42 @@ package wp
import (
"github.com/fthvgb1/wp-go/helper/slice"
"github.com/fthvgb1/wp-go/internal/cmd/reload"
"github.com/fthvgb1/wp-go/internal/pkg/cache"
"github.com/fthvgb1/wp-go/internal/pkg/constraints"
"github.com/fthvgb1/wp-go/internal/wpconfig"
)
func (h *Handle) WidgetAreaData() {
h.ginH["showArchiveCount"] = wpconfig.GetPHPArrayValWithDefaults[int64]("widget_archives", 0, int64(2), "count")
h.ginH["archiveDropdown"] = wpconfig.GetPHPArrayValWithDefaults[int64]("widget_archives", 0, int64(2), "dropdown")
h.ginH["archiveTitle"] = wpconfig.GetPHPArrayValWithDefaults[string]("widget_archives", "归档", int64(2), "title")
h.ginH["archives"] = cache.Archives(h.C)
h.ginH["recentPosts"] = slice.Map(cache.RecentPosts(h.C, 5), ProjectTitle)
h.Archives()
h.RecentPosts()
h.ginH["categories"] = cache.CategoriesTags(h.C, constraints.Category)
h.ginH["recentComments"] = cache.RecentComments(h.C, 5)
}
var recentConf = map[any]any{
"number": int64(5),
"show_date": 0,
"title": "近期文章",
}
func (h *Handle) RecentPosts() {
set := reload.GetAnyValBy("recentPostsConfig", func() map[any]any {
return wpconfig.GetPHPArrayVal[map[any]any]("widget_recent-posts", recentConf, int64(2))
})
h.ginH["recentPostsConfig"] = set
h.ginH["recentPosts"] = slice.Map(cache.RecentPosts(h.C, int(set["number"].(int64))), ProjectTitle)
}
var archivesConfig = map[any]any{
"count": 0,
"dropdown": 0,
"title": "归档",
}
func (h *Handle) Archives() {
h.ginH["archivesConfig"] = reload.GetAnyValBy("archivesConfig", func() map[any]any {
return wpconfig.GetPHPArrayVal[map[any]any]("widget_archives", archivesConfig, int64(2))
})
h.ginH["archives"] = cache.Archives(h.C)
}

View File

@ -128,12 +128,9 @@ func (h *Handle) AddCacheComponent(name string, fn func(*Handle) string) {
}
func (h *Handle) CacheStr(name string, fn func(*Handle) string) string {
v, ok := reload.GetStr(name)
if !ok {
v = fn(h)
reload.SetStr(name, v)
}
return v
return reload.GetAnyValBy(name, func() string {
return fn(h)
})
}
func (h *Handle) PushHeadScript(fn ...Components) {

View File

@ -55,7 +55,7 @@ func GetLang() string {
return strings.Replace(s, "_", "-", 1)
}
func GetPHPArrayValWithDefaults[T any](optionName string, defaults T, key ...any) T {
func GetPHPArrayVal[T any](optionName string, defaults T, key ...any) T {
op, ok := phpArr.Load(optionName)
if ok {
return maps.GetAnyAnyValWithDefaults(op, defaults, key...)

View File

@ -49,21 +49,21 @@ func Group(group string) Condition {
}
}
func Order(order SqlBuilder) Condition {
func Order[T ~[][]string](order T) Condition {
return func(c *QueryCondition) {
c.Order = order
c.Order = SqlBuilder(order)
}
}
func Join(join SqlBuilder) Condition {
func Join[T ~[][]string](join T) Condition {
return func(c *QueryCondition) {
c.Join = join
c.Join = SqlBuilder(join)
}
}
func Having(having SqlBuilder) Condition {
func Having[T ~[][]string](having T) Condition {
return func(c *QueryCondition) {
c.Having = having
c.Having = SqlBuilder(having)
}
}