3a898e8aa0
后端修复: - 验证码存储与校验机制 - 订单创建事务+库存扣减 - GetStats字段名错误 - VerifyEmail改为POST - 供应商更新字段白名单 - 文件删除安全检查 - 抽奖安全随机数 - 用户管理CRUD - 订单取消/确认收货 - 工单回复 - Toggle返回新数据 - 移除死代码 前端修复: - 404兜底路由 - 401软跳转 - API层统一 - 面包屑补充banners - 国际化完善 - 购物车并行删除 - 退出清理购物车 - 供应商Dashboard数据 - 工单详情页 - 订单取消/确认收货
179 lines
4.6 KiB
Go
179 lines
4.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"sale/internal/crawler"
|
|
"sale/internal/models"
|
|
"sale/internal/schemas"
|
|
"sale/internal/utils"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ArticleHandler struct{}
|
|
|
|
func NewArticleHandler() *ArticleHandler {
|
|
return &ArticleHandler{}
|
|
}
|
|
|
|
func (h *ArticleHandler) List(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
var total int64
|
|
utils.DB.Model(&models.Article{}).Where("is_published = ?", true).Count(&total)
|
|
|
|
var articles []models.Article
|
|
offset := (page - 1) * pageSize
|
|
utils.DB.Where("is_published = ?", true).
|
|
Order("is_pinned DESC, sort_order ASC, created_at DESC").
|
|
Offset(offset).Limit(pageSize).Find(&articles)
|
|
|
|
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": articles,
|
|
"pagination": gin.H{
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
"total": total,
|
|
"total_pages": totalPages,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (h *ArticleHandler) GetByID(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var article models.Article
|
|
if err := utils.DB.Preload("Author").First(&article, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Article not found"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"data": article})
|
|
}
|
|
|
|
func (h *ArticleHandler) AdminList(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
|
|
var total int64
|
|
utils.DB.Model(&models.Article{}).Count(&total)
|
|
|
|
var articles []models.Article
|
|
offset := (page - 1) * pageSize
|
|
utils.DB.Preload("Author").
|
|
Order("is_pinned DESC, sort_order ASC, created_at DESC").
|
|
Offset(offset).Limit(pageSize).Find(&articles)
|
|
|
|
totalPages := int(math.Ceil(float64(total) / float64(pageSize)))
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"data": articles,
|
|
"pagination": gin.H{
|
|
"page": page,
|
|
"page_size": pageSize,
|
|
"total": total,
|
|
"total_pages": totalPages,
|
|
},
|
|
})
|
|
}
|
|
|
|
func (h *ArticleHandler) Create(c *gin.Context) {
|
|
userID := c.GetUint("user_id")
|
|
var req schemas.CreateArticleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
article := models.Article{
|
|
Title: req.Title,
|
|
Content: req.Content,
|
|
Summary: req.Summary,
|
|
CoverImage: req.CoverImage,
|
|
IsPinned: req.IsPinned,
|
|
IsPublished: req.IsPublished,
|
|
SortOrder: req.SortOrder,
|
|
AuthorID: &userID,
|
|
}
|
|
|
|
utils.DB.Create(&article)
|
|
c.JSON(http.StatusCreated, gin.H{"data": article})
|
|
}
|
|
|
|
func (h *ArticleHandler) Update(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var article models.Article
|
|
if err := utils.DB.First(&article, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Article not found"})
|
|
return
|
|
}
|
|
|
|
var req schemas.UpdateArticleRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
updates := make(map[string]interface{})
|
|
if req.Title != nil {
|
|
updates["title"] = *req.Title
|
|
}
|
|
if req.Content != nil {
|
|
updates["content"] = *req.Content
|
|
}
|
|
if req.Summary != nil {
|
|
updates["summary"] = *req.Summary
|
|
}
|
|
if req.CoverImage != nil {
|
|
updates["cover_image"] = *req.CoverImage
|
|
}
|
|
if req.IsPinned != nil {
|
|
updates["is_pinned"] = *req.IsPinned
|
|
}
|
|
if req.IsPublished != nil {
|
|
updates["is_published"] = *req.IsPublished
|
|
}
|
|
if req.SortOrder != nil {
|
|
updates["sort_order"] = *req.SortOrder
|
|
}
|
|
|
|
utils.DB.Model(&article).Updates(updates)
|
|
c.JSON(http.StatusOK, gin.H{"data": article})
|
|
}
|
|
|
|
func (h *ArticleHandler) Delete(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
if err := utils.DB.Delete(&models.Article{}, id).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete article"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "Article deleted successfully"})
|
|
}
|
|
|
|
func (h *ArticleHandler) TogglePin(c *gin.Context) {
|
|
id, _ := strconv.Atoi(c.Param("id"))
|
|
var article models.Article
|
|
if err := utils.DB.First(&article, id).Error; err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "Article not found"})
|
|
return
|
|
}
|
|
|
|
utils.DB.Model(&article).Update("is_pinned", !article.IsPinned)
|
|
utils.DB.First(&article, id)
|
|
c.JSON(http.StatusOK, gin.H{"data": article})
|
|
}
|
|
|
|
// CrawlRibenyan 采集 ribenyan.com 文章
|
|
func (h *ArticleHandler) CrawlRibenyan(c *gin.Context) {
|
|
crawler := crawler.NewRibenyanCrawler(utils.DB)
|
|
|
|
if err := crawler.CrawlArticles(); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "Crawl completed successfully"})
|
|
}
|