🎨 优化文章相关接口,新增app专用接口

This commit is contained in:
2025-07-19 06:18:33 +08:00
parent cf0f60d221
commit aac13d369c
6 changed files with 126 additions and 4 deletions

View File

@@ -5,6 +5,7 @@ import (
"git.echol.cn/loser/lckt/model/article" "git.echol.cn/loser/lckt/model/article"
"git.echol.cn/loser/lckt/model/article/request" "git.echol.cn/loser/lckt/model/article/request"
r "git.echol.cn/loser/lckt/model/common/response" r "git.echol.cn/loser/lckt/model/common/response"
"git.echol.cn/loser/lckt/utils/user_jwt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"go.uber.org/zap" "go.uber.org/zap"
) )
@@ -88,3 +89,37 @@ func (ArticleApi) ById(ctx *gin.Context) {
} }
r.OkWithData(article, ctx) 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)
}

View File

@@ -11,8 +11,11 @@ type Article struct {
Content string `json:"content" gorm:"comment:文章内容;type:longtext"` Content string `json:"content" gorm:"comment:文章内容;type:longtext"`
CoverImg string `json:"coverImg" gorm:"comment:文章封面图"` CoverImg string `json:"coverImg" gorm:"comment:文章封面图"`
TeacherId int `json:"teacherId" gorm:"comment:讲师ID"` TeacherId int `json:"teacherId" gorm:"comment:讲师ID"`
Price int64 `json:"price" gorm:"comment:文章价格(单位为分)"`
TeacherName string `json:"teacherName" 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 文章表 // TableName 文章表

View File

@@ -4,6 +4,10 @@ import "git.echol.cn/loser/lckt/model/common/request"
type GetList struct { type GetList struct {
request.PageInfo 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 { type DeleteIds struct {

View File

@@ -1,5 +1,18 @@
package vo 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 { type ArticleVo struct {
ID int `json:"id" gorm:"comment:文章ID"` ID int `json:"id" gorm:"comment:文章ID"`
Title string `json:"title" gorm:"comment:文章标题"` Title string `json:"title" gorm:"comment:文章标题"`
@@ -10,4 +23,6 @@ type ArticleVo struct {
TeacherId int `json:"teacherId" gorm:"comment:讲师ID"` TeacherId int `json:"teacherId" gorm:"comment:讲师ID"`
TeacherName string `json:"teacherName" gorm:"comment:讲师名称"` TeacherName string `json:"teacherName" gorm:"comment:讲师名称"`
TeacherAvatar string `json:"teacherAvatar" 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-是
} }

View File

@@ -16,14 +16,15 @@ func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin
articleRouter.POST("", artApi.Create) // 新建文章 articleRouter.POST("", artApi.Create) // 新建文章
articleRouter.DELETE("", artApi.Delete) // 批量删除文章 articleRouter.DELETE("", artApi.Delete) // 批量删除文章
articleRouter.PUT("", artApi.Update) // 更新文章 articleRouter.PUT("", artApi.Update) // 更新文章
articleRouter.GET("list", artApi.List) // 获取文章列表
articleRouter.GET("", artApi.ById) // 文章开放接口
} }
{ {
articleRouterWithoutRecord.GET(":id", artApi.ById) // 根据ID获取文章 articleRouterWithoutRecord.GET(":id", artApi.ById) // 根据ID获取文章
} }
{ {
articleRouterWithoutAuth.GET("list", artApi.List) // 获取文章列表 articleRouterWithoutAuth.GET("app/list", artApi.APPGetList) // 文章公开接口
articleRouterWithoutAuth.GET("", artApi.ById) // 文章开放接口 articleRouterWithoutAuth.GET("app/:id", artApi.AppById) // 文章开放接口
} }
} }

View File

@@ -2,8 +2,11 @@ package article
import ( import (
"git.echol.cn/loser/lckt/global" "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"
"git.echol.cn/loser/lckt/model/article/request" "git.echol.cn/loser/lckt/model/article/request"
"git.echol.cn/loser/lckt/model/article/vo"
"go.uber.org/zap"
) )
type ArticleService struct{} 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 err = db.Limit(limit).Offset(offset).Find(&list).Error
return 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
}