优化router、main等文件。

This commit is contained in:
2026-08-14 17:59:20 +08:00
parent 0f0850357d
commit f1780de632
6 changed files with 303 additions and 180 deletions
+7 -1
View File
@@ -62,7 +62,7 @@ func loginInfo(c *gin.Context) (isLoggedIn bool, loginName string) {
return true, loginName
}
// 首页
// 首页数据结构
type IndexData struct {
Title string
SiteName string
@@ -84,6 +84,7 @@ type IndexData struct {
LoginName string
}
// 首页(供模板使用)
func IndexView(c *gin.Context) {
cfg := getConfig()
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
@@ -94,6 +95,7 @@ func IndexView(c *gin.Context) {
categoryID, _ := strconv.Atoi(c.Query("category"))
tagID, _ := strconv.Atoi(c.Query("tag"))
authorID, _ := strconv.Atoi(c.Query("author"))
db := database.DB.Model(&models.Post{}).Preload("Category").Preload("Tags").Preload("Author").
Where("status = ?", "published")
@@ -105,6 +107,9 @@ func IndexView(c *gin.Context) {
db = db.Joins("JOIN post_tags ON post_tags.post_id = posts.id").
Where("post_tags.tag_id = ?", tagID)
}
if authorID > 0 {
db = db.Where("author_id = ?", authorID)
}
var total int64
db.Count(&total)
@@ -169,6 +174,7 @@ type PostDetailData struct {
LoginName string
}
// 文章详情页(供模板使用)
func PostView(c *gin.Context) {
cfg := getConfig()
slug := c.Param("slug")