优化一些逻辑。

This commit is contained in:
2026-08-06 17:45:06 +08:00
parent ea293f7866
commit ec1a19f469
5 changed files with 13 additions and 26 deletions
+4 -1
View File
@@ -264,7 +264,10 @@ func UpdatePost(c *gin.Context) {
})
tags = append(tags, tag)
}
database.DB.Model(&post).Association("Tags").Replace(tags)
if err := database.DB.Model(&post).Association("Tags").Replace(tags); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新文章标签失败"})
return
}
}
if err := database.DB.Model(&post).Updates(updates).Error; err != nil {
+2 -2
View File
@@ -147,7 +147,7 @@ func CreateTheme(c *gin.Context) {
}
// 创建主题目录
if err := os.MkdirAll(themePath, 0755); err != nil {
if err := os.MkdirAll(themePath, 0700); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "创建主题目录失败",
})
@@ -411,7 +411,7 @@ func SaveThemeFile(c *gin.Context) {
}
// 保存文件
if err := os.WriteFile(filePath, []byte(req.Content), 0644); err != nil {
if err := os.WriteFile(filePath, []byte(req.Content), 0600); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"error": "保存文件失败",
})
+1 -18
View File
@@ -1,7 +1,6 @@
package handlers
import (
"html/template"
"log/slog"
"math"
"net/http"
@@ -11,7 +10,6 @@ import (
"goblog/config"
"goblog/database"
"goblog/models"
"goblog/utils"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
@@ -27,21 +25,6 @@ func loadSidebarData() (categories []models.Category, tags []models.Tag, postCou
return
}
func templateFuncs() template.FuncMap {
return template.FuncMap{
"add": func(a, b int) int {
return a + b
},
"sub": func(a, b int) int {
return a - b
},
"html": func(s string) template.HTML {
return template.HTML(s)
},
"markdown": utils.RenderMarkdown,
}
}
// 首页
type IndexData struct {
Title string
@@ -79,7 +62,7 @@ func IndexView(c *gin.Context) {
Where("status = ?", "published")
userId, exists := c.Get("userID")
userId, exists = 1, true
if exists {
db = db.Or("author_id = ?", userId)
}
+1 -1
View File
@@ -21,7 +21,7 @@ func JWTAuth() gin.HandlerFunc {
}
parts := strings.SplitN(authHeader, " ", 2)
if !(len(parts) == 2 && parts[0] == "Bearer") {
if len(parts) != 2 || parts[0] != "Bearer" {
c.JSON(http.StatusUnauthorized, gin.H{"error": "认证格式错误"})
c.Abort()
return
+5 -4
View File
@@ -118,10 +118,11 @@ func (r *TemplateRenderer) reload(theme string) {
for _, pageName := range pages {
// 加载 base.html、对应页面模板以及所有公共局部模板
allFiles := []string{
templateDir + "/base.html",
templateDir + "/" + pageName + ".html",
}
allFiles := make([]string, 0, 2+len(partials))
allFiles = append(allFiles,
templateDir+"/base.html",
templateDir+"/"+pageName+".html",
)
allFiles = append(allFiles, partials...)
// 解析模板文件