ai优化,删除未使用代码,提取重复代码。
This commit is contained in:
+40
-91
@@ -1,7 +1,6 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
@@ -15,6 +14,34 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// getConfig 获取全局配置(启动时已初始化,直接返回)
|
||||
func getConfig() *config.Config {
|
||||
if cfg := config.GetGlobalConfig(); cfg != nil {
|
||||
return cfg
|
||||
}
|
||||
return config.Load()
|
||||
}
|
||||
|
||||
// loadSiteInfo 加载站点名称和描述(优先数据库,回退配置文件)
|
||||
func loadSiteInfo(cfg *config.Config) (siteName, siteDesc string) {
|
||||
siteName = cfg.App.Name
|
||||
siteDesc = cfg.App.Description
|
||||
if v, err := models.GetOptionValue(database.DB, OptionSiteName, 0); err == nil && v != "" {
|
||||
siteName = v
|
||||
}
|
||||
if v, err := models.GetOptionValue(database.DB, OptionSiteDesc, 0); err == nil && v != "" {
|
||||
siteDesc = v
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// loadPublishedPages 加载所有已发布页面(按排序)
|
||||
func loadPublishedPages() []models.Page {
|
||||
var pages []models.Page
|
||||
database.DB.Where("status = ?", "published").Order("`order` ASC").Find(&pages)
|
||||
return pages
|
||||
}
|
||||
|
||||
// loadSidebarData 加载侧边栏数据
|
||||
func loadSidebarData() (categories []models.Category, tags []models.Tag, postCount, categoryCount, tagCount int64) {
|
||||
database.DB.Order("name ASC").Find(&categories)
|
||||
@@ -58,10 +85,7 @@ type IndexData struct {
|
||||
}
|
||||
|
||||
func IndexView(c *gin.Context) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
if cfg == nil {
|
||||
cfg = config.Load()
|
||||
}
|
||||
cfg := getConfig()
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
@@ -91,39 +115,15 @@ func IndexView(c *gin.Context) {
|
||||
var total int64
|
||||
db.Count(&total)
|
||||
|
||||
// 调试日志
|
||||
slog.Info("首页查询", "total", total, "page", page, "categoryID", categoryID, "tagID", tagID)
|
||||
|
||||
var posts []models.Post
|
||||
offset := (page - 1) * pageSize
|
||||
db.Order("is_top DESC, published_at DESC, created_at DESC").
|
||||
Offset(offset).Limit(pageSize).Find(&posts)
|
||||
|
||||
// 调试日志
|
||||
slog.Info("查询结果", "postCount", len(posts))
|
||||
for i, p := range posts {
|
||||
slog.Info("文章", "index", i, "id", p.ID, "title", p.Title, "status", p.Status, "publishedAt", p.PublishedAt, "views", p.Views, "commentsCount", len(p.Comments))
|
||||
}
|
||||
|
||||
// 获取页面
|
||||
var pages []models.Page
|
||||
database.DB.Where("status = ?", "published").Order("`order` ASC").Find(&pages)
|
||||
|
||||
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
||||
|
||||
// 加载侧边栏数据
|
||||
categories, tags, postCount, categoryCount, tagCount := loadSidebarData()
|
||||
|
||||
// 从数据库读取主题设置,如果不存在则使用配置文件中的值
|
||||
siteName := cfg.App.Name
|
||||
siteDesc := cfg.App.Description
|
||||
|
||||
if v, err := models.GetOptionValue(database.DB, "site_name", 0); err == nil {
|
||||
siteName = v
|
||||
}
|
||||
if v, err := models.GetOptionValue(database.DB, "site_desc", 0); err == nil {
|
||||
siteDesc = v
|
||||
}
|
||||
siteName, siteDesc := loadSiteInfo(cfg)
|
||||
pages := loadPublishedPages()
|
||||
|
||||
data := IndexData{
|
||||
Title: "首页",
|
||||
@@ -176,10 +176,7 @@ type PostDetailData struct {
|
||||
}
|
||||
|
||||
func PostView(c *gin.Context) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
if cfg == nil {
|
||||
cfg = config.Load()
|
||||
}
|
||||
cfg := getConfig()
|
||||
slug := c.Param("slug")
|
||||
|
||||
var post models.Post
|
||||
@@ -207,9 +204,7 @@ func PostView(c *gin.Context) {
|
||||
// 增加浏览量
|
||||
database.DB.Model(&post).UpdateColumn("views", gorm.Expr("views + 1"))
|
||||
|
||||
// 获取页面
|
||||
var pages []models.Page
|
||||
database.DB.Where("status = ?", "published").Order("`order` ASC").Find(&pages)
|
||||
pages := loadPublishedPages()
|
||||
|
||||
// 统计评论数
|
||||
var commentCount int64
|
||||
@@ -262,18 +257,7 @@ func PostView(c *gin.Context) {
|
||||
nextErr := database.DB.Where("status = ? AND published_at > ?", "published", post.PublishedAt).
|
||||
Order("published_at ASC").First(&nextPost).Error
|
||||
|
||||
// 从数据库读取站点设置
|
||||
siteName := cfg.App.Name
|
||||
siteDesc := cfg.App.Description
|
||||
|
||||
if v, err := models.GetOptionValue(database.DB, "site_name", 0); err == nil {
|
||||
siteName = v
|
||||
}
|
||||
if v, err := models.GetOptionValue(database.DB, "site_desc", 0); err == nil {
|
||||
siteDesc = v
|
||||
}
|
||||
|
||||
// 加载侧边栏数据
|
||||
siteName, siteDesc := loadSiteInfo(cfg)
|
||||
categories, tags, postCount, categoryCount, tagCount := loadSidebarData()
|
||||
|
||||
data := PostDetailData{
|
||||
@@ -331,10 +315,7 @@ type PageDetailData struct {
|
||||
}
|
||||
|
||||
func PageView(c *gin.Context) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
if cfg == nil {
|
||||
cfg = config.Load()
|
||||
}
|
||||
cfg := getConfig()
|
||||
slug := c.Param("slug")
|
||||
|
||||
var page models.Page
|
||||
@@ -348,21 +329,8 @@ func PageView(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 获取所有页面
|
||||
var pages []models.Page
|
||||
database.DB.Where("status = ?", "published").Order("`order` ASC").Find(&pages)
|
||||
|
||||
// 从数据库读取站点设置
|
||||
siteName := cfg.App.Name
|
||||
siteDesc := cfg.App.Description
|
||||
|
||||
if v, err := models.GetOptionValue(database.DB, "site_name", 0); err == nil {
|
||||
siteName = v
|
||||
}
|
||||
if v, err := models.GetOptionValue(database.DB, "site_desc", 0); err == nil {
|
||||
siteDesc = v
|
||||
}
|
||||
// 加载侧边栏数据
|
||||
pages := loadPublishedPages()
|
||||
siteName, siteDesc := loadSiteInfo(cfg)
|
||||
categories, tags, postCount, categoryCount, tagCount := loadSidebarData()
|
||||
|
||||
data := PageDetailData{
|
||||
@@ -419,10 +387,7 @@ func SubmitComment(c *gin.Context) {
|
||||
|
||||
// 搜索结果页
|
||||
func SearchView(c *gin.Context) {
|
||||
cfg := config.GetGlobalConfig()
|
||||
if cfg == nil {
|
||||
cfg = config.Load()
|
||||
}
|
||||
cfg := getConfig()
|
||||
|
||||
keyword := c.Query("keyword")
|
||||
if keyword == "" {
|
||||
@@ -449,26 +414,10 @@ func SearchView(c *gin.Context) {
|
||||
db.Order("published_at DESC").
|
||||
Offset(offset).Limit(pageSize).Find(&posts)
|
||||
|
||||
// 获取页面
|
||||
var pages []models.Page
|
||||
database.DB.Where("status = ?", "published").Order("`order` ASC").Find(&pages)
|
||||
|
||||
pages := loadPublishedPages()
|
||||
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
||||
|
||||
// 加载侧边栏数据
|
||||
categories, tags, postCount, categoryCount, tagCount := loadSidebarData()
|
||||
|
||||
// 从数据库读取主题设置
|
||||
siteName := cfg.App.Name
|
||||
siteDesc := cfg.App.Description
|
||||
|
||||
var option models.Option
|
||||
if err := database.DB.Where("name = ? AND user_id = ?", "site_name", 0).First(&option).Error; err == nil {
|
||||
siteName = option.Value
|
||||
}
|
||||
if err := database.DB.Where("name = ? AND user_id = ?", "site_desc", 0).First(&option).Error; err == nil {
|
||||
siteDesc = option.Value
|
||||
}
|
||||
siteName, siteDesc := loadSiteInfo(cfg)
|
||||
|
||||
data := IndexData{
|
||||
Title: "搜索: " + keyword,
|
||||
|
||||
Reference in New Issue
Block a user