🎨 优化文章上传接口
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
type ArticleApi struct{}
|
||||
|
||||
func (ArticleApi) Create(ctx *gin.Context) {
|
||||
var p article.Article
|
||||
var p request.CreateArticle
|
||||
if err := ctx.ShouldBind(&p); err != nil {
|
||||
r.FailWithMessage(err.Error(), ctx)
|
||||
global.GVA_LOG.Error("参数有误!", zap.Error(err))
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package request
|
||||
|
||||
import "git.echol.cn/loser/lckt/model/common/request"
|
||||
import (
|
||||
"git.echol.cn/loser/lckt/model/common/request"
|
||||
)
|
||||
|
||||
type GetList struct {
|
||||
request.PageInfo
|
||||
@@ -26,4 +28,19 @@ type BulkUpload struct {
|
||||
// 发布时间
|
||||
PublishTime string `json:"publishTime" form:"publishTime"` // 发布时间
|
||||
IsFree *int `json:"isFree" form:"isFree"` // 是否免费
|
||||
FreeTime string `json:"freeTime" form:"freeTime"` // 设置为免费时,免费时间段
|
||||
}
|
||||
|
||||
type CreateArticle struct {
|
||||
Title string `json:"title" form:"title" binding:"required"`
|
||||
Desc string `json:"desc" form:"desc" binding:"required"`
|
||||
Content string `json:"content" form:"content" binding:"required"`
|
||||
CoverImg string `json:"coverImg" form:"coverImg" binding:"required"`
|
||||
TeacherId int `json:"teacherId" form:"teacherId" binding:"required"`
|
||||
TeacherName string `json:"teacherName" form:"teacherName" binding:"required"`
|
||||
Price int64 `json:"price" form:"price" binding:"required"` // 价格,单位分
|
||||
IsFree *int `json:"isFree" form:"isFree"` // 是否免费 0-否 1-是
|
||||
// 分类ID
|
||||
CategoryId int `json:"categoryId" form:"categoryId" binding:"required"` // 分类ID
|
||||
PublishTime string `json:"publishTime" form:"publishTime"` // 发布时间
|
||||
}
|
||||
|
@@ -8,10 +8,9 @@ import (
|
||||
type ArticleRouter struct{}
|
||||
|
||||
// InitBotRouter 初始化 文章 路由信息
|
||||
func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin.RouterGroup, AppRouter *gin.RouterGroup) {
|
||||
func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, AppRouter *gin.RouterGroup) {
|
||||
articleRouter := Router.Group("article").Use(middleware.OperationRecord())
|
||||
articleRouterWithoutRecord := Router.Group("article")
|
||||
articleRouterWithoutAuth := PublicRouter.Group("article")
|
||||
appRouter := AppRouter.Group("article")
|
||||
{
|
||||
articleRouter.POST("", artApi.Create) // 新建文章
|
||||
@@ -26,8 +25,8 @@ func (s *ArticleRouter) InitBotRouter(Router *gin.RouterGroup, PublicRouter *gin
|
||||
|
||||
}
|
||||
{
|
||||
articleRouterWithoutAuth.GET("app/list", artApi.APPGetList) // 文章公开接口
|
||||
articleRouterWithoutAuth.GET("app/:id", artApi.AppById) // 文章开放接口
|
||||
appRouter.GET("app/list", artApi.APPGetList) // 文章公开接口
|
||||
appRouter.GET("app/:id", artApi.AppById) // 文章开放接口
|
||||
}
|
||||
{
|
||||
// App端文章相关接口
|
||||
|
@@ -15,8 +15,31 @@ import (
|
||||
|
||||
type ArticleService struct{}
|
||||
|
||||
func (ArticleService) CreateArticle(req article.Article) (err error) {
|
||||
err = global.GVA_DB.Create(&req).Error
|
||||
func (ArticleService) CreateArticle(req request.CreateArticle) (err error) {
|
||||
// 将 p.PublishTime转为time.time类型
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
publishTime, _ := time.ParseInLocation("2006-01-02 15:04:05", req.PublishTime, loc)
|
||||
|
||||
status := 2
|
||||
if req.PublishTime == "" {
|
||||
status = 1 // 如果没有设置发布时间,默认立即发布
|
||||
}
|
||||
|
||||
model := article.Article{
|
||||
Title: req.Title,
|
||||
Desc: req.Desc,
|
||||
CategoryId: req.CategoryId,
|
||||
TeacherId: req.TeacherId,
|
||||
TeacherName: req.TeacherName,
|
||||
CoverImg: req.CoverImg,
|
||||
Content: req.Content,
|
||||
IsFree: req.IsFree,
|
||||
Price: req.Price,
|
||||
PublishTime: &publishTime,
|
||||
Status: status,
|
||||
}
|
||||
|
||||
err = global.GVA_DB.Create(&model).Error
|
||||
return
|
||||
}
|
||||
|
||||
@@ -287,6 +310,11 @@ func (s ArticleService) BulkUpload(p request.BulkUpload) (err error) {
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
publishTime, _ := time.ParseInLocation("2006-01-02 15:04:05", p.PublishTime, loc)
|
||||
|
||||
status := 2
|
||||
if p.PublishTime == "" {
|
||||
status = 1 // 如果没有设置发布时间,默认立即发布
|
||||
}
|
||||
|
||||
articles = append(articles, article.Article{
|
||||
Title: teacher.NickName + "--" + p.Title,
|
||||
Desc: p.Desc,
|
||||
@@ -298,6 +326,7 @@ func (s ArticleService) BulkUpload(p request.BulkUpload) (err error) {
|
||||
IsFree: p.IsFree,
|
||||
Price: int64(p.Price),
|
||||
PublishTime: &publishTime,
|
||||
Status: status,
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user