daShangDao_kfzgw-info/kfztp.go

136 lines
3.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"github.com/parnurzeal/gorequest"
)
// 获取商品模版
func outGetGoodsTplMsgLinux(token, itemId, proxy string) (map[string]interface{}, error) {
if token == "" {
return nil, fmt.Errorf("请先登录获取Token")
}
url := fmt.Sprintf("https://seller.kongfz.com/pc/itemInfo/getTplFields?itemId=%s&isClone=1&v=%d",
itemId, time.Now().Unix())
log.Printf("请求URL: %s", url)
// 创建HTTP客户端
request := gorequest.New()
// 设置代理如果有提供代理URL
if proxy != "" {
request.Proxy(proxy)
log.Printf("使用代理: %s", proxy)
}
resp, body, errs := request.Get(url).
Set("Cookie", fmt.Sprintf("PHPSESSID=%s", token)).
Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36").
Set("Accept", "application/json, text/plain, */*").
Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8").
Set("Referer", "https://seller.kongfz.com/").
Set("Origin", "https://seller.kongfz.com").
Timeout(30 * time.Second).
End()
if len(errs) > 0 {
return nil, fmt.Errorf("请求失败: %v", errs)
}
// 检查HTTP状态码
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP错误: %d - %s", resp.StatusCode, resp.Status)
}
var data map[string]interface{}
if err := json.Unmarshal([]byte(body), &data); err != nil {
return nil, fmt.Errorf("解析JSON失败: %w", err)
}
// 检查API响应状态
if val, ok := data["status"].(float64); ok && val == 1 {
return data, nil
}
return nil, fmt.Errorf("API返回错误: %+v", data)
}
//func main() {
// fmt.Println("=== 孔夫子商品模板API服务 ===")
//
// http.HandleFunc("/api/goodsTemplate", getGoodsTemplateHandler)
// port := "8989"
// server := &http.Server{
// Addr: ":" + port,
// Handler: nil,
// }
// // 启动服务器
// if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
// fmt.Printf("服务器启动失败: %v\n", err)
// }
//}
// 获取商品模板接口
func getGoodsTemplateHandler(w http.ResponseWriter, r *http.Request) {
// 设置响应头
w.Header().Set("Content-Type", "application/json")
// 只处理GET请求
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "只支持GET请求",
})
return
}
// 获取查询参数
token := r.URL.Query().Get("token")
itemId := r.URL.Query().Get("itemId")
proxy := r.URL.Query().Get("proxy")
// 验证必需参数
if token == "" || itemId == "" {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": "缺少必需参数: token 和 itemid",
"usage": map[string]string{
"token": "登录Token (PHPSESSID)",
"itemid": "商品ID",
"proxy": "代理地址 (可选)",
},
"example": "GET /api/goods-template?token=abc123&itemid=123456&proxy=http://proxy:8080",
})
return
}
log.Printf("收到请求 - 商品ID: %s", itemId)
// 调用函数获取商品模板
result, err := outGetGoodsTplMsgLinux(token, itemId, proxy)
if err != nil {
log.Printf("获取商品模板失败: %v", err)
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]interface{}{
"success": false,
"error": err.Error(),
})
return
}
// 返回成功响应
json.NewEncoder(w).Encode(map[string]interface{}{
"success": true,
"data": result,
})
}