✨ 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:
chenxin committed 2026-10-12 14:49:14 +08:00
1 parent 8ca372050b
commit 84bd3238ab
28 files changed
+1180 -270

No files matched your search

+14 -49
View File
@@ -1,14 +1,9 @@
package fqapi
import (
"bytes"
"compress/zlib"
"encoding/base64"
"encoding/json"
"fanqie/internal/common"
"fmt"
"io"
"sort"
"strings"
"sync"
)
@@ -17,57 +12,29 @@ var (
multiContentLogger = common.NewLogger("multi_content", "INFO", "")
)
// handleDecryptTask 处理解密任务
// handleDecryptTask 处理解密任务(crypt_status 分支见 decodeContentBody)
func handleDecryptTask(itemID string, itemData map[string]interface{}, keyInfo *KeyInfo, textMode, imageMode bool) (string, map[string]interface{}) {
if code, ok := itemData["code"].(float64); !ok || code != 0 {
return itemID, itemData
}
contentEncoded, ok := itemData["content"].(string)
if !ok {
if _, ok := itemData["content"].(string); !ok {
return itemID, itemData
}
// 解码content
contentBytes, err := base64.StdEncoding.DecodeString(contentEncoded)
if err != nil || len(contentBytes) < 16 {
// 解密(含压缩与明文分支)
contentStr, err := decodeContentBody(itemData, keyInfo)
if err != nil || contentStr == "" {
// 解密失败保留上游原样(ignore_failed 默认跳过失败章节)
return itemID, itemData
}
iv := contentBytes[0:16]
encryptedData := contentBytes[16:]
// 解密
if keyInfo == nil {
return itemID, itemData
}
keyBytes, _ := base64.StdEncoding.DecodeString(keyInfo.Key)
decrypted, err := De(encryptedData, keyBytes, iv)
if err != nil {
return itemID, itemData
}
// 检查是否需要解压
compressStatus, _ := itemData["compress_status"].(float64)
if compressStatus != 0 {
// zlib解压
reader, err := zlib.NewReader(bytes.NewReader(decrypted))
if err == nil {
decompressed, err := io.ReadAll(reader)
reader.Close()
if err == nil {
decrypted = decompressed
}
}
}
// 更新内容
bodyCopy := make(map[string]interface{})
for k, v := range itemData {
bodyCopy[k] = v
}
bodyCopy["content"] = string(decrypted)
bodyCopy["content"] = contentStr
// 缓存
cacheData := buildCacheData(bodyCopy)
@@ -75,7 +42,7 @@ func handleDecryptTask(itemID string, itemData map[string]interface{}, keyInfo *
// 解析内容
parser := NewContentParser()
bodyCopy["content"] = parser.Parse(string(decrypted), textMode, imageMode)
bodyCopy["content"] = parser.Parse(contentStr, textMode, imageMode)
return itemID, bodyCopy
}
@@ -121,10 +88,11 @@ func MultiContent(itemIDs interface{}, bookID string, textMode, imageMode, ignor
currentKeyInfo := &KeyInfo{
Key: item.Key,
Ver: item.KeyVer,
Ts: item.KeyTs,
}
multiContentLogger.Debug(fmt.Sprintf("使用池中设备 ID=%d (depth: %d)", item.ID, depth))
// 构建参数
// 构建参数(form 编码,见 buildSortedQuery 注释)
headerWithoutVersion := removeVersionFromHeader(header)
params := []string{
@@ -133,18 +101,15 @@ func MultiContent(itemIDs interface{}, bookID string, textMode, imageMode, ignor
"channel=0",
"device_platform=android",
"os_version=0",
"version_code=66.9",
"version_code=" + ClientVersionCode,
"book_id=" + bookID,
"item_ids=" + itemIDStr,
"novel_text_type=1",
"req_type=1",
fmt.Sprintf("key_register_ts=%d", currentKeyInfo.Ts),
headerWithoutVersion,
}
allParams := strings.Join(params, "&")
paramList := strings.Split(allParams, "&")
sort.Strings(paramList)
sortedParams := strings.Join(paramList, "&")
sortedParams := buildSortedQuery(params...)
url := "https://reading.snssdk.com/reading/reader/batch_full/v/?" + sortedParams
@@ -205,7 +170,7 @@ func MultiContent(itemIDs interface{}, bookID string, textMode, imageMode, ignor
multiContentLogger.Warning("密钥版本不匹配,重新获取密钥")
newKeyInfo, err := GetKey(header)
if err == nil {
UpdateKeyInPool(header, newKeyInfo.Key, newKeyInfo.Ver)
UpdateKeyInPool(header, newKeyInfo)
} else {
// 获取密钥失败,切换设备
multiContentLogger.Warning(fmt.Sprintf("获取密钥失败,切换设备重试 (depth: %d)", depth))