🎨 新增用户端文章接口&优化部分旧接口
This commit is contained in:
@@ -115,7 +115,7 @@ func (ArticleApi) AppById(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
userId := user_jwt.GetUserID(ctx)
|
userId := user_jwt.GetUserID(ctx)
|
||||||
article, err := articleService.APPGetArticle(id, userId)
|
article, err := articleService.APPGetArticle(id, int(userId))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
global.GVA_LOG.Error("查询失败!", zap.Error(err))
|
||||||
r.FailWithMessage("查询失败:"+err.Error(), ctx)
|
r.FailWithMessage("查询失败:"+err.Error(), ctx)
|
||||||
@@ -123,3 +123,45 @@ func (ArticleApi) AppById(ctx *gin.Context) {
|
|||||||
}
|
}
|
||||||
r.OkWithData(article, ctx)
|
r.OkWithData(article, ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppPush app端推送文章
|
||||||
|
func (ArticleApi) AppPush(ctx *gin.Context) {
|
||||||
|
var p article.Article
|
||||||
|
if err := ctx.ShouldBind(&p); err != nil {
|
||||||
|
r.FailWithMessage(err.Error(), ctx)
|
||||||
|
global.GVA_LOG.Error("参数有误!", zap.Error(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := articleService.AppPush(p)
|
||||||
|
if err != nil {
|
||||||
|
global.GVA_LOG.Error("推送失败!", zap.Error(err))
|
||||||
|
r.FailWithMessage("推送失败:"+err.Error(), ctx)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
r.OkWithMessage("推送成功", ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetMyArticleList 获取我的文章列表
|
||||||
|
func (ArticleApi) GetMyArticleList(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.TeacherId = int(userId) // 设置当前用户ID为教师ID
|
||||||
|
|
||||||
|
list, total, err := articleService.GetMyArticleList(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)
|
||||||
|
}
|
||||||
|
@@ -16,6 +16,7 @@ type Article struct {
|
|||||||
IsFree int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是
|
IsFree int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是
|
||||||
// 分类ID
|
// 分类ID
|
||||||
CategoryId int `json:"categoryId" gorm:"comment:分类ID"`
|
CategoryId int `json:"categoryId" gorm:"comment:分类ID"`
|
||||||
|
Status int `json:"status" gorm:"comment:状态;default:1"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过
|
||||||
}
|
}
|
||||||
|
|
||||||
// TableName 文章表
|
// TableName 文章表
|
||||||
|
@@ -8,6 +8,7 @@ type GetList struct {
|
|||||||
// 分类ID
|
// 分类ID
|
||||||
CategoryId int `json:"categoryId" form:"categoryId"` // 分类ID
|
CategoryId int `json:"categoryId" form:"categoryId"` // 分类ID
|
||||||
TeacherId int `json:"teacherId" form:"teacherId"` // 讲师ID
|
TeacherId int `json:"teacherId" form:"teacherId"` // 讲师ID
|
||||||
|
Status int `json:"status" form:"status"` // 状态 0-草稿 1-已发布 2-待审核 3-审核不通过
|
||||||
}
|
}
|
||||||
|
|
||||||
type DeleteIds struct {
|
type DeleteIds struct {
|
||||||
|
@@ -8,10 +8,11 @@ import (
|
|||||||
type ArticleRouter struct{}
|
type ArticleRouter struct{}
|
||||||
|
|
||||||
// InitBotRouter 初始化 文章 路由信息
|
// InitBotRouter 初始化 文章 路由信息
|
||||||
func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup) {
|
func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup, AppRouter *gin.RouterGroup) {
|
||||||
articleRouter := Router.Group("article").Use(middleware.OperationRecord())
|
articleRouter := Router.Group("article").Use(middleware.OperationRecord())
|
||||||
articleRouterWithoutRecord := Router.Group("article")
|
articleRouterWithoutRecord := Router.Group("article")
|
||||||
articleRouterWithoutAuth := PublicRouter.Group("article")
|
articleRouterWithoutAuth := PublicRouter.Group("article")
|
||||||
|
appRouter := AppRouter.Group("article")
|
||||||
{
|
{
|
||||||
articleRouter.POST("", artApi.Create) // 新建文章
|
articleRouter.POST("", artApi.Create) // 新建文章
|
||||||
articleRouter.DELETE("", artApi.Delete) // 批量删除文章
|
articleRouter.DELETE("", artApi.Delete) // 批量删除文章
|
||||||
@@ -27,4 +28,9 @@ func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin
|
|||||||
articleRouterWithoutAuth.GET("app/list", artApi.APPGetList) // 文章公开接口
|
articleRouterWithoutAuth.GET("app/list", artApi.APPGetList) // 文章公开接口
|
||||||
articleRouterWithoutAuth.GET("app/:id", artApi.AppById) // 文章开放接口
|
articleRouterWithoutAuth.GET("app/:id", artApi.AppById) // 文章开放接口
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
// App端文章相关接口
|
||||||
|
appRouter.POST("publish", artApi.AppPush) // 获取文章列表
|
||||||
|
appRouter.GET("my", artApi.GetMyArticleList) // 获取我的文章列表
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -75,7 +75,7 @@ func (s ArticleService) APPGetArticleList(pageInfo request.GetList) (list []vo.A
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s ArticleService) APPGetArticle(id string, userId uint) (article *vo.ArticleVo, err error) {
|
func (s ArticleService) APPGetArticle(id string, userId int) (article *vo.ArticleVo, err error) {
|
||||||
err = global.GVA_DB.Table("article").Where("id = ?", id).First(&article).Error
|
err = global.GVA_DB.Table("article").Where("id = ?", id).First(&article).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
global.GVA_LOG.Error("获取文章失败", zap.Error(err))
|
global.GVA_LOG.Error("获取文章失败", zap.Error(err))
|
||||||
@@ -94,7 +94,9 @@ func (s ArticleService) APPGetArticle(id string, userId uint) (article *vo.Artic
|
|||||||
global.GVA_LOG.Error("查询用户购买记录失败", zap.Error(err))
|
global.GVA_LOG.Error("查询用户购买记录失败", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if count == 0 {
|
|
||||||
|
// 如果count = 0 或者 TeacherId 不等于 userId,表示用户没有购买过该文章
|
||||||
|
if count == 0 && article.TeacherId != userId {
|
||||||
// 用户没有购买过,隐藏Content
|
// 用户没有购买过,隐藏Content
|
||||||
article.Content = ""
|
article.Content = ""
|
||||||
article.IsBuy = 0 // 设置为未购买
|
article.IsBuy = 0 // 设置为未购买
|
||||||
@@ -103,3 +105,39 @@ func (s ArticleService) APPGetArticle(id string, userId uint) (article *vo.Artic
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AppPush app端推送文章
|
||||||
|
func (s ArticleService) AppPush(req article.Article) (err error) {
|
||||||
|
err = global.GVA_DB.Create(&req).Error
|
||||||
|
if err != nil {
|
||||||
|
global.GVA_LOG.Error("推送文章失败", zap.Error(err))
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s ArticleService) GetMyArticleList(req request.GetList) (list []article.Article, total int64, err error) {
|
||||||
|
|
||||||
|
limit := req.PageSize
|
||||||
|
offset := req.PageSize * (req.Page - 1)
|
||||||
|
|
||||||
|
db := global.GVA_DB.Model(&article.Article{}).Where("teacher_id = ?", req.TeacherId)
|
||||||
|
if req.Title != "" {
|
||||||
|
db = db.Where("title LIKE ?", "%"+req.Title+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.CategoryId != 0 {
|
||||||
|
db = db.Where("category_id = ?", req.CategoryId)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Status != 0 {
|
||||||
|
db = db.Where("status = ?", req.Status)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Count(&total).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, 0, err
|
||||||
|
}
|
||||||
|
err = db.Limit(limit).Offset(offset).Find(&list).Error
|
||||||
|
return list, total, err
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user