fix sitemap

This commit is contained in:
aichy126 2023-06-09 15:02:39 +08:00
parent 3092954e68
commit cacf5e309f
2 changed files with 24 additions and 0 deletions

View File

@ -25,6 +25,11 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
log.Error("get site general failed:", err)
return
}
siteInfo, err := t.siteInfoService.GetSiteSeo(ctx)
if err != nil {
log.Error("get site GetSiteSeo failed:", err)
return
}
sitemapInfo := &schema.SiteMapList{}
infoStr, err := t.data.Cache.GetString(ctx, schema.SitemapCachekey)
@ -32,6 +37,10 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
log.Errorf("get Cache failed: %s", err)
return
}
hasTitle := false
if siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitle || siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitleByShortID {
hasTitle = true
}
if err = json.Unmarshal([]byte(infoStr), sitemapInfo); err != nil {
log.Errorf("get sitemap info failed: %s", err)
return
@ -45,6 +54,7 @@ func (t *TemplateRenderController) Sitemap(ctx *gin.Context) {
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"list": sitemapInfo.QuestionIDs,
"general": general,
"hastitle": hasTitle,
},
)
} else {
@ -68,6 +78,15 @@ func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error
log.Error("get site general failed:", err)
return err
}
siteInfo, err := t.siteInfoService.GetSiteSeo(ctx)
if err != nil {
log.Error("get site GetSiteSeo failed:", err)
return err
}
hasTitle := false
if siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitle || siteInfo.PermaLink == schema.PermaLinkQuestionIDAndTitleByShortID {
hasTitle = true
}
cachekey := fmt.Sprintf(schema.SitemapPageCachekey, page)
infoStr, err := t.data.Cache.GetString(ctx, cachekey)
@ -85,6 +104,7 @@ func (t *TemplateRenderController) SitemapPage(ctx *gin.Context, page int) error
"xmlHeader": template.HTML(`<?xml version="1.0" encoding="UTF-8"?>`),
"list": sitemapInfo.PageData,
"general": general,
"hastitle": hasTitle,
},
)
return nil

View File

@ -2,7 +2,11 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ range .list }}
<url>
{{if $.hastitle}}
<loc>{{$.general.SiteUrl}}/questions/{{.ID}}/{{.Title}}</loc>
{{else}}
<loc>{{$.general.SiteUrl}}/questions/{{.ID}}</loc>
{{end}}
<lastmod>{{.UpdateTime}}</lastmod>
</url>
{{ end }}