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:
@ -179,28 +179,43 @@ func (ctrl *SpaceController) loadCollectionsData(c *gin.Context, data gin.H, uid
|
||||
data["FoldersResult"] = folders
|
||||
|
||||
// 默认展示第一个收藏夹的内容
|
||||
activeFolderID := c.DefaultQuery("folder", "")
|
||||
if activeFolderID == "" && len(folders.Folders) > 0 {
|
||||
activeFolderID = strconv.FormatUint(uint64(folders.Folders[0].ID), 10)
|
||||
folderParam := c.DefaultQuery("folder", "")
|
||||
var activeFolderID uint
|
||||
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
|
||||
if activeFolderID != "" {
|
||||
fid, parseErr := strconv.ParseUint(activeFolderID, 10, 64)
|
||||
if parseErr == nil {
|
||||
items, itemErr := ctrl.favoriteSvc.ListFolderItems(uid, uint(fid), page, pageSize)
|
||||
if activeFolderID > 0 {
|
||||
items, itemErr := ctrl.favoriteSvc.ListFolderItems(uid, activeFolderID, page, pageSize)
|
||||
if itemErr != nil || items == nil {
|
||||
folderItems = &service.FolderItemsResult{Items: []model.FolderItem{}, Total: 0}
|
||||
} else {
|
||||
folderItems = items
|
||||
}
|
||||
}
|
||||
}
|
||||
if folderItems == nil {
|
||||
folderItems = &service.FolderItemsResult{Items: []model.FolderItem{}, Total: 0}
|
||||
}
|
||||
data["FolderItems"] = folderItems
|
||||
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["TotalPages"] = folderItems.TotalPages
|
||||
data["PrevPage"] = max(1, page-1)
|
||||
|
||||
@ -23,6 +23,8 @@ func LoadTemplates(roots ...TemplateRoot) (*template.Template, error) {
|
||||
"add": func(a, b int) int { return a + b },
|
||||
"subtract": func(a, b int) int { return a - b },
|
||||
"formatTTL": formatTTL,
|
||||
"formatCount": formatCount,
|
||||
"str": str,
|
||||
"assetV": common.AssetV,
|
||||
"substr": func(s string, start, length int) string {
|
||||
runes := []rune(s)
|
||||
@ -100,3 +102,19 @@ func formatTTL(minutes int) string {
|
||||
}
|
||||
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) }
|
||||
|
||||
@ -351,7 +351,7 @@
|
||||
{{if .FoldersResult}}
|
||||
{{range $f := .FoldersResult.Folders}}
|
||||
<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}}">
|
||||
<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}}
|
||||
@ -366,32 +366,21 @@
|
||||
<main class="space-main">
|
||||
{{if .FoldersResult}}
|
||||
{{if gt (len .FoldersResult.Folders) 0}}
|
||||
{{/* 找到当前激活的收藏夹 */}}
|
||||
{{$activeFolder := ""}}
|
||||
{{range $f := .FoldersResult.Folders}}
|
||||
{{if eq (str $.ActiveFolderID) (str $f.ID)}}
|
||||
{{$activeFolder = $f}}
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
{{if $activeFolder}}
|
||||
{{if .ActiveFolder}}
|
||||
<!-- 收藏夹头信息 -->
|
||||
<div class="collection-header">
|
||||
<div class="collection-cover">
|
||||
{{if $activeFolder.Description}}
|
||||
<div class="collection-cover-placeholder"></div>
|
||||
{{end}}
|
||||
</div>
|
||||
<div class="collection-info-row">
|
||||
<div>
|
||||
<h1 class="collection-name">{{$activeFolder.Name}}</h1>
|
||||
<h1 class="collection-name">{{.ActiveFolder.Name}}</h1>
|
||||
<p class="collection-meta">
|
||||
{{if $activeFolder.IsPublic}}公开{{else}}私密{{end}}
|
||||
· 视频数 {{.FolderItems.Total}}
|
||||
{{if .ActiveFolder.IsPublic}}公开{{else}}私密{{end}}
|
||||
· 文章数 {{.FolderItems.Total}}
|
||||
</p>
|
||||
</div>
|
||||
<div class="collection-actions">
|
||||
<button class="space-action-btn primary small">播放全部</button>
|
||||
<button class="space-action-btn outline small">批量操作</button>
|
||||
</div>
|
||||
</div>
|
||||
@ -406,14 +395,8 @@
|
||||
</div>
|
||||
<div class="collection-filter">
|
||||
<select class="filter-select">
|
||||
<option>当前</option>
|
||||
<option>全部</option>
|
||||
</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>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user