✨ feat(api): 新增榜单书架历史与搜索续传并重构正文缓存链路
- 新增 /rank 免签名榜单接口(多域名容错)、/bookshelf 与 /read-history(sessionid 登录态,书架支持分组过滤) - 搜索重写:翻页透传上游 passback/search_id/next_offset/corrected_query 并按 book_id 去重,15~20 位纯数字关键词直查详情(ID 直搜) - 删除 /login/check 登录校验端点(login.go、路由与文档同步移除,sessionid 说明改为直接供书架/历史使用) - 正文链路重构:singleflight 合并同章节并发回源防击穿,缓存与解析解耦统一在出口处理,decodeContentBody 按 crypt_status 分支并兜底 500003 明文 - 缓存层:SQL 持久层 TTL 与业务 TTL 解耦(不可变内容过期后由 SQL 回填),动态短缓存只走 Redis,withCache 合并并发 miss - 密钥池:KeyTs 随正文请求上传降低密钥不匹配重试,密钥超 24 小时异步刷新并防刷新风暴,last_used_at 批量落库 - 签名补 X-Helios(与 X-Ladon 同值,对齐书源实现);设备注册版本号统一走 ClientVersionCode/Name - 分类 landing 透传新版标签 selected_items;multi-detail 批量详情供书架/历史复用 - README 与 openapi.yaml 补充新接口文档
This commit is contained in:
1 parent
8ca372050b
commit
84bd3238ab
28 files changed
+1180
-270
No files matched your search
@@ -0,0 +1,77 @@
|
||||
package fqapi
|
||||
|
||||
import (
|
||||
"fanqie/internal/common"
|
||||
"fmt"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var rankLogger = common.NewLogger("rank", "INFO", "")
|
||||
|
||||
// rankSDKHosts 免签名榜单接口的可用域名(依次容错)
|
||||
var rankSDKHosts = []string{
|
||||
"https://novel.snssdk.com",
|
||||
"https://api.fanqiesdk.com",
|
||||
"https://api-lf.fanqiesdk.com",
|
||||
}
|
||||
|
||||
const rankSDKUA = "com.ss.android.article.news/7.0.3 (Linux; U; Android 12; zh_CN; Pixel 6)"
|
||||
|
||||
// RankTypes 榜单类型
|
||||
var RankTypes = map[int]string{
|
||||
10: "推荐榜",
|
||||
11: "完结榜",
|
||||
12: "热搜榜",
|
||||
13: "黑马榜",
|
||||
15: "爆更榜",
|
||||
}
|
||||
|
||||
// HandleGetRank 处理获取榜单,榜单数据动态,短缓存 10 分钟
|
||||
// sideType 见 RankTypes(无效值回退推荐榜);page 从 1 起,每页 20 本
|
||||
func HandleGetRank(sideType, page int) (string, error) {
|
||||
if _, ok := RankTypes[sideType]; !ok {
|
||||
sideType = 10
|
||||
}
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
return withCache(fmt.Sprintf("rank_%d_%d", sideType, page), 600, func() (string, error) {
|
||||
return fetchRank(sideType, page)
|
||||
})
|
||||
}
|
||||
|
||||
// fetchRank 免签名 SDK 接口获取榜单(无设备/签名依赖,多域名容错)
|
||||
func fetchRank(sideType, page int) (string, error) {
|
||||
vals := url.Values{
|
||||
"aid": {"13"},
|
||||
"type": {"1"},
|
||||
"side_type": {fmt.Sprintf("%d", sideType)},
|
||||
"offset": {fmt.Sprintf("%d", page-1)},
|
||||
"limit": {"20"},
|
||||
}
|
||||
path := "/api/novel/channel/homepage/rank/rank_list/v2/?" + vals.Encode()
|
||||
|
||||
var lastErr error
|
||||
client := common.NewHTTPClient()
|
||||
for _, host := range rankSDKHosts {
|
||||
resp, err := client.Get(host+path, map[string]string{
|
||||
"User-Agent": rankSDKUA,
|
||||
"Accept": "application/json, text/plain, */*",
|
||||
})
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
continue
|
||||
}
|
||||
respJSON, err := resp.JSON()
|
||||
if err != nil {
|
||||
lastErr = fmt.Errorf("响应解析失败: %w", err)
|
||||
continue
|
||||
}
|
||||
if code, ok := respJSON["code"].(float64); ok && code == 0 {
|
||||
return reencodeUnwrapped(resp.Text), nil
|
||||
}
|
||||
lastErr = fmt.Errorf("code=%.0f msg=%v", respJSON["code"], respJSON["message"])
|
||||
}
|
||||
rankLogger.Error(fmt.Sprintf("获取榜单失败: %v", lastErr))
|
||||
return BuildErrorResponse("获取榜单失败"), lastErr
|
||||
}
|
||||
Reference in new issue
Block a user