27 changed files with 268 additions and 70 deletions
-
5api/v1/live_streams/live_streams.go
-
11api/v1/shows/shows.go
-
15internal/cmd/cmd.go
-
6internal/controller/ebooks/ebooks.go
-
22internal/controller/live_streams/live_streams.go
-
24internal/controller/shows/shows.go
-
28internal/dao/internal/live_streams.go
-
4internal/dao/internal/shows.go
-
4internal/logic/ebooks.go
-
37internal/logic/live_streams.go
-
1internal/logic/live_streams/live_streams.go
-
57internal/logic/shows.go
-
1internal/logic/shows/shows.go
-
16internal/model/do/live_streams.go
-
2internal/model/do/shows.go
-
10internal/model/entity/ebooks.go
-
14internal/model/entity/live_streams.go
-
24internal/model/entity/shows.go
-
0internal/service/ebooks.go
-
27internal/service/live_streams.go
-
1internal/service/live_streams/live_streams.go
-
26internal/service/shows.go
-
1internal/service/shows/shows.go
-
2main.go
-
BINresource/public/resource/image/live/p1.png
-
BINresource/public/resource/image/shows/t1.png
-
BINresource/public/resource/image/shows/v1.png
@ -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"` |
|||
} |
@ -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) |
|||
} |
@ -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) |
|||
} |
@ -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 +0,0 @@ |
|||
package live_streams |
@ -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 +0,0 @@ |
|||
package shows |
@ -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 +0,0 @@ |
|||
package live_streams |
@ -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 +0,0 @@ |
|||
package shows |
After Width: 228 | Height: 106 | Size: 36 KiB |
After Width: 145 | Height: 92 | Size: 20 KiB |
After Width: 191 | Height: 121 | Size: 64 KiB |
Write
Preview
Loading…
Cancel
Save
Reference in new issue