ZHANGZENGNING 1 month ago
parent
commit
cfcb706b15
  1. 5
      api/v1/live_streams/live_streams.go
  2. 11
      api/v1/shows/shows.go
  3. 15
      internal/cmd/cmd.go
  4. 6
      internal/controller/ebooks/ebooks.go
  5. 22
      internal/controller/live_streams/live_streams.go
  6. 24
      internal/controller/shows/shows.go
  7. 28
      internal/dao/internal/live_streams.go
  8. 4
      internal/dao/internal/shows.go
  9. 4
      internal/logic/ebooks.go
  10. 37
      internal/logic/live_streams.go
  11. 1
      internal/logic/live_streams/live_streams.go
  12. 57
      internal/logic/shows.go
  13. 1
      internal/logic/shows/shows.go
  14. 16
      internal/model/do/live_streams.go
  15. 2
      internal/model/do/shows.go
  16. 10
      internal/model/entity/ebooks.go
  17. 14
      internal/model/entity/live_streams.go
  18. 24
      internal/model/entity/shows.go
  19. 0
      internal/service/ebooks.go
  20. 27
      internal/service/live_streams.go
  21. 1
      internal/service/live_streams/live_streams.go
  22. 26
      internal/service/shows.go
  23. 1
      internal/service/shows/shows.go
  24. 2
      main.go
  25. BIN
      resource/public/resource/image/live/p1.png
  26. BIN
      resource/public/resource/image/shows/t1.png
  27. BIN
      resource/public/resource/image/shows/v1.png

5
api/v1/live_streams/live_streams.go

@ -2,9 +2,8 @@ package live_streams
import "practice_Go/internal/model/entity" import "practice_Go/internal/model/entity"
type GetListReq struct {
Status *int `v:"in:0,1" dc:"开播状态(0=未开播,1=直播中)"`
type GetLiveListReq struct {
} }
type GetListRes struct {
type GetLiveListRes struct {
List []*entity.LiveStreams `json:"list" dc:"LiveStreams list"` List []*entity.LiveStreams `json:"list" dc:"LiveStreams list"`
} }

11
api/v1/shows/shows.go

@ -1,12 +1,11 @@
package shows package shows
import (
"practice_Go/internal/model/entity"
)
import "practice_Go/internal/model/entity"
type GetListReq struct {
Type *int `v:"in:1,2" dc:"类型(1=文章,2=视频)"`
type ShowReq struct {
QueryType int `v:"required|in:1,2,3,4" p:"queryType"` // 验证参数有效性
} }
type GetListRes struct {
type GetShowsListRes struct {
List []*entity.Shows `json:"list" dc:"shows list"` List []*entity.Shows `json:"list" dc:"shows list"`
} }

15
internal/cmd/cmd.go

@ -2,13 +2,21 @@ package cmd
import ( import (
"context" "context"
controller "practice_Go/internal/controller/ebooks"
"practice_Go/internal/controller/shows"
"practice_Go/internal/controller/ebooks"
"practice_Go/internal/controller/live_streams"
"github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd" "github.com/gogf/gf/v2/os/gcmd"
) )
func MiddlewareCORS(r *ghttp.Request) {
r.Response.CORSDefault()
r.Middleware.Next()
}
var ( var (
Main = gcmd.Command{ Main = gcmd.Command{
Name: "main", Name: "main",
@ -17,7 +25,10 @@ var (
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server() s := g.Server()
s.Group("/api", func(group *ghttp.RouterGroup) { s.Group("/api", func(group *ghttp.RouterGroup) {
group.POST("/ebooks/getEbookList", controller.Ebooks.GetEbooksList)
group.Middleware(MiddlewareCORS)
group.POST("/ebooks/getEbookList", ebooks.Ebooks.GetEbooksList)
group.POST("/live/getLiveList", live_streams.Live.GetLiveList)
group.POST("/show/getShowList", shows.Shows.GetShowList)
}) })
s.Run() s.Run()
return nil return nil

6
internal/controller/ebooks/ebooks.go

@ -1,9 +1,9 @@
package controller
package ebooks
import ( import (
"github.com/gogf/gf/v2/net/ghttp" "github.com/gogf/gf/v2/net/ghttp"
"practice_Go/api/v1/ebooks" "practice_Go/api/v1/ebooks"
"practice_Go/internal/service/ebooks"
ebook "practice_Go/internal/service"
) )
type cEbooks struct{} type cEbooks struct{}
@ -15,7 +15,7 @@ func (c *cEbooks) GetEbooksList(r *ghttp.Request) {
if err := r.Parse(&req); err != nil { if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(err.Error()) r.Response.WriteJsonExit(err.Error())
} }
res, err := service.Ebooks().GetEbooksList(r.Context(), req)
res, err := ebook.Ebooks().GetEbooksList(r.Context(), req)
if err != nil { if err != nil {
r.Response.WriteJsonExit(err.Error()) r.Response.WriteJsonExit(err.Error())
} }

22
internal/controller/live_streams/live_streams.go

@ -1 +1,23 @@
package live_streams package live_streams
import (
"github.com/gogf/gf/v2/net/ghttp"
"practice_Go/api/v1/live_streams"
live "practice_Go/internal/service"
)
type cLive struct{}
var Live = cLive{}
func (c *cLive) GetLiveList(r *ghttp.Request) {
var req *live_streams.GetLiveListReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(err.Error())
}
res, err := live.Live().GetLiveList(r.Context(), req)
if err != nil {
r.Response.WriteJsonExit(err.Error())
}
r.Response.WriteJsonExit(res)
}

24
internal/controller/shows/shows.go

@ -1 +1,25 @@
package shows package shows
import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/net/ghttp"
"practice_Go/api/v1/shows"
"practice_Go/internal/service"
)
type cShows struct{}
var Shows = cShows{}
func (c *cShows) GetShowList(r *ghttp.Request) {
var req *shows.ShowReq
if err := r.Parse(&req); err != nil {
r.Response.WriteJsonExit(gcode.New(400, "参数错误", nil))
}
res, err := service.Shows().GetShowsList(r.Context(), req)
if err != nil {
r.Response.WriteJsonExit(gcode.New(500, err.Error(), nil))
}
r.Response.WriteJson(res)
}

28
internal/dao/internal/live_streams.go

@ -20,24 +20,24 @@ type LiveStreamsDao struct {
// LiveStreamsColumns defines and stores column names for table live_streams. // LiveStreamsColumns defines and stores column names for table live_streams.
type LiveStreamsColumns struct { type LiveStreamsColumns struct {
Id string // 主键
Cover string // 直播封面URL
Title string // 直播标题
Avatar string // 主播头像URL
PublisherId string // 主播ID
StartTime string // 计划开播时间
Status string // 开播状态(0=未开播,1=直播中)
Id string // 主键
Cover string // 直播封面URL
Title string // 直播标题
Avatar string // 主播头像URL
PublisherName string // 主播名字
StartTime string // 计划开播时间
Status string // 开播状态(0=未开播,1=直播中)
} }
// liveStreamsColumns holds the columns for table live_streams. // liveStreamsColumns holds the columns for table live_streams.
var liveStreamsColumns = LiveStreamsColumns{ var liveStreamsColumns = LiveStreamsColumns{
Id: "id",
Cover: "cover",
Title: "title",
Avatar: "avatar",
PublisherId: "publisher_id",
StartTime: "start_time",
Status: "status",
Id: "id",
Cover: "cover",
Title: "title",
Avatar: "avatar",
PublisherName: "publisher_name",
StartTime: "start_time",
Status: "status",
} }
// NewLiveStreamsDao creates and returns a new DAO object for table data access. // NewLiveStreamsDao creates and returns a new DAO object for table data access.

4
internal/dao/internal/shows.go

@ -23,7 +23,7 @@ type ShowsColumns struct {
Id string // 主键 Id string // 主键
Cover string // 封面图URL Cover string // 封面图URL
Title string // 标题 Title string // 标题
PublisherId string // 发布人ID
PublisherName string // 发布人名字
PublisherAvatar string // 发布人头像URL PublisherAvatar string // 发布人头像URL
PublishTime string // 发布时间 PublishTime string // 发布时间
ViewCount string // 观看人数 ViewCount string // 观看人数
@ -39,7 +39,7 @@ var showsColumns = ShowsColumns{
Id: "id", Id: "id",
Cover: "cover", Cover: "cover",
Title: "title", Title: "title",
PublisherId: "publisher_id",
PublisherName: "publisher_name",
PublisherAvatar: "publisher_avatar", PublisherAvatar: "publisher_avatar",
PublishTime: "publish_time", PublishTime: "publish_time",
ViewCount: "view_count", ViewCount: "view_count",

4
internal/logic/ebooks/ebooks.go → internal/logic/ebooks.go

@ -4,13 +4,13 @@ import (
"context" "context"
"practice_Go/api/v1/ebooks" "practice_Go/api/v1/ebooks"
"practice_Go/internal/dao" "practice_Go/internal/dao"
"practice_Go/internal/service/ebooks"
ebook "practice_Go/internal/service"
) )
type sEbooks struct{} type sEbooks struct{}
func init() { func init() {
service.RegisterEbooks(New())
ebook.RegisterEbooks(New())
} }
func New() *sEbooks { func New() *sEbooks {
return &sEbooks{} return &sEbooks{}

37
internal/logic/live_streams.go

@ -0,0 +1,37 @@
package logic
import (
"context"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"practice_Go/api/v1/live_streams"
"practice_Go/internal/dao"
"practice_Go/internal/model/entity"
live "practice_Go/internal/service"
)
type sLives struct{}
func init() {
live.RegisterLives(NewLive())
}
func NewLive() *sLives {
return &sLives{}
}
// 实现接口 ILive 的方法
func (s *sLives) GetLiveList(ctx context.Context, req *live_streams.GetLiveListReq) (res live_streams.GetLiveListRes, err error) {
var list []*entity.LiveStreams
err = dao.LiveStreams.Ctx(ctx).
OrderAsc("start_time"). // 按开播时间升序
Limit(8). // 取前8条
Scan(&list)
if err != nil {
g.Log().Errorf(ctx, "直播数据获取失败:%v", err)
return res, gerror.Wrap(err, "直播数据服务异常")
}
// 将 list 转换为 live_streams.GetLiveListRes 的结构
res.List = list
return res, nil
}

1
internal/logic/live_streams/live_streams.go

@ -1 +0,0 @@
package live_streams

57
internal/logic/shows.go

@ -0,0 +1,57 @@
package logic
import (
"context"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
v1 "practice_Go/api/v1/shows"
"practice_Go/internal/dao"
"practice_Go/internal/model/entity"
show "practice_Go/internal/service"
)
type sShows struct{}
func init() {
show.RegisterShows(NewShows())
}
func NewShows() *sShows {
return &sShows{}
}
const (
QueryRecommend = iota + 1 // 推荐界面
QueryVideo // 精选视频
QueryClub // 博股俱乐部
QueryChannel // homilyLink频道
)
// 实现 IShows 接口中的 GetShowsList 方法
func (s *sShows) GetShowsList(ctx context.Context, req *v1.ShowReq) (res v1.GetShowsListRes, err error) {
model := dao.Shows.Ctx(ctx)
// 根据请求类型动态调整查询条件
switch req.QueryType {
case QueryVideo:
model = model.Where("type", 2).OrderAsc("publish_time") // 视频类型正序
case QueryClub:
model = model.Where("club_id", 1).OrderDesc("publish_time") // 俱乐部倒序
case QueryChannel:
model = model.Where("publisher_name", "homilyLink").OrderDesc("publish_time")
default:
model = model.OrderDesc("publish_time") // 默认推荐倒序
}
// 查询数据并填充到结果列表
var list []*entity.Shows
if err := model.Scan(&list); err != nil {
g.Log().Errorf(ctx, "内容查询异常|类型:%d|错误:%v", req.QueryType, err)
return res, gerror.WrapCode(gcode.CodeDbOperationError, err, "数据获取失败")
}
// 将查询结果赋值给返回值
res.List = list
return res, nil
}

1
internal/logic/shows/shows.go

@ -1 +0,0 @@
package shows

16
internal/model/do/live_streams.go

@ -11,12 +11,12 @@ import (
// LiveStreams is the golang structure of table live_streams for DAO operations like Where/Data. // LiveStreams is the golang structure of table live_streams for DAO operations like Where/Data.
type LiveStreams struct { type LiveStreams struct {
g.Meta `orm:"table:live_streams, do:true"`
Id interface{} // 主键
Cover interface{} // 直播封面URL
Title interface{} // 直播标题
Avatar interface{} // 主播头像URL
PublisherId interface{} // 主播ID
StartTime *gtime.Time // 计划开播时间
Status interface{} // 开播状态(0=未开播,1=直播中)
g.Meta `orm:"table:live_streams, do:true"`
Id interface{} // 主键
Cover interface{} // 直播封面URL
Title interface{} // 直播标题
Avatar interface{} // 主播头像URL
PublisherName interface{} // 主播名字
StartTime *gtime.Time // 计划开播时间
Status interface{} // 开播状态(0=未开播,1=直播中)
} }

2
internal/model/do/shows.go

@ -15,7 +15,7 @@ type Shows struct {
Id interface{} // 主键 Id interface{} // 主键
Cover interface{} // 封面图URL Cover interface{} // 封面图URL
Title interface{} // 标题 Title interface{} // 标题
PublisherId interface{} // 发布人ID
PublisherName interface{} // 发布人名字
PublisherAvatar interface{} // 发布人头像URL PublisherAvatar interface{} // 发布人头像URL
PublishTime *gtime.Time // 发布时间 PublishTime *gtime.Time // 发布时间
ViewCount interface{} // 观看人数 ViewCount interface{} // 观看人数

10
internal/model/entity/ebooks.go

@ -6,9 +6,9 @@ package entity
// Ebooks is the golang structure for table ebooks. // Ebooks is the golang structure for table ebooks.
type Ebooks struct { type Ebooks struct {
Id uint64 `json:"id" orm:"id" ` // 主键
Cover string `json:"cover" orm:"cover" ` // 封面图URL
Title string `json:"title" orm:"title" ` // 书名(唯一约束)
Description string `json:"description" orm:"description" ` // 简介
ViewCount uint `json:"viewCount" orm:"view_count" ` // 观看人数
Id uint64 `json:"id" orm:"id" description:"主键"` // 主键
Cover string `json:"cover" orm:"cover" description:"封面图URL"` // 封面图URL
Title string `json:"title" orm:"title" description:"书名(唯一约束)"` // 书名(唯一约束)
Description string `json:"description" orm:"description" description:"简介"` // 简介
ViewCount uint `json:"viewCount" orm:"view_count" description:"观看人数"` // 观看人数
} }

14
internal/model/entity/live_streams.go

@ -10,11 +10,11 @@ import (
// LiveStreams is the golang structure for table live_streams. // LiveStreams is the golang structure for table live_streams.
type LiveStreams struct { type LiveStreams struct {
Id uint64 `json:"id" orm:"id" ` // 主键
Cover string `json:"cover" orm:"cover" ` // 直播封面URL
Title string `json:"title" orm:"title" ` // 直播标题
Avatar string `json:"avatar" orm:"avatar" ` // 主播头像URL
PublisherId uint64 `json:"publisherId" orm:"publisher_id" ` // 主播ID
StartTime *gtime.Time `json:"startTime" orm:"start_time" ` // 计划开播时间
Status int `json:"status" orm:"status" ` // 开播状态(0=未开播,1=直播中)
Id uint64 `json:"id" orm:"id" description:"主键"` // 主键
Cover string `json:"cover" orm:"cover" description:"直播封面URL"` // 直播封面URL
Title string `json:"title" orm:"title" description:"直播标题"` // 直播标题
Avatar string `json:"avatar" orm:"avatar" description:"主播头像URL"` // 主播头像URL
PublisherName string `json:"publisherName" orm:"publisher_name" description:"主播名字"` // 主播名字
StartTime *gtime.Time `json:"startTime" orm:"start_time" description:"计划开播时间"` // 计划开播时间
Status int `json:"status" orm:"status" description:"开播状态(0=未开播,1=直播中)"` // 开播状态(0=未开播,1=直播中)
} }

24
internal/model/entity/shows.go

@ -10,16 +10,16 @@ import (
// Shows is the golang structure for table shows. // Shows is the golang structure for table shows.
type Shows struct { type Shows struct {
Id uint64 `json:"id" orm:"id" ` // 主键
Cover string `json:"cover" orm:"cover" ` // 封面图URL
Title string `json:"title" orm:"title" ` // 标题
PublisherId uint64 `json:"publisherId" orm:"publisher_id" ` // 发布人ID
PublisherAvatar string `json:"publisherAvatar" orm:"publisher_avatar" ` // 发布人头像URL
PublishTime *gtime.Time `json:"publishTime" orm:"publish_time" ` // 发布时间
ViewCount uint `json:"viewCount" orm:"view_count" ` // 观看人数
CommentCount uint `json:"commentCount" orm:"comment_count" ` // 评论数
LikeCount uint `json:"likeCount" orm:"like_count" ` // 点赞数
Type int `json:"type" orm:"type" ` // 类型(1=文章,2=视频)
ClubId int `json:"clubId" orm:"club_id" ` // 所属俱乐部(0=无,1=博古)
VideoDuration uint `json:"videoDuration" orm:"video_duration" ` // 视频时长(秒)
Id uint64 `json:"id" orm:"id" description:"主键"` // 主键
Cover string `json:"cover" orm:"cover" description:"封面图URL"` // 封面图URL
Title string `json:"title" orm:"title" description:"标题"` // 标题
PublisherName string `json:"publisherName" orm:"publisher_name" description:"发布人名字"` // 发布人名字
PublisherAvatar string `json:"publisherAvatar" orm:"publisher_avatar" description:"发布人头像URL"` // 发布人头像URL
PublishTime *gtime.Time `json:"publishTime" orm:"publish_time" description:"发布时间"` // 发布时间
ViewCount uint `json:"viewCount" orm:"view_count" description:"观看人数"` // 观看人数
CommentCount uint `json:"commentCount" orm:"comment_count" description:"评论数"` // 评论数
LikeCount uint `json:"likeCount" orm:"like_count" description:"点赞数"` // 点赞数
Type int `json:"type" orm:"type" description:"类型(1=文章,2=视频)"` // 类型(1=文章,2=视频)
ClubId int `json:"clubId" orm:"club_id" description:"所属俱乐部(0=无,1=博古)"` // 所属俱乐部(0=无,1=博古)
VideoDuration uint `json:"videoDuration" orm:"video_duration" description:"视频时长(秒)"` // 视频时长(秒)
} }

0
internal/service/ebooks/ebooks.go → internal/service/ebooks.go

27
internal/service/live_streams.go

@ -0,0 +1,27 @@
package service
import (
"context"
"practice_Go/api/v1/live_streams"
)
type (
ILive interface {
GetLiveList(ctx context.Context, req *live_streams.GetLiveListReq) (res live_streams.GetLiveListRes, err error)
}
)
var (
localLive ILive
)
func Live() ILive {
if localLive == nil {
panic("implement not found for interface IUser, forgot register?")
}
return localLive
}
func RegisterLives(i ILive) {
localLive = i
}

1
internal/service/live_streams/live_streams.go

@ -1 +0,0 @@
package live_streams

26
internal/service/shows.go

@ -0,0 +1,26 @@
package service
import (
"context"
"practice_Go/api/v1/shows"
)
type (
IShows interface {
GetShowsList(ctx context.Context, req *shows.ShowReq) (res shows.GetShowsListRes, err error)
}
)
var (
localShows IShows
)
func Shows() IShows {
if localShows == nil {
panic("implement not found for interface IShows")
}
return localShows
}
func RegisterShows(i IShows) {
localShows = i
}

1
internal/service/shows/shows.go

@ -1 +0,0 @@
package shows

2
main.go

@ -4,7 +4,7 @@ import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2" _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
"github.com/gogf/gf/v2/os/gctx" "github.com/gogf/gf/v2/os/gctx"
"practice_Go/internal/cmd" "practice_Go/internal/cmd"
_ "practice_Go/internal/logic/ebooks"
_ "practice_Go/internal/logic"
_ "practice_Go/internal/packed" _ "practice_Go/internal/packed"
) )

BIN
resource/public/resource/image/live/p1.png

After

Width: 228  |  Height: 106  |  Size: 36 KiB

BIN
resource/public/resource/image/shows/t1.png

After

Width: 145  |  Height: 92  |  Size: 20 KiB

BIN
resource/public/resource/image/shows/v1.png

After

Width: 191  |  Height: 121  |  Size: 64 KiB

Loading…
Cancel
Save