handlers区分backend和frontend,需要确认。
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
@@ -21,7 +21,7 @@ type AdminPageData struct {
|
||||
Menus []AdminMenuGroup // 当前角色可见菜单(登录接口同源)
|
||||
PageSet map[string]bool // 菜单中的页面标识集合,模板据此渲染对应页面框架
|
||||
DisplayName string // 侧边栏显示的用户名
|
||||
CurrentUser template.JS // JSON 序列化的当前用户信息(id/username/nickname/role),注入前端
|
||||
CurrentUser template.JS // JSON 序列化的当前用户信息(id/username/nickname/role),注入前端
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -0,0 +1,43 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"goblog/database"
|
||||
"goblog/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GetDashboardStats 返回仪表盘统计数字(按角色过滤:管理员统计全部,普通用户统计自己的)
|
||||
func GetDashboardStats(c *gin.Context) {
|
||||
// 文章数:管理员统计全部,普通用户统计自己的(与文章管理页可见范围一致)
|
||||
postQuery := database.DB.Model(&models.Post{})
|
||||
if !isAdmin(c) {
|
||||
userID, _ := c.Get("userID")
|
||||
postQuery = postQuery.Where("author_id = ?", userID)
|
||||
}
|
||||
var postCount int64
|
||||
postQuery.Count(&postCount)
|
||||
|
||||
var categoryCount, tagCount int64
|
||||
database.DB.Model(&models.Category{}).Count(&categoryCount)
|
||||
database.DB.Model(&models.Tag{}).Count(&tagCount)
|
||||
|
||||
// 待审核评论数:管理员统计全部,普通用户统计自己文章上的
|
||||
commentQuery := database.DB.Model(&models.Comment{}).Where("status = ?", "pending")
|
||||
if !isAdmin(c) {
|
||||
userID, _ := c.Get("userID")
|
||||
commentQuery = commentQuery.Where("post_id IN (?)",
|
||||
database.DB.Model(&models.Post{}).Select("id").Where("author_id = ?", userID))
|
||||
}
|
||||
var pendingCommentCount int64
|
||||
commentQuery.Count(&pendingCommentCount)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"posts": postCount,
|
||||
"categories": categoryCount,
|
||||
"tags": tagCount,
|
||||
"pending_comments": pendingCommentCount,
|
||||
})
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,26 +1,18 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"goblog/config"
|
||||
"goblog/database"
|
||||
"goblog/models"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// 站点基础设置键名
|
||||
const (
|
||||
OptionSiteName = "site_name"
|
||||
OptionSiteDesc = "site_desc"
|
||||
OptionShowComments = "show_comments" // "1" 显示评论 / "0" 隐藏评论
|
||||
OptionCommentPageSize = "comments_per_page" // 前台评论每页条数
|
||||
)
|
||||
|
||||
// GetSettings 获取站点基础设置(管理员)
|
||||
func GetSettings(c *gin.Context) {
|
||||
cfg := getConfig()
|
||||
cfg := config.GetConfig()
|
||||
|
||||
opts, err := models.GetOptionsByUser(database.DB, 0)
|
||||
if err != nil {
|
||||
@@ -30,10 +22,10 @@ func GetSettings(c *gin.Context) {
|
||||
|
||||
// 默认值:站点信息取配置文件,评论默认显示、每页 10 条
|
||||
settings := map[string]string{
|
||||
OptionSiteName: cfg.App.Name,
|
||||
OptionSiteDesc: cfg.App.Description,
|
||||
OptionShowComments: "1",
|
||||
OptionCommentPageSize: "10",
|
||||
models.OptionSiteName: cfg.App.Name,
|
||||
models.OptionSiteDesc: cfg.App.Description,
|
||||
models.OptionShowComments: "1",
|
||||
models.OptionCommentPageSize: "10",
|
||||
}
|
||||
for name := range settings {
|
||||
if v, ok := opts[name]; ok && v != "" {
|
||||
@@ -65,10 +57,10 @@ func UpdateSettings(c *gin.Context) {
|
||||
showComments = "1"
|
||||
}
|
||||
options := map[string]string{
|
||||
OptionSiteName: req.SiteName,
|
||||
OptionSiteDesc: req.SiteDesc,
|
||||
OptionShowComments: showComments,
|
||||
OptionCommentPageSize: strconv.Itoa(req.CommentsPerPage),
|
||||
models.OptionSiteName: req.SiteName,
|
||||
models.OptionSiteDesc: req.SiteDesc,
|
||||
models.OptionShowComments: showComments,
|
||||
models.OptionCommentPageSize: strconv.Itoa(req.CommentsPerPage),
|
||||
}
|
||||
|
||||
if err := models.BatchSetOptions(database.DB, 0, options); err != nil {
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package backend
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
@@ -1,4 +1,4 @@
|
||||
package handlers
|
||||
package frontend
|
||||
|
||||
import (
|
||||
"math"
|
||||
@@ -26,10 +26,10 @@ func getConfig() *config.Config {
|
||||
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 != "" {
|
||||
if v, err := models.GetOptionValue(database.DB, models.OptionSiteName, 0); err == nil && v != "" {
|
||||
siteName = v
|
||||
}
|
||||
if v, err := models.GetOptionValue(database.DB, OptionSiteDesc, 0); err == nil && v != "" {
|
||||
if v, err := models.GetOptionValue(database.DB, models.OptionSiteDesc, 0); err == nil && v != "" {
|
||||
siteDesc = v
|
||||
}
|
||||
return
|
||||
@@ -98,12 +98,6 @@ func IndexView(c *gin.Context) {
|
||||
db := database.DB.Model(&models.Post{}).Preload("Category").Preload("Tags").Preload("Author").
|
||||
Where("status = ?", "published")
|
||||
|
||||
userId, exists := c.Get("userID")
|
||||
|
||||
if exists {
|
||||
db = db.Or("author_id = ?", userId)
|
||||
}
|
||||
|
||||
if categoryID > 0 {
|
||||
db = db.Where("category_id = ?", categoryID)
|
||||
}
|
||||
@@ -212,11 +206,11 @@ func PostView(c *gin.Context) {
|
||||
|
||||
// 读取评论展示设置
|
||||
showComments := true
|
||||
if v, optErr := models.GetOptionValue(database.DB, OptionShowComments, 0); optErr == nil {
|
||||
if v, optErr := models.GetOptionValue(database.DB, models.OptionShowComments, 0); optErr == nil {
|
||||
showComments = v != "0"
|
||||
}
|
||||
commentPageSize := 10
|
||||
if v, optErr := models.GetOptionValue(database.DB, OptionCommentPageSize, 0); optErr == nil {
|
||||
if v, optErr := models.GetOptionValue(database.DB, models.OptionCommentPageSize, 0); optErr == nil {
|
||||
if n, convErr := strconv.Atoi(v); convErr == nil && n > 0 {
|
||||
commentPageSize = n
|
||||
}
|
||||
Reference in New Issue
Block a user