diff --git a/api/v1/live_streams/live_streams.go b/api/v1/live_streams/live_streams.go index bd91bc3..98ecb9e 100644 --- a/api/v1/live_streams/live_streams.go +++ b/api/v1/live_streams/live_streams.go @@ -2,9 +2,8 @@ package live_streams 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"` } diff --git a/api/v1/shows/shows.go b/api/v1/shows/shows.go index 479d207..8a7cf77 100644 --- a/api/v1/shows/shows.go +++ b/api/v1/shows/shows.go @@ -1,12 +1,11 @@ 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"` } diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 419ec86..6f53ebc 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -2,13 +2,21 @@ package cmd import ( "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/net/ghttp" "github.com/gogf/gf/v2/os/gcmd" ) +func MiddlewareCORS(r *ghttp.Request) { + r.Response.CORSDefault() + r.Middleware.Next() +} + var ( Main = gcmd.Command{ Name: "main", @@ -17,7 +25,10 @@ var ( Func: func(ctx context.Context, parser *gcmd.Parser) (err error) { s := g.Server() 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() return nil diff --git a/internal/controller/ebooks/ebooks.go b/internal/controller/ebooks/ebooks.go index 98bb67c..3e6839c 100644 --- a/internal/controller/ebooks/ebooks.go +++ b/internal/controller/ebooks/ebooks.go @@ -1,9 +1,9 @@ -package controller +package ebooks import ( "github.com/gogf/gf/v2/net/ghttp" "practice_Go/api/v1/ebooks" - "practice_Go/internal/service/ebooks" + ebook "practice_Go/internal/service" ) type cEbooks struct{} @@ -15,7 +15,7 @@ func (c *cEbooks) GetEbooksList(r *ghttp.Request) { if err := r.Parse(&req); err != nil { r.Response.WriteJsonExit(err.Error()) } - res, err := service.Ebooks().GetEbooksList(r.Context(), req) + res, err := ebook.Ebooks().GetEbooksList(r.Context(), req) if err != nil { r.Response.WriteJsonExit(err.Error()) } diff --git a/internal/controller/live_streams/live_streams.go b/internal/controller/live_streams/live_streams.go index acbeb44..1479ac7 100644 --- a/internal/controller/live_streams/live_streams.go +++ b/internal/controller/live_streams/live_streams.go @@ -1 +1,23 @@ 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) +} diff --git a/internal/controller/shows/shows.go b/internal/controller/shows/shows.go index 01b2981..5ab8a68 100644 --- a/internal/controller/shows/shows.go +++ b/internal/controller/shows/shows.go @@ -1 +1,25 @@ 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) +} diff --git a/internal/dao/internal/live_streams.go b/internal/dao/internal/live_streams.go index 6002938..7270d88 100644 --- a/internal/dao/internal/live_streams.go +++ b/internal/dao/internal/live_streams.go @@ -20,24 +20,24 @@ type LiveStreamsDao struct { // LiveStreamsColumns defines and stores column names for table live_streams. 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. 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. diff --git a/internal/dao/internal/shows.go b/internal/dao/internal/shows.go index 7b5fb2a..e902a0a 100644 --- a/internal/dao/internal/shows.go +++ b/internal/dao/internal/shows.go @@ -23,7 +23,7 @@ type ShowsColumns struct { Id string // 主键 Cover string // 封面图URL Title string // 标题 - PublisherId string // 发布人ID + PublisherName string // 发布人名字 PublisherAvatar string // 发布人头像URL PublishTime string // 发布时间 ViewCount string // 观看人数 @@ -39,7 +39,7 @@ var showsColumns = ShowsColumns{ Id: "id", Cover: "cover", Title: "title", - PublisherId: "publisher_id", + PublisherName: "publisher_name", PublisherAvatar: "publisher_avatar", PublishTime: "publish_time", ViewCount: "view_count", diff --git a/internal/logic/ebooks/ebooks.go b/internal/logic/ebooks.go similarity index 84% rename from internal/logic/ebooks/ebooks.go rename to internal/logic/ebooks.go index 30b4c7c..b4e099a 100644 --- a/internal/logic/ebooks/ebooks.go +++ b/internal/logic/ebooks.go @@ -4,13 +4,13 @@ import ( "context" "practice_Go/api/v1/ebooks" "practice_Go/internal/dao" - "practice_Go/internal/service/ebooks" + ebook "practice_Go/internal/service" ) type sEbooks struct{} func init() { - service.RegisterEbooks(New()) + ebook.RegisterEbooks(New()) } func New() *sEbooks { return &sEbooks{} diff --git a/internal/logic/live_streams.go b/internal/logic/live_streams.go new file mode 100644 index 0000000..ab98671 --- /dev/null +++ b/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 +} diff --git a/internal/logic/live_streams/live_streams.go b/internal/logic/live_streams/live_streams.go deleted file mode 100644 index acbeb44..0000000 --- a/internal/logic/live_streams/live_streams.go +++ /dev/null @@ -1 +0,0 @@ -package live_streams diff --git a/internal/logic/shows.go b/internal/logic/shows.go new file mode 100644 index 0000000..a98f331 --- /dev/null +++ b/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 +} diff --git a/internal/logic/shows/shows.go b/internal/logic/shows/shows.go deleted file mode 100644 index 01b2981..0000000 --- a/internal/logic/shows/shows.go +++ /dev/null @@ -1 +0,0 @@ -package shows diff --git a/internal/model/do/live_streams.go b/internal/model/do/live_streams.go index e769851..e94ff89 100644 --- a/internal/model/do/live_streams.go +++ b/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. 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=直播中) } diff --git a/internal/model/do/shows.go b/internal/model/do/shows.go index ddcd448..12abea4 100644 --- a/internal/model/do/shows.go +++ b/internal/model/do/shows.go @@ -15,7 +15,7 @@ type Shows struct { Id interface{} // 主键 Cover interface{} // 封面图URL Title interface{} // 标题 - PublisherId interface{} // 发布人ID + PublisherName interface{} // 发布人名字 PublisherAvatar interface{} // 发布人头像URL PublishTime *gtime.Time // 发布时间 ViewCount interface{} // 观看人数 diff --git a/internal/model/entity/ebooks.go b/internal/model/entity/ebooks.go index a8f2f27..d3fa710 100644 --- a/internal/model/entity/ebooks.go +++ b/internal/model/entity/ebooks.go @@ -6,9 +6,9 @@ package entity // Ebooks is the golang structure for table ebooks. 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:"观看人数"` // 观看人数 } diff --git a/internal/model/entity/live_streams.go b/internal/model/entity/live_streams.go index 9d94f7a..4616ba8 100644 --- a/internal/model/entity/live_streams.go +++ b/internal/model/entity/live_streams.go @@ -10,11 +10,11 @@ import ( // LiveStreams is the golang structure for table live_streams. 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=直播中) } diff --git a/internal/model/entity/shows.go b/internal/model/entity/shows.go index afb760e..8fa5d37 100644 --- a/internal/model/entity/shows.go +++ b/internal/model/entity/shows.go @@ -10,16 +10,16 @@ import ( // Shows is the golang structure for table shows. 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:"视频时长(秒)"` // 视频时长(秒) } diff --git a/internal/service/ebooks/ebooks.go b/internal/service/ebooks.go similarity index 100% rename from internal/service/ebooks/ebooks.go rename to internal/service/ebooks.go diff --git a/internal/service/live_streams.go b/internal/service/live_streams.go new file mode 100644 index 0000000..7a82eb8 --- /dev/null +++ b/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 +} diff --git a/internal/service/live_streams/live_streams.go b/internal/service/live_streams/live_streams.go deleted file mode 100644 index acbeb44..0000000 --- a/internal/service/live_streams/live_streams.go +++ /dev/null @@ -1 +0,0 @@ -package live_streams diff --git a/internal/service/shows.go b/internal/service/shows.go new file mode 100644 index 0000000..79f38fb --- /dev/null +++ b/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 +} diff --git a/internal/service/shows/shows.go b/internal/service/shows/shows.go deleted file mode 100644 index 01b2981..0000000 --- a/internal/service/shows/shows.go +++ /dev/null @@ -1 +0,0 @@ -package shows diff --git a/main.go b/main.go index 8040101..ee4befd 100644 --- a/main.go +++ b/main.go @@ -4,7 +4,7 @@ import ( _ "github.com/gogf/gf/contrib/drivers/mysql/v2" "github.com/gogf/gf/v2/os/gctx" "practice_Go/internal/cmd" - _ "practice_Go/internal/logic/ebooks" + _ "practice_Go/internal/logic" _ "practice_Go/internal/packed" ) diff --git a/resource/public/resource/image/live/p1.png b/resource/public/resource/image/live/p1.png new file mode 100644 index 0000000..3ca4a97 Binary files /dev/null and b/resource/public/resource/image/live/p1.png differ diff --git a/resource/public/resource/image/shows/t1.png b/resource/public/resource/image/shows/t1.png new file mode 100644 index 0000000..9fefa3a Binary files /dev/null and b/resource/public/resource/image/shows/t1.png differ diff --git a/resource/public/resource/image/shows/v1.png b/resource/public/resource/image/shows/v1.png new file mode 100644 index 0000000..227d6fb Binary files /dev/null and b/resource/public/resource/image/shows/v1.png differ