🐛 fix(search): 按 tab_type 过滤搜索结果
- 上游 search/tab/v/ 固定返回所有 tab 的 search_tabs,此前原样转发导致传 tab_type 仍返回全部结果 - 新增 filterSearchTabs 只保留匹配项,无匹配时保留原样兜底 - 过滤路径用 UseNumber 解析重组,避免 19 位 book_id 经 float64 丢精度;解析失败退回原样转发 - 新增测试:过滤正确性、无匹配兜底、大整数精度保持
This commit is contained in:
1 parent
bb0cc39950
commit
8bc6f5c2ae
2 files changed
+99
-1
No files matched your search
@@ -1,6 +1,8 @@
|
||||
package fqapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fanqie/internal/common"
|
||||
"fmt"
|
||||
"net/url"
|
||||
@@ -57,9 +59,42 @@ func fetchBookSearch(key string, offset int, tabType int) (string, error) {
|
||||
}
|
||||
|
||||
if code, ok := respJSON["code"].(float64); ok && code == 0 {
|
||||
return resp.Text, nil
|
||||
// 用 UseNumber 重新解析后按 tab_type 过滤:
|
||||
// 上游 search/tab/v/ 固定返回所有 tab 的 search_tabs;
|
||||
// 大整数 ID 经 float64 重组会丢精度,必须 UseNumber
|
||||
dec := json.NewDecoder(bytes.NewReader(resp.Content))
|
||||
dec.UseNumber()
|
||||
var numJSON map[string]interface{}
|
||||
if err := dec.Decode(&numJSON); err != nil {
|
||||
return resp.Text, nil // 解析失败时退回原样转发
|
||||
}
|
||||
filterSearchTabs(numJSON, tabType)
|
||||
return ToJSON(numJSON)
|
||||
}
|
||||
|
||||
return BuildErrorResponseWithCode(1, "搜索失败"), nil
|
||||
}
|
||||
|
||||
// filterSearchTabs 按 tab_type 只保留 search_tabs 中匹配的项;无匹配时保留原样
|
||||
func filterSearchTabs(respJSON map[string]interface{}, tabType int) {
|
||||
tabs, ok := respJSON["search_tabs"].([]interface{})
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
filtered := make([]interface{}, 0, 1)
|
||||
for _, t := range tabs {
|
||||
m, ok := t.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if tt, ok := m["tab_type"].(json.Number); ok {
|
||||
if v, err := tt.Int64(); err == nil && int(v) == tabType {
|
||||
filtered = append(filtered, m)
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(filtered) > 0 {
|
||||
respJSON["search_tabs"] = filtered
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user