fix: 修复 Space 模板缺少 formatCount/str 函数导致的启动 panic

- 在 theme/loader.go FuncMap 中添加 formatCount 和 str 函数
- formatCount 将 int64 格式化为中文计数(如 2.5万)
- 控制器 ActiveFolderID 改为 uint 类型,避免模板中 str 类型比较
- 控制器传递 ActiveFolder 对象,移除模板内查找逻辑
- 清理旧模板替换残留的孤儿 HTML/SVG 代码
This commit is contained in:
2026-06-01 21:51:17 +08:00
parent d9eff2df08
commit c6da3932f3
3 changed files with 53 additions and 37 deletions

View File

@ -179,21 +179,25 @@ func (ctrl *SpaceController) loadCollectionsData(c *gin.Context, data gin.H, uid
data["FoldersResult"] = folders data["FoldersResult"] = folders
// 默认展示第一个收藏夹的内容 // 默认展示第一个收藏夹的内容
activeFolderID := c.DefaultQuery("folder", "") folderParam := c.DefaultQuery("folder", "")
if activeFolderID == "" && len(folders.Folders) > 0 { var activeFolderID uint
activeFolderID = strconv.FormatUint(uint64(folders.Folders[0].ID), 10) if folderParam != "" {
fid, parseErr := strconv.ParseUint(folderParam, 10, 64)
if parseErr == nil {
activeFolderID = uint(fid)
}
}
if activeFolderID == 0 && len(folders.Folders) > 0 {
activeFolderID = folders.Folders[0].ID
} }
var folderItems *service.FolderItemsResult var folderItems *service.FolderItemsResult
if activeFolderID != "" { if activeFolderID > 0 {
fid, parseErr := strconv.ParseUint(activeFolderID, 10, 64) items, itemErr := ctrl.favoriteSvc.ListFolderItems(uid, activeFolderID, page, pageSize)
if parseErr == nil { if itemErr != nil || items == nil {
items, itemErr := ctrl.favoriteSvc.ListFolderItems(uid, uint(fid), page, pageSize) folderItems = &service.FolderItemsResult{Items: []model.FolderItem{}, Total: 0}
if itemErr != nil || items == nil { } else {
folderItems = &service.FolderItemsResult{Items: []model.FolderItem{}, Total: 0} folderItems = items
} else {
folderItems = items
}
} }
} }
if folderItems == nil { if folderItems == nil {
@ -201,6 +205,17 @@ func (ctrl *SpaceController) loadCollectionsData(c *gin.Context, data gin.H, uid
} }
data["FolderItems"] = folderItems data["FolderItems"] = folderItems
data["ActiveFolderID"] = activeFolderID data["ActiveFolderID"] = activeFolderID
// 找到当前激活的收藏夹对象传给模板
var activeFolder *model.Folder
for i := range folders.Folders {
if folders.Folders[i].ID == activeFolderID {
activeFolder = &folders.Folders[i]
break
}
}
data["ActiveFolder"] = activeFolder
data["Page"] = page data["Page"] = page
data["TotalPages"] = folderItems.TotalPages data["TotalPages"] = folderItems.TotalPages
data["PrevPage"] = max(1, page-1) data["PrevPage"] = max(1, page-1)

View File

@ -22,8 +22,10 @@ func LoadTemplates(roots ...TemplateRoot) (*template.Template, error) {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"add": func(a, b int) int { return a + b }, "add": func(a, b int) int { return a + b },
"subtract": func(a, b int) int { return a - b }, "subtract": func(a, b int) int { return a - b },
"formatTTL": formatTTL, "formatTTL": formatTTL,
"assetV": common.AssetV, "formatCount": formatCount,
"str": str,
"assetV": common.AssetV,
"substr": func(s string, start, length int) string { "substr": func(s string, start, length int) string {
runes := []rune(s) runes := []rune(s)
if start >= len(runes) { if start >= len(runes) {
@ -100,3 +102,19 @@ func formatTTL(minutes int) string {
} }
return fmt.Sprintf("%d 天 %d 小时", days, remainHours) return fmt.Sprintf("%d 天 %d 小时", days, remainHours)
} }
// formatCount 将 int64 数字格式化为简洁中文计数(如 "2.5万"、"70.3万"、"0"
func formatCount(n int64) string {
if n < 10000 {
return fmt.Sprintf("%d", n)
}
if n < 100000000 {
v := float64(n) / 10000.0
return fmt.Sprintf("%.1f万", v)
}
v := float64(n) / 100000000.0
return fmt.Sprintf("%.1f亿", v)
}
// str 将 uint 转为字符串,用于模板中与字符串比较
func str(n uint) string { return fmt.Sprintf("%d", n) }

View File

@ -351,7 +351,7 @@
{{if .FoldersResult}} {{if .FoldersResult}}
{{range $f := .FoldersResult.Folders}} {{range $f := .FoldersResult.Folders}}
<a href="/space/{{$.SpaceUser.ID}}?tab=collections&folder={{$f.ID}}" <a href="/space/{{$.SpaceUser.ID}}?tab=collections&folder={{$f.ID}}"
class="space-sidebar-link{{if eq (str $.ActiveFolderID) (str $f.ID)}} active{{end}}" class="space-sidebar-link{{if eq $.ActiveFolderID $f.ID}} active{{end}}"
data-folder-id="{{$f.ID}}"> data-folder-id="{{$f.ID}}">
<svg class="space-sidebar-icon" viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M17 3H7c-1.1 0-2 .9-2 2v16l7-3 7 3V5c0-1.1-.9-2-2-2zm0 15l-5-2.18L7 18V5h10v13z"/></svg> <svg class="space-sidebar-icon" viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M17 3H7c-1.1 0-2 .9-2 2v16l7-3 7 3V5c0-1.1-.9-2-2-2zm0 15l-5-2.18L7 18V5h10v13z"/></svg>
{{$f.Name}} {{$f.Name}}
@ -366,32 +366,21 @@
<main class="space-main"> <main class="space-main">
{{if .FoldersResult}} {{if .FoldersResult}}
{{if gt (len .FoldersResult.Folders) 0}} {{if gt (len .FoldersResult.Folders) 0}}
{{/* 找到当前激活的收藏夹 */}} {{if .ActiveFolder}}
{{$activeFolder := ""}}
{{range $f := .FoldersResult.Folders}}
{{if eq (str $.ActiveFolderID) (str $f.ID)}}
{{$activeFolder = $f}}
{{end}}
{{end}}
{{if $activeFolder}}
<!-- 收藏夹头信息 --> <!-- 收藏夹头信息 -->
<div class="collection-header"> <div class="collection-header">
<div class="collection-cover"> <div class="collection-cover">
{{if $activeFolder.Description}}
<div class="collection-cover-placeholder"></div> <div class="collection-cover-placeholder"></div>
{{end}}
</div> </div>
<div class="collection-info-row"> <div class="collection-info-row">
<div> <div>
<h1 class="collection-name">{{$activeFolder.Name}}</h1> <h1 class="collection-name">{{.ActiveFolder.Name}}</h1>
<p class="collection-meta"> <p class="collection-meta">
{{if $activeFolder.IsPublic}}公开{{else}}私密{{end}} {{if .ActiveFolder.IsPublic}}公开{{else}}私密{{end}}
· 视频数 {{.FolderItems.Total}} · 文章数 {{.FolderItems.Total}}
</p> </p>
</div> </div>
<div class="collection-actions"> <div class="collection-actions">
<button class="space-action-btn primary small">播放全部</button>
<button class="space-action-btn outline small">批量操作</button> <button class="space-action-btn outline small">批量操作</button>
</div> </div>
</div> </div>
@ -406,14 +395,8 @@
</div> </div>
<div class="collection-filter"> <div class="collection-filter">
<select class="filter-select"> <select class="filter-select">
<option>当前</option> <option>全部</option>
</select> </select>
<div class="filter-search">
<input type="text" placeholder="请输入关键词" class="filter-search-input">
<button class="filter-search-btn">
<svg viewBox="0 0 24 24" width="16" height="16"><path fill="currentColor" d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>
</button>
</div>
</div> </div>
</div> </div>