游标修正

This commit is contained in:
2023-12-07 02:19:34 +08:00
parent 04e1456eaa
commit 5af7f0d5c1
2 changed files with 42 additions and 14 deletions

View File

@@ -6,7 +6,9 @@ import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
)
// 查询参数
@@ -63,16 +65,16 @@ type Response struct {
} `json:"hits"`
}
func (res Response) ToIDList(first, last, after, before int) (id_list []string) {
func (res Response) ToIDList(first, last int, after, before string) (id_list []string) {
for _, hit := range res.Hits.Hits {
fmt.Println(hit)
id_list = append(id_list, hit.ID)
}
// 如果 after 不为 0, 从这个ID开始向后取切片
if after != 0 {
after_str := fmt.Sprint(after)
if after != "" {
for i, id := range id_list {
if id == after_str {
if id == after {
id_list = id_list[i+1:]
break
}
@@ -80,10 +82,9 @@ func (res Response) ToIDList(first, last, after, before int) (id_list []string)
}
// 如果 before 不为 0, 从这个ID开始向前取切片
if before != 0 {
before_str := fmt.Sprint(before)
if before != "" {
for i, id := range id_list {
if id == before_str {
if id == before {
id_list = id_list[:i]
break
}
@@ -121,17 +122,39 @@ func ZincSearch(query map[string]interface{}) (rest Response, err error) {
queryBytes, _ := json.Marshal(query)
req, err := http.NewRequest("POST", zincURL, bytes.NewBuffer(queryBytes))
if err != nil {
log.Println("创建请求失败", err)
return
}
req.Header.Set("Content-type", "application/json")
req.Header.Set("Authorization", "Basic "+bas64encodedCreds)
resp, err := http.DefaultClient.Do(req)
// 创建一个新的 HTTP 客户端
customClient := &http.Client{
Transport: &http.Transport{
Proxy: func(*http.Request) (*url.URL, error) {
return nil, nil
},
},
}
resp, err := customClient.Do(req)
if err != nil {
log.Println("发起请求失败", err)
return
}
defer resp.Body.Close()
// 检查状态码
if resp.StatusCode != http.StatusOK {
log.Println("请求未成功,状态码:", resp.StatusCode)
return rest, fmt.Errorf("请求未成功,状态码:%d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(&rest)
if err != nil {
log.Println("解析响应失败", err)
return
}
return
}