Initial commit
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package translationser
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
"videoweb/app/service/upstream"
|
||||
"videoweb/pub/common/log"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const (
|
||||
categoryListPath = "/api/app/translation/category/list"
|
||||
categoryListCacheTTL = 60 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
navMu sync.Mutex
|
||||
navCache []HeaderNavItemVM
|
||||
navAt time.Time
|
||||
)
|
||||
|
||||
// FetchCategoryList 拉取导航分类列表。
|
||||
func FetchCategoryList(c *gin.Context) (*AppTranslationInfo, error) {
|
||||
var env APIEnvelope[AppTranslationInfo]
|
||||
if err := upstream.GetPublicJSON(c, categoryListPath, &env); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if env.Code != 200 {
|
||||
return nil, fmt.Errorf("translation category list code=%d msg=%s", env.Code, env.Msg)
|
||||
}
|
||||
return &env.Data, nil
|
||||
}
|
||||
|
||||
// FetchHeaderNavOrDefault 带短缓存;失败或空列表时返回默认导航。
|
||||
func FetchHeaderNavOrDefault(c *gin.Context) []HeaderNavItemVM {
|
||||
navMu.Lock()
|
||||
defer navMu.Unlock()
|
||||
if navCache != nil && time.Since(navAt) < categoryListCacheTTL {
|
||||
return append([]HeaderNavItemVM(nil), navCache...)
|
||||
}
|
||||
|
||||
info, err := FetchCategoryList(c)
|
||||
if err != nil {
|
||||
log.Error("fetch translation category list failed", log.E(err))
|
||||
return DefaultHeaderNav()
|
||||
}
|
||||
vm := BuildHeaderNav(info)
|
||||
if len(vm) == 0 {
|
||||
return DefaultHeaderNav()
|
||||
}
|
||||
navCache = vm
|
||||
navAt = time.Now()
|
||||
return append([]HeaderNavItemVM(nil), vm...)
|
||||
}
|
||||
Reference in New Issue
Block a user