增加ico文件
This commit is contained in:
@@ -225,6 +225,11 @@ func UpdatePost(c *gin.Context) {
|
||||
updates["published_at"] = &now
|
||||
}
|
||||
}
|
||||
// 如果文章是已发布状态但没有发布时间,设置发布时间
|
||||
if post.Status == "published" && post.PublishedAt == nil {
|
||||
now := time.Now()
|
||||
updates["published_at"] = &now
|
||||
}
|
||||
updates["is_top"] = req.IsTop
|
||||
|
||||
// 处理标签
|
||||
|
||||
+28
-18
@@ -15,7 +15,6 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 模板函数
|
||||
func templateFuncs() template.FuncMap {
|
||||
return template.FuncMap{
|
||||
"add": func(a, b int) int {
|
||||
@@ -30,12 +29,6 @@ func templateFuncs() template.FuncMap {
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化模板 - 返回 gin 可用的 HTMLRender
|
||||
func InitTemplates(theme string) (*template.Template, error) {
|
||||
tmpl, err := template.New("").Funcs(templateFuncs()).ParseGlob("templates/" + theme + "/*.html")
|
||||
return tmpl, err
|
||||
}
|
||||
|
||||
// 首页
|
||||
type IndexData struct {
|
||||
Title string
|
||||
@@ -61,8 +54,7 @@ func IndexView(c *gin.Context) {
|
||||
tagID, _ := strconv.Atoi(c.Query("tag"))
|
||||
|
||||
db := database.DB.Model(&models.Post{}).Preload("Category").Preload("Tags").Preload("Author").
|
||||
Where("status = ?", "published").
|
||||
Where("published_at IS NOT NULL")
|
||||
Where("status = ?", "published")
|
||||
|
||||
if categoryID > 0 {
|
||||
db = db.Where("category_id = ?", categoryID)
|
||||
@@ -98,7 +90,7 @@ func IndexView(c *gin.Context) {
|
||||
Year: time.Now().Year(),
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "base.html", data)
|
||||
c.HTML(http.StatusOK, "index", data)
|
||||
}
|
||||
|
||||
// 文章详情页
|
||||
@@ -112,6 +104,8 @@ type PostDetailData struct {
|
||||
Comments []models.Comment
|
||||
CommentCount int64
|
||||
Year int
|
||||
PrevPost *models.Post // 上一篇
|
||||
NextPost *models.Post // 下一篇
|
||||
}
|
||||
|
||||
func PostView(c *gin.Context) {
|
||||
@@ -129,7 +123,7 @@ func PostView(c *gin.Context) {
|
||||
First(&post).Error
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusNotFound, "base.html", gin.H{
|
||||
c.HTML(http.StatusNotFound, "index", gin.H{
|
||||
"Title": "404 - 页面不存在",
|
||||
"SiteName": cfg.App.Name,
|
||||
"Year": time.Now().Year(),
|
||||
@@ -148,6 +142,16 @@ func PostView(c *gin.Context) {
|
||||
var commentCount int64
|
||||
database.DB.Model(&models.Comment{}).Where("post_id = ? AND status = ?", post.ID, "approved").Count(&commentCount)
|
||||
|
||||
// 获取上一篇(发布时间更早的文章)
|
||||
var prevPost models.Post
|
||||
prevErr := database.DB.Where("status = ? AND published_at < ?", "published", post.PublishedAt).
|
||||
Order("published_at DESC").First(&prevPost).Error
|
||||
|
||||
// 获取下一篇(发布时间更晚的文章)
|
||||
var nextPost models.Post
|
||||
nextErr := database.DB.Where("status = ? AND published_at > ?", "published", post.PublishedAt).
|
||||
Order("published_at ASC").First(&nextPost).Error
|
||||
|
||||
data := PostDetailData{
|
||||
Title: post.Title,
|
||||
SiteName: cfg.App.Name,
|
||||
@@ -160,7 +164,15 @@ func PostView(c *gin.Context) {
|
||||
Year: time.Now().Year(),
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "base.html", data)
|
||||
// 只有当查询成功时才赋值
|
||||
if prevErr == nil {
|
||||
data.PrevPost = &prevPost
|
||||
}
|
||||
if nextErr == nil {
|
||||
data.NextPost = &nextPost
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "post", data)
|
||||
}
|
||||
|
||||
// 独立页面
|
||||
@@ -180,9 +192,8 @@ func PageView(c *gin.Context) {
|
||||
|
||||
var page models.Page
|
||||
err := database.DB.Where("slug = ? AND status = ?", slug, "published").First(&page).Error
|
||||
|
||||
if err != nil {
|
||||
c.HTML(http.StatusNotFound, "base.html", gin.H{
|
||||
c.HTML(http.StatusNotFound, "index", gin.H{
|
||||
"Title": "404 - 页面不存在",
|
||||
"SiteName": cfg.App.Name,
|
||||
"Year": time.Now().Year(),
|
||||
@@ -204,7 +215,7 @@ func PageView(c *gin.Context) {
|
||||
Year: time.Now().Year(),
|
||||
}
|
||||
|
||||
c.HTML(http.StatusOK, "base.html", data)
|
||||
c.HTML(http.StatusOK, "page", data)
|
||||
}
|
||||
|
||||
// 提交评论(表单提交)
|
||||
@@ -228,14 +239,13 @@ func SubmitComment(c *gin.Context) {
|
||||
Email: email,
|
||||
Website: website,
|
||||
Content: content,
|
||||
IP: c.ClientIP(),
|
||||
Status: "pending",
|
||||
}
|
||||
|
||||
if err := database.DB.Create(&comment).Error; err != nil {
|
||||
c.String(http.StatusInternalServerError, "发表评论失败")
|
||||
c.String(http.StatusInternalServerError, "提交失败")
|
||||
return
|
||||
}
|
||||
|
||||
c.Redirect(http.StatusFound, "/post/"+post.Slug+"#comment-"+strconv.Itoa(int(comment.ID)))
|
||||
c.Redirect(http.StatusFound, "/post/"+post.Slug)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user