diff --git a/api/v1/article/article.go b/api/v1/article/article.go index c724594..de0ecb7 100644 --- a/api/v1/article/article.go +++ b/api/v1/article/article.go @@ -165,3 +165,28 @@ func (ArticleApi) GetMyArticleList(ctx *gin.Context) { } r.OkWithDetailed(gin.H{"list": list, "total": total}, "查询成功", ctx) } + +// GetBuyList 获取用户购买的文章列表 +func (ArticleApi) GetBuyList(ctx *gin.Context) { + var p request.GetList + if err := ctx.ShouldBind(&p); err != nil { + r.FailWithMessage(err.Error(), ctx) + global.GVA_LOG.Error("参数有误!", zap.Error(err)) + return + } + + userId := user_jwt.GetUserID(ctx) + if userId == 0 { + r.FailWithMessage("用户未登录", ctx) + return + } + p.UserId = userId // 设置当前用户ID + + list, total, err := articleService.GetBuyList(p) + if err != nil { + global.GVA_LOG.Error("查询失败!", zap.Error(err)) + r.FailWithMessage("查询失败:"+err.Error(), ctx) + return + } + r.OkWithDetailed(gin.H{"list": list, "total": total}, "查询成功", ctx) +} diff --git a/model/article/request/article.go b/model/article/request/article.go index ca391ec..1d2c7b6 100644 --- a/model/article/request/article.go +++ b/model/article/request/article.go @@ -6,9 +6,10 @@ type GetList struct { request.PageInfo Title string `json:"title" form:"title"` // 文章标题 // 分类ID - CategoryId int `json:"categoryId" form:"categoryId"` // 分类ID - TeacherId int `json:"teacherId" form:"teacherId"` // 讲师ID - Status int `json:"status" form:"status"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过 + CategoryId int `json:"categoryId" form:"categoryId"` // 分类ID + TeacherId int `json:"teacherId" form:"teacherId"` // 讲师ID + Status int `json:"status" form:"status"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过 + UserId uint `json:"userId" form:"userId"` // 用户ID } type DeleteIds struct { diff --git a/router/article/article.go b/router/article/article.go index 021c7e0..de2c02f 100644 --- a/router/article/article.go +++ b/router/article/article.go @@ -32,5 +32,6 @@ func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin // App端文章相关接口 appRouter.POST("publish", artApi.AppPush) // 获取文章列表 appRouter.GET("my", artApi.GetMyArticleList) // 获取我的文章列表 + appRouter.GET("buyList", artApi.GetBuyList) // 获取购买的文章列表 } } diff --git a/service/article/article.go b/service/article/article.go index e29dc94..daebe49 100644 --- a/service/article/article.go +++ b/service/article/article.go @@ -141,3 +141,36 @@ func (s ArticleService) GetMyArticleList(req request.GetList) (list []article.Ar err = db.Limit(limit).Offset(offset).Find(&list).Error return list, total, err } + +func (s ArticleService) GetBuyList(p request.GetList) (articles []article.Article, total int64, err error) { + buyIds := make([]int, 0) + // 获取用户购买的文章ID列表 + err = global.GVA_DB.Model(&app.Order{}).Where("user_id = ? AND status = 2", p.UserId).Select("article_id").Find(&buyIds).Error + if err != nil { + global.GVA_LOG.Error("查询用户购买记录失败", zap.Error(err)) + return nil, 0, err + } + + if len(buyIds) == 0 { + // 如果没有购买记录,直接返回空列表 + return []article.Article{}, 0, nil + } + + db := global.GVA_DB.Model(&article.Article{}).Where("id IN ?", buyIds) + if p.Title != "" { + db = db.Where("title LIKE ?", "%"+p.Title+"%") + } + + err = db.Count(&total).Error + if err != nil { + global.GVA_LOG.Error("查询购买文章总数失败", zap.Error(err)) + return nil, 0, err + } + + err = db.Limit(p.PageSize).Offset(p.PageSize * (p.Page - 1)).Find(&articles).Error + if err != nil { + global.GVA_LOG.Error("查询购买文章列表失败", zap.Error(err)) + return nil, 0, err + } + return articles, total, nil +}