91 lines
2.0 KiB
Go
91 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fanqie/internal/fqapi"
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// HandleSearch 搜索处理
|
|
func HandleSearch(w http.ResponseWriter, r *http.Request) {
|
|
incrementTotal()
|
|
incrementSearch()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
query := r.URL.Query().Get("query")
|
|
if query == "" {
|
|
query = r.URL.Query().Get("key")
|
|
}
|
|
|
|
offsetStr := r.URL.Query().Get("offset")
|
|
offset := 0
|
|
if offsetStr != "" {
|
|
offset, _ = strconv.Atoi(offsetStr)
|
|
}
|
|
|
|
tabTypeStr := r.URL.Query().Get("tab_type")
|
|
tabType := 3
|
|
if tabTypeStr != "" {
|
|
tabType, _ = strconv.Atoi(tabTypeStr)
|
|
}
|
|
|
|
result, err := fqapi.HandleBookSearch(query, offset, tabType)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "搜索失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleDetail 详情处理
|
|
func HandleDetail(w http.ResponseWriter, r *http.Request) {
|
|
incrementTotal()
|
|
incrementDetail()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetDetail(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "获取详情失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleCatalog 目录处理
|
|
func HandleCatalog(w http.ResponseWriter, r *http.Request) {
|
|
incrementTotal()
|
|
incrementCatalog()
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetCatalog(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "获取目录失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|
|
// HandleMultiDetail 批量详情处理
|
|
func HandleMultiDetail(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
bookID := r.URL.Query().Get("book_id")
|
|
|
|
result, err := fqapi.HandleGetMultiDetail(bookID)
|
|
if err != nil {
|
|
fmt.Fprintf(w, `{"code": -1, "msg": "批量获取详情失败: %s"}`, err.Error())
|
|
return
|
|
}
|
|
|
|
fmt.Fprint(w, result)
|
|
}
|
|
|