fix: 修复发帖后链接404 + 通知卡片跳转错误

- post_repo.go: FindByIDWithAuthor/Restore 中 posts.uid 改为 posts.id(posts 表主键是 id,不存在 uid 列)
- theme/loader.go: 新增 derefUint 模板函数,显式解引用 *uint 指针(Go 1.26 模板引擎传参给 printf 时不自动解引用)
- messages/index.html: 通知卡片链接使用 derefUint 包裹 RelatedID 避免打印指针地址
This commit is contained in:
2026-05-31 13:58:14 +08:00
parent 59af6b2635
commit e98bfe193b
3 changed files with 12 additions and 3 deletions

View File

@ -37,7 +37,7 @@ func (r *PostRepo) FindByIDWithAuthor(id uint) (*model.Post, error) {
err := r.db.Table("posts"). err := r.db.Table("posts").
Select("posts.*, users.username as author_name"). Select("posts.*, users.username as author_name").
Joins("LEFT JOIN users ON users.uid = posts.user_id"). Joins("LEFT JOIN users ON users.uid = posts.user_id").
Where("posts.uid = ?", id). Where("posts.id = ?", id).
First(&post).Error First(&post).Error
if err != nil { if err != nil {
return nil, err return nil, err
@ -157,7 +157,7 @@ func (r *PostRepo) CountPending() (int64, error) {
// Restore 恢复软删除 // Restore 恢复软删除
func (r *PostRepo) Restore(id uint) error { func (r *PostRepo) Restore(id uint) error {
result := r.db.Unscoped().Model(&model.Post{}).Where("uid = ?", id).Update("deleted_at", nil) result := r.db.Unscoped().Model(&model.Post{}).Where("id = ?", id).Update("deleted_at", nil)
if result.Error != nil { if result.Error != nil {
return result.Error return result.Error
} }

View File

@ -24,6 +24,15 @@ func LoadTemplates(roots ...TemplateRoot) (*template.Template, error) {
"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, "assetV": common.AssetV,
// derefUint 安全解引用 *uint 指针。
// Go 1.26 的模板引擎在将 *uint 传给 printf/print 时不会自动解引用,
// 导致打印指针地址而非值。通过此函数显式解引用后传给 printf。
"derefUint": func(p *uint) uint {
if p == nil {
return 0
}
return *p
},
} }
t := template.New("").Funcs(funcMap) t := template.New("").Funcs(funcMap)
for _, root := range roots { for _, root := range roots {

View File

@ -40,7 +40,7 @@
{{range $i, $m := .Messages}} {{range $i, $m := .Messages}}
{{$link := ""}} {{$link := ""}}
{{if or (eq $m.NotifyType "post_approved") (eq $m.NotifyType "post_rejected")}} {{if or (eq $m.NotifyType "post_approved") (eq $m.NotifyType "post_rejected")}}
{{$link = printf "/posts/%d" $m.RelatedID}} {{$link = printf "/posts/%d" (derefUint $m.RelatedID)}}
{{else if or (eq $m.NotifyType "audit_approved") (eq $m.NotifyType "audit_rejected")}} {{else if or (eq $m.NotifyType "audit_approved") (eq $m.NotifyType "audit_rejected")}}
{{$link = "/settings"}} {{$link = "/settings"}}
{{end}} {{end}}