240 lines
6.4 KiB
Go
240 lines
6.4 KiB
Go
package playletser
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
"videoweb/app/service/upstream"
|
|
"videoweb/pub/common/log"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
const homePath = "/api/app/playlet/home"
|
|
const homeCacheTTL = 30 * time.Second
|
|
|
|
var (
|
|
homeMu sync.Mutex
|
|
homeCache *HomePlayletResponse
|
|
homeAt time.Time
|
|
)
|
|
|
|
// FetchHome 拉取首页短剧聚合数据。
|
|
func FetchHome(c *gin.Context) (*HomePlayletResponse, error) {
|
|
var env APIEnvelope[HomePlayletResponse]
|
|
if err := upstream.GetPublicJSON(c, homePath, &env); err != nil {
|
|
return nil, err
|
|
}
|
|
if env.Code != 200 {
|
|
return nil, fmt.Errorf("playlet home code=%d msg=%s", env.Code, env.Msg)
|
|
}
|
|
return &env.Data, nil
|
|
}
|
|
|
|
// FetchHomeOrEmpty 带短缓存;失败时打日志并返回空结构,保证 SSR 页面可渲染。
|
|
func FetchHomeOrEmpty(c *gin.Context) *HomePlayletResponse {
|
|
homeMu.Lock()
|
|
defer homeMu.Unlock()
|
|
if homeCache != nil && time.Since(homeAt) < homeCacheTTL {
|
|
return cloneHome(homeCache)
|
|
}
|
|
|
|
data, err := FetchHome(c)
|
|
if err != nil {
|
|
log.Error("fetch playlet home failed", log.E(err))
|
|
if homeCache != nil {
|
|
return cloneHome(homeCache)
|
|
}
|
|
return &HomePlayletResponse{}
|
|
}
|
|
if data == nil {
|
|
if homeCache != nil {
|
|
return cloneHome(homeCache)
|
|
}
|
|
return &HomePlayletResponse{}
|
|
}
|
|
data.SeoInfo.Normalize()
|
|
homeCache = data
|
|
homeAt = time.Now()
|
|
return cloneHome(homeCache)
|
|
}
|
|
|
|
func cloneHome(src *HomePlayletResponse) *HomePlayletResponse {
|
|
if src == nil {
|
|
return &HomePlayletResponse{}
|
|
}
|
|
out := *src
|
|
if src.Announcements != nil {
|
|
out.Announcements = append([]AnnouncementItem(nil), src.Announcements...)
|
|
}
|
|
if src.HomeBannerData != nil {
|
|
out.HomeBannerData = append([]CommonData(nil), src.HomeBannerData...)
|
|
}
|
|
if src.HomeConfigData != nil {
|
|
out.HomeConfigData = make([]HomeConfigData, len(src.HomeConfigData))
|
|
for i, cfg := range src.HomeConfigData {
|
|
out.HomeConfigData[i] = cfg
|
|
if cfg.SeoInfo.Keywords != nil {
|
|
out.HomeConfigData[i].SeoInfo.Keywords = append([]string(nil), cfg.SeoInfo.Keywords...)
|
|
}
|
|
if cfg.List != nil {
|
|
out.HomeConfigData[i].List = append([]CommonData(nil), cfg.List...)
|
|
}
|
|
if cfg.ThemeList != nil {
|
|
out.HomeConfigData[i].ThemeList = make([]HomeConfigThemeData, len(cfg.ThemeList))
|
|
for j, theme := range cfg.ThemeList {
|
|
out.HomeConfigData[i].ThemeList[j] = theme
|
|
if theme.List != nil {
|
|
out.HomeConfigData[i].ThemeList[j].List = append([]CommonData(nil), theme.List...)
|
|
}
|
|
}
|
|
}
|
|
if cfg.VideoList != nil {
|
|
out.HomeConfigData[i].VideoList = append([]VideoItemResp(nil), cfg.VideoList...)
|
|
}
|
|
}
|
|
}
|
|
if src.HomeTodayUpdateData != nil {
|
|
out.HomeTodayUpdateData = append([]CommonData(nil), src.HomeTodayUpdateData...)
|
|
}
|
|
if src.HomeCategoryData != nil {
|
|
out.HomeCategoryData = make([]HomeCategoryData, len(src.HomeCategoryData))
|
|
for i, cat := range src.HomeCategoryData {
|
|
out.HomeCategoryData[i] = cat
|
|
if cat.List != nil {
|
|
out.HomeCategoryData[i].List = append([]CommonData(nil), cat.List...)
|
|
}
|
|
}
|
|
}
|
|
if src.HomeRecommendThemes != nil {
|
|
out.HomeRecommendThemes = append([]HomeRecommendTheme(nil), src.HomeRecommendThemes...)
|
|
}
|
|
if src.SeoInfo.Keywords != nil {
|
|
out.SeoInfo.Keywords = append([]string(nil), src.SeoInfo.Keywords...)
|
|
}
|
|
return &out
|
|
}
|
|
|
|
// ActiveHomeBanners 过滤未启用 banner;无 showOnBanner 标记时保留全部。
|
|
func ActiveHomeBanners(list []CommonData) []CommonData {
|
|
if len(list) == 0 {
|
|
return list
|
|
}
|
|
hasFlag := false
|
|
for _, item := range list {
|
|
if item.ShowOnBanner {
|
|
hasFlag = true
|
|
break
|
|
}
|
|
}
|
|
if !hasFlag {
|
|
return append([]CommonData(nil), list...)
|
|
}
|
|
out := make([]CommonData, 0, len(list))
|
|
for _, item := range list {
|
|
if item.ShowOnBanner {
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// ActiveHomeConfigs 仅保留上架配置,按接口顺序。
|
|
func ActiveHomeConfigs(list []HomeConfigData) []HomeConfigData {
|
|
if len(list) == 0 {
|
|
return list
|
|
}
|
|
out := make([]HomeConfigData, 0, len(list))
|
|
for _, item := range list {
|
|
if !item.Status {
|
|
continue
|
|
}
|
|
out = append(out, item)
|
|
}
|
|
return out
|
|
}
|
|
|
|
// HomeConfigsByType 上架且 type 匹配的首页配置(保持接口顺序)。
|
|
func HomeConfigsByType(list []HomeConfigData, typ int) []HomeConfigData {
|
|
out := make([]HomeConfigData, 0, len(list))
|
|
for _, item := range ActiveHomeConfigs(list) {
|
|
if item.Type == typ {
|
|
out = append(out, item)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// FirstHomeConfigSEOByType 返回首页第一个已上架指定类型配置的 TDK。
|
|
// 专题更多页没有配置 ID 路径,因此按首页实际展示顺序匹配首个专题配置。
|
|
func FirstHomeConfigSEOByType(list []HomeConfigData, typ int) (SeoInfo, bool) {
|
|
for _, item := range ActiveHomeConfigs(list) {
|
|
if item.Type != typ {
|
|
continue
|
|
}
|
|
seo := item.SeoInfo
|
|
seo.Normalize()
|
|
return seo, true
|
|
}
|
|
return SeoInfo{}, false
|
|
}
|
|
|
|
// FindHomeConfigByIdentity 按路由携带的 ID 或 slug 精确查找已上架首页配置。
|
|
func FindHomeConfigByIdentity(list []HomeConfigData, id int64, slug string) (HomeConfigData, bool) {
|
|
slug = strings.TrimSpace(slug)
|
|
for _, item := range ActiveHomeConfigs(list) {
|
|
if id > 0 {
|
|
if item.ID == id {
|
|
return item, true
|
|
}
|
|
continue
|
|
}
|
|
if slug != "" && strings.EqualFold(strings.TrimSpace(item.Slug), slug) {
|
|
return item, true
|
|
}
|
|
}
|
|
return HomeConfigData{}, false
|
|
}
|
|
|
|
// FindHomeConfigByName 按名称查找上架首页配置(精确匹配 trim 后名称)。
|
|
func FindHomeConfigByName(list []HomeConfigData, name string) (HomeConfigData, bool) {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
return HomeConfigData{}, false
|
|
}
|
|
for _, item := range ActiveHomeConfigs(list) {
|
|
if strings.TrimSpace(item.Name) == name {
|
|
return item, true
|
|
}
|
|
}
|
|
return HomeConfigData{}, false
|
|
}
|
|
|
|
// MoreListURLByHomeConfigName 生成指定首页配置区块的更多页链接。
|
|
func MoreListURLByHomeConfigName(c *gin.Context, name string) string {
|
|
cfg, ok := FindHomeConfigByName(FetchHomeOrEmpty(c).HomeConfigData, name)
|
|
if !ok || cfg.ID <= 0 {
|
|
return "/more-list"
|
|
}
|
|
return MoreListURL(cfg.Slug, cfg.ID, 0, "", 0, 1)
|
|
}
|
|
|
|
var navMegaIcons = []string{
|
|
"/static/images/nav_trend.svg",
|
|
"/static/images/nav_zap.svg",
|
|
"/static/images/nav_star.svg",
|
|
"/static/images/nav_grid.svg",
|
|
}
|
|
|
|
// NavMegaIcon 快捷入口图标,按序号循环。
|
|
func NavMegaIcon(index int) string {
|
|
if len(navMegaIcons) == 0 {
|
|
return "/static/images/nav_star.svg"
|
|
}
|
|
if index < 0 {
|
|
index = 0
|
|
}
|
|
return navMegaIcons[index%len(navMegaIcons)]
|
|
}
|