Files
huangguo_server/common/laosiji/comics.go
T
rootandClaude Opus 5 8679200f41 Initial commit
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 13:57:10 +08:00

115 lines
2.6 KiB
Go

package laosiji
import (
"91porn-server/common/log"
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"time"
)
func getComicsSearchUrl() string {
return fmt.Sprintf("%s/lsjapi/comics/search", APIUrl)
}
func getComicsDetailUrl() string {
return fmt.Sprintf("%s/lsjapi/comics/detail", APIUrl)
}
func ComicsSearch(ctx context.Context, req ComicsSearchListReq) (resp ComicsSearchListResp, err error) {
endpoint := getComicsSearchUrl()
req.Need_total_info = "y"
var data map[string]interface{}
data, err = StructToMapViaJSON(req)
if err != nil {
log.Error("GenerateUndress post fail", log.E(err))
return resp, err
}
err = comicsPost(ctx, endpoint, data, &resp)
if err != nil {
log.Error("GenerateUndress post fail", log.Any("data", data), log.E(err))
}
return
}
func ComicsDetail(ctx context.Context, req ComicsDetailReq) (resp ComicsDetailResp, err error) {
endpoint := getComicsDetailUrl()
var data map[string]interface{}
data, err = StructToMapViaJSON(req)
if err != nil {
log.Error("GenerateUndress post fail", log.E(err))
return resp, err
}
err = comicsPost(ctx, endpoint, data, &resp)
if err != nil {
log.Error("GenerateUndress post fail", log.Any("data", data), log.E(err))
}
return
}
func comicsPost(ctx context.Context, endpoint string, req map[string]interface{}, response interface{}) (err error) {
jsonData, err := json.Marshal(req)
if err != nil {
log.Warn("GenerateAiUndress", log.E(err))
return
}
encryptedData, err := encryptBase64(string(jsonData), APIKey)
if err != nil {
log.Warn("GenerateAiUndress encryptBase64", log.E(err))
return
}
// 创建 POST 请求
httpReq, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer([]byte(encryptedData)))
if err != nil {
return
}
httpReq.Header.Set("Content-Type", "application/json")
httpReq.Header.Set("Content-Length", fmt.Sprintf("%d", len(encryptedData)))
httpReq.Header.Set("appid", Appid)
// 发送请求
client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Do(httpReq)
if err != nil {
return
}
defer resp.Body.Close()
// 读取响应体
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
// 检查 HTTP 状态码
if resp.StatusCode != http.StatusOK {
return
}
// 解析 JSON
var res Response
if err = json.Unmarshal(body, &res); err != nil {
return
}
if res.Status != "y" {
err = fmt.Errorf("errorCode:%v error:%v", res.ErrorCode, res.Error)
return
}
// 进行解密
dataStr, err := decryptBase64(res.Data, APIKey)
if err != nil {
return
}
err = json.Unmarshal([]byte(dataStr), response)
if err != nil {
return
}
return
}