diff --git a/api/v1/article/article.go b/api/v1/article/article.go index 529f851..a1623bf 100644 --- a/api/v1/article/article.go +++ b/api/v1/article/article.go @@ -5,6 +5,7 @@ import ( "git.echol.cn/loser/lckt/model/article" "git.echol.cn/loser/lckt/model/article/request" r "git.echol.cn/loser/lckt/model/common/response" + "git.echol.cn/loser/lckt/utils/user_jwt" "github.com/gin-gonic/gin" "go.uber.org/zap" ) @@ -88,3 +89,37 @@ func (ArticleApi) ById(ctx *gin.Context) { } r.OkWithData(article, ctx) } + +func (ArticleApi) APPGetList(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 + } + list, total, err := articleService.APPGetArticleList(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) +} + +// AppById 根据ID获取文章 +func (ArticleApi) AppById(ctx *gin.Context) { + id := ctx.Param("id") + if id == "" { + r.FailWithMessage("参数错误", ctx) + return + } + + userId := user_jwt.GetUserID(ctx) + article, err := articleService.APPGetArticle(id, userId) + if err != nil { + global.GVA_LOG.Error("查询失败!", zap.Error(err)) + r.FailWithMessage("查询失败:"+err.Error(), ctx) + return + } + r.OkWithData(article, ctx) +} diff --git a/model/article/article.go b/model/article/article.go index 6d9a0ea..c11a16f 100644 --- a/model/article/article.go +++ b/model/article/article.go @@ -11,8 +11,11 @@ type Article struct { Content string `json:"content" gorm:"comment:文章内容;type:longtext"` CoverImg string `json:"coverImg" gorm:"comment:文章封面图"` TeacherId int `json:"teacherId" gorm:"comment:讲师ID"` - Price int64 `json:"price" gorm:"comment:文章价格(单位为分)"` TeacherName string `json:"teacherName" gorm:"comment:讲师名称"` + Price int64 `json:"price" gorm:"comment:文章价格(单位为分)"` + IsFree int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是 + // 分类ID + CategoryId int `json:"categoryId" gorm:"comment:分类ID"` } // TableName 文章表 diff --git a/model/article/request/article.go b/model/article/request/article.go index 4ba84b8..fb3a6c4 100644 --- a/model/article/request/article.go +++ b/model/article/request/article.go @@ -4,6 +4,10 @@ import "git.echol.cn/loser/lckt/model/common/request" 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 } type DeleteIds struct { diff --git a/model/article/vo/article.go b/model/article/vo/article.go index 9a9494d..dd3ddbf 100644 --- a/model/article/vo/article.go +++ b/model/article/vo/article.go @@ -1,5 +1,18 @@ package vo +type ArticleListVo struct { + ID int `json:"id" gorm:"comment:文章ID"` + Title string `json:"title" gorm:"comment:文章标题"` + Desc string `json:"desc" gorm:"comment:文章描述"` + //Content string `json:"content" gorm:"comment:文章内容"` + CoverImg string `json:"coverImg" gorm:"comment:文章封面图"` + Price int64 `json:"price" gorm:"comment:文章价格(单位为分)"` + TeacherId int `json:"teacherId" gorm:"comment:讲师ID"` + TeacherName string `json:"teacherName" gorm:"comment:讲师名称"` + TeacherAvatar string `json:"teacherAvatar" gorm:"comment:讲师头像"` + IsFree int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是 +} + type ArticleVo struct { ID int `json:"id" gorm:"comment:文章ID"` Title string `json:"title" gorm:"comment:文章标题"` @@ -10,4 +23,6 @@ type ArticleVo struct { TeacherId int `json:"teacherId" gorm:"comment:讲师ID"` TeacherName string `json:"teacherName" gorm:"comment:讲师名称"` TeacherAvatar string `json:"teacherAvatar" gorm:"comment:讲师头像"` + IsFree int `json:"isFree" gorm:"comment:是否免费;default:0"` // 是否免费 0-否 1-是 + IsBuy int `json:"isBuy" gorm:"comment:是否购买;default:0"` // 是否购买 0-否 1-是 } diff --git a/router/article/article.go b/router/article/article.go index f9a3515..d370fd3 100644 --- a/router/article/article.go +++ b/router/article/article.go @@ -16,14 +16,15 @@ func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin articleRouter.POST("", artApi.Create) // 新建文章 articleRouter.DELETE("", artApi.Delete) // 批量删除文章 articleRouter.PUT("", artApi.Update) // 更新文章 - + articleRouter.GET("list", artApi.List) // 获取文章列表 + articleRouter.GET("", artApi.ById) // 文章开放接口 } { articleRouterWithoutRecord.GET(":id", artApi.ById) // 根据ID获取文章 } { - articleRouterWithoutAuth.GET("list", artApi.List) // 获取文章列表 - articleRouterWithoutAuth.GET("", artApi.ById) // 文章开放接口 + articleRouterWithoutAuth.GET("app/list", artApi.APPGetList) // 文章公开接口 + articleRouterWithoutAuth.GET("app/:id", artApi.AppById) // 文章开放接口 } } diff --git a/service/article/article.go b/service/article/article.go index 44e67f0..9b2aed0 100644 --- a/service/article/article.go +++ b/service/article/article.go @@ -2,8 +2,11 @@ package article import ( "git.echol.cn/loser/lckt/global" + "git.echol.cn/loser/lckt/model/app" "git.echol.cn/loser/lckt/model/article" "git.echol.cn/loser/lckt/model/article/request" + "git.echol.cn/loser/lckt/model/article/vo" + "go.uber.org/zap" ) type ArticleService struct{} @@ -39,3 +42,64 @@ func (ArticleService) GetArticleList(pageInfo request.GetList) (list []article.A err = db.Limit(limit).Offset(offset).Find(&list).Error return } + +func (s ArticleService) APPGetArticleList(pageInfo request.GetList) (list []vo.ArticleListVo, total int64, err error) { + limit := pageInfo.PageSize + offset := pageInfo.PageSize * (pageInfo.Page - 1) + + db := global.GVA_DB.Model(&article.Article{}) + err = db.Count(&total).Error + if err != nil { + return + } + + if pageInfo.Title != "" { + db = db.Where("title LIKE ?", "%"+pageInfo.Title+"%") + } + if pageInfo.CategoryId != 0 { + db = db.Where("category_id = ?", pageInfo.CategoryId) + } + if pageInfo.TeacherId != 0 { + db = db.Where("teacher_id = ?", pageInfo.TeacherId) + } + + if pageInfo.Keyword != "" { + db = db.Where("title LIKE ? OR article.desc LIKE ? OR teacher_name LIKE ?", "%"+pageInfo.Keyword+"%", "%"+pageInfo.Keyword+"%", "%"+pageInfo.Keyword+"%") + } + + err = db.Limit(limit).Offset(offset).Omit("teacher_avatar").Order("created_at desc").Find(&list).Error + for i, a := range list { + global.GVA_DB.Table("app_user").Select("avatar").Where("id = ?", a.TeacherId).Scan(&list[i].TeacherAvatar) + } + + return +} + +func (s ArticleService) APPGetArticle(id string, userId uint) (article vo.ArticleVo, err error) { + err = global.GVA_DB.Table("article").Where("id = ?", id).First(&article).Error + if err != nil { + global.GVA_LOG.Error("获取文章失败", zap.Error(err)) + return + } + article.IsBuy = 1 // 设置为已购买 + + global.GVA_DB.Table("app_user").Select("avatar").Where("id = ?", article.TeacherId).Scan(&article.TeacherAvatar) + + // 判断是否免费 + if article.IsFree == 0 { + // 如果不是免费文章,判断用户是否购买过 + var count int64 + err = global.GVA_DB.Model(&app.Order{}).Where("article_id = ? AND user_id = ?", id, userId).Count(&count).Error + if err != nil { + global.GVA_LOG.Error("查询用户购买记录失败", zap.Error(err)) + return + } + if count == 0 { + // 用户没有购买过,隐藏Content + article.Content = "" + article.IsBuy = 0 // 设置为未购买 + } + } + + return +}