ソースを参照

duoduo-search

k.zhang 4 年 前
コミット
b70eed7bb2

+ 11 - 0
apis/pdd/base.go

@@ -0,0 +1,11 @@
+package pdd
+
+import "github.com/gin-gonic/gin"
+
+//拼多多 多多客接口
+func InitPddDdkRouter(engine *gin.RouterGroup) {
+	v1 := engine.Group("v1")
+	{
+		v1.POST("/pdd/ddk/search", PddDdkGoodsSearch) //多多客  商品查询
+	}
+}

+ 32 - 20
apis/pdd/pdd.ddk.goods.search.go

@@ -4,43 +4,55 @@ import (
 	"duoduo/conf"
 	"duoduo/models/pdd"
 	"duoduo/tools"
+	"duoduo/tools/app"
+
 	"fmt"
 	"github.com/gin-gonic/gin"
-	"sort"
+
+	"time"
 )
 
 func PddDdkGoodsSearch(c *gin.Context) {
 	var inData pdd.PddDdkGoodsSearchRequest
-	confIni, errConf := conf.ConnIni()
-	if errConf != nil {
-		fmt.Println(errConf)
+	var outData pdd.PddDdkGoodsSearchResponse
+	var pddErr pdd.ErrorPddResponse
+
+	confIni, err := conf.ConnIni()
+	if err != nil {
+		app.Error(c, 400, err, err.Error())
+		return
 	}
 
 	inData.ClientId = confIni.MustValue("pdd", "client_id")
-	//inData.ClientSecret = confIni.MustValue("pdd", "client_secret")
 	inData.Type = "pdd.ddk.goods.search"
+	inData.Timestamp = fmt.Sprintf("%d", time.Now().Unix())
+	inData.DataType = "JSON"
 
-	//  https://gw-api.pinduoduo.com/api/router?
-	//  type=pdd.ddk.goods.search&
-	//  data_type=JSON&
-	//  client_id=a5d05569c6584a50bfb6600b2d150bae&timestamp=1620461572&
-	//  sign=8F2C23805C8B8A388AFB512DD3EAD727
+	inData.Sign = ""
 
-	jsonStr, err := tools.JsonMarshal(inData)
+	sign, err := DDKSign(inData)
 	if err != nil {
-		fmt.Print("err = ", err.Error())
+		app.Error(c, 400, err, err.Error())
+		return
 	}
 
-	pddSearch := make(map[string]string)
-	err = tools.JsonUnmarshal(jsonStr, &pddSearch)
+	inData.Sign = sign
+	val, err := PostDDk(inData, nil)
+
 	if err != nil {
-		fmt.Print(err.Error())
+		app.Error(c, 400, err, err.Error())
+		return
 	}
-
-	var ks []string
-	for k := range pddSearch {
-		ks = append(ks, k)
+	err = tools.JsonUnmarshal(val, &outData)
+	if err != nil {
+		err = tools.JsonUnmarshal(val, &pddErr)
+		if err != nil {
+			app.Error(c, 500, err, err.Error())
+			return
+		}
+		app.Error(c, 500, nil, pddErr.ErrorResponse.ErrorMsg)
+		return
 	}
-	sort.Strings(ks)
+	app.OK(c, outData, app.Success)
 
 }

+ 47 - 34
apis/pdd/pdd.ddk.goods.search_test.go

@@ -5,13 +5,13 @@ import (
 	"duoduo/models/pdd"
 	"duoduo/tools"
 	"fmt"
-	"sort"
 	"testing"
 	"time"
 )
 
 func TestPddDdkGoodsSearch(t *testing.T) {
 	var inData pdd.PddDdkGoodsSearchRequest
+	//var outData pdd.PddDdkGoodsSearchResponse
 	confIni, errConf := conf.ConnIni()
 	if errConf != nil {
 		fmt.Println("err1", errConf)
@@ -21,45 +21,58 @@ func TestPddDdkGoodsSearch(t *testing.T) {
 	inData.Type = "pdd.ddk.goods.search"
 	inData.Timestamp = fmt.Sprintf("%d", time.Now().Unix())
 	inData.DataType = "JSON"
+	//inData.CatId = ""
+	inData.Sign = ""
 
-	fmt.Print("inData", inData)
+	//fmt.Print("inData", inData)
+	//
+	//jsonStr, err := tools.JsonMarshal(inData)
+	//if err != nil {
+	//	fmt.Print("err2 = ", err.Error())
+	//}
+	//
+	//fmt.Print(jsonStr)
+	//pddSearch := make(map[string]string)
+	//err = tools.JsonUnmarshal(jsonStr, &pddSearch)
+	//if err != nil {
+	//	fmt.Print("err3=", err.Error())
+	//}
+	//
+	//var ks []string
+	//for k := range pddSearch {
+	//	fmt.Print(k)
+	//	ks = append(ks, k)
+	//}
+	//sort.Strings(ks)
+	//plainText := confIni.MustValue("pdd", "client_secret")
+	////排序后拼接数据
+	//for i := 0; i < len(ks); i++ {
+	//	fmt.Print(ks[i])
+	//	plainText = plainText + ks[i]
+	//	plainText = plainText + pddSearch[ks[i]]
+	//}
 
-	jsonStr, err := tools.JsonMarshal(inData)
-	if err != nil {
-		fmt.Print("err2 = ", err.Error())
-	}
+	//plainText += confIni.MustValue("pdd", "client_secret")
+	//fmt.Print("plainText = ", plainText)
+	//sign := tools.MD5(plainText)
+	//fmt.Print(sign)
 
-	fmt.Print(jsonStr)
-	pddSearch := make(map[string]string)
-	err = tools.JsonUnmarshal(jsonStr, &pddSearch)
+	sign, err := DDKSign(inData)
 	if err != nil {
-		fmt.Print("err3=", err.Error())
+		fmt.Print("err = ", err.Error())
 	}
 
-	var ks []string
-	for k := range pddSearch {
-		fmt.Print(k)
-		ks = append(ks, k)
-	}
-	sort.Strings(ks)
-	plainText := confIni.MustValue("pdd", "client_secret")
-	//排序后拼接数据
-	for i := 0; i < len(ks); i++ {
-		fmt.Print(ks[i])
-		plainText = plainText + ks[i]
-		plainText = plainText + pddSearch[ks[i]]
-	}
+	inData.Sign = sign
+	_, err = PostDDk(inData, nil)
+	//tools.JsonUnmarshal(val,ou)
 
-	plainText += confIni.MustValue("pdd", "client_secret")
-	fmt.Print("plainText = ", plainText)
-	sign := tools.MD5(plainText)
-	fmt.Print(sign)
+	//fmt.Print(val)
 
-	url := "https://gw-api.pinduoduo.com/api/router?type=pdd.ddk.goods.search&data_type=JSON&client_id=" + inData.ClientId + "&" + "timestamp=" + inData.Timestamp + "&" + "sign=" + sign
-	fmt.Print("url = ", url)
-	val, err := DuoDuoGet(url)
-	if err != nil {
-		fmt.Print("err = ", err.Error())
-	}
-	fmt.Print("val = ", val)
+	//url := "https://gw-api.pinduoduo.com/api/router?type=pdd.ddk.goods.search&data_type=JSON&client_id=" + inData.ClientId + "&" + "timestamp=" + inData.Timestamp + "&" + "sign=" + sign
+	//fmt.Print("url = ", url)
+	//val, err := DuoDuoGet(url)
+	//if err != nil {
+	//	fmt.Print("err = ", err.Error())
+	//}
+	//fmt.Print("val = ", val)
 }

+ 91 - 2
apis/pdd/post.go

@@ -8,6 +8,7 @@ import (
 	"fmt"
 	"io/ioutil"
 	"net/http"
+	"sort"
 )
 
 func PostPdd(inputObj interface{}, outputObj interface{}, function string) error {
@@ -18,6 +19,8 @@ func PostPdd(inputObj interface{}, outputObj interface{}, function string) error
 		fmt.Println(err.Error())
 		return err
 	}
+	var jsonStr = []byte(jsonData) //转换二进制
+	buffer := bytes.NewBuffer(jsonStr)
 
 	fmt.Print("jsonData = ", jsonData)
 
@@ -28,9 +31,9 @@ func PostPdd(inputObj interface{}, outputObj interface{}, function string) error
 
 	path := confIni.MustValue("pdd", "api_http")
 	fmt.Print(path)
-	path = "https://gw-api.pinduoduo.com/api/router?type=pdd.ddk.goods.search&data_type=JSON&client_id=a5d05569c6584a50bfb6600b2d150bae&timestamp=1620461572&sign=8F2C23805C8B8A388AFB512DD3EAD727"
+	//path = "https://gw-api.pinduoduo.com/api/router?type=pdd.ddk.goods.search&data_type=JSON&client_id=a5d05569c6584a50bfb6600b2d150bae&timestamp=1620461572&sign=8F2C23805C8B8A388AFB512DD3EAD727"
 
-	request, err := http.NewRequest("POST", path, nil)
+	request, err := http.NewRequest("POST", path, buffer)
 	if err != nil {
 		fmt.Println(err.Error())
 		return err
@@ -50,6 +53,92 @@ func PostPdd(inputObj interface{}, outputObj interface{}, function string) error
 
 }
 
+func PostDDk(inputObj interface{}, outputObj interface{}) (string, error) {
+	//数据转json
+	jsonData, err := tools.JsonMarshal(inputObj)
+	if err != nil {
+		return "", err
+	}
+	fmt.Print(jsonData)
+	var jsonStr = []byte(jsonData) //转换二进制
+	buffer := bytes.NewBuffer(jsonStr)
+	confIni, errConf := conf.ConnIni()
+	if errConf != nil {
+		return "", err
+	}
+
+	path := confIni.MustValue("pdd", "api_http")
+	fmt.Print(path)
+
+	request, err := http.NewRequest("POST", path, buffer)
+	if err != nil {
+		fmt.Println(err.Error())
+		return "", err
+	}
+	request.Header.Set("Content-Type", "application/json;charset=UTF-8")
+	client := http.Client{}
+	//创建客户端
+	resp, err := client.Do(request) //发送请求
+	if err != nil {
+		fmt.Printf("client.Do%v", err)
+		return "", err
+	}
+	respBytes, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		return "", err
+	}
+	fmt.Print(string(respBytes))
+
+	return string(respBytes), nil
+
+}
+
+func DDKSign(inputObj interface{}) (string, error) {
+
+	jsonData, err := tools.JsonMarshal(inputObj)
+	if err != nil {
+		return "", err
+	}
+	pddSearch := make(map[string]string)
+	err = tools.JsonUnmarshal(jsonData, &pddSearch)
+	if err != nil {
+		return "", err
+	}
+
+	confIni, errConf := conf.ConnIni()
+	if errConf != nil {
+		return "", err
+	}
+
+	path := confIni.MustValue("pdd", "api_http")
+	fmt.Print(path)
+
+	var ks []string
+	for k, v := range pddSearch {
+		fmt.Print(k)
+		fmt.Print(v)
+		if v != "" {
+			ks = append(ks, k)
+		}
+
+	}
+	sort.Strings(ks)
+	plainText := confIni.MustValue("pdd", "client_secret")
+	//排序后拼接数据
+	for i := 0; i < len(ks); i++ {
+		fmt.Print(ks[i])
+		plainText = plainText + ks[i]
+		plainText = plainText + pddSearch[ks[i]]
+	}
+
+	plainText += confIni.MustValue("pdd", "client_secret")
+	fmt.Print("plainText = ", plainText)
+	sign := tools.MD5(plainText)
+	fmt.Print(sign)
+
+	return sign, nil
+}
+
 //调用阿里接口
 func Post(inputObj interface{}, outputObj interface{}, function string) error {
 	//加载配置

+ 12 - 0
models/pdd/base.go

@@ -0,0 +1,12 @@
+package pdd
+
+type ErrorPddResponse struct {
+	ErrorResponse ErrorResponse `json:"error_response"`
+}
+type ErrorResponse struct {
+	ErrorMsg  string `json:"error_msg"`
+	SubMsg    string `json:"sub_msg"`
+	SubCode   string `json:"sub_code"`
+	ErrorCode int    `json:"error_code"`
+	RequestID string `json:"request_id"`
+}

+ 2 - 0
models/pdd/pdd.ddk.goods.search.go

@@ -6,6 +6,8 @@ type PddDdkGoodsSearchRequest struct {
 	Type      string `json:"type"`
 	Timestamp string `json:"timestamp"`
 	DataType  string `json:"data_type"`
+	Sign      string `json:"sign"`
+	//CatId     string `json:"catId"`
 }
 
 type PddDdkGoodsSearchResponse struct {

+ 5 - 1
routers/router.go

@@ -1,6 +1,7 @@
 package routers
 
 import (
+	"duoduo/apis/pdd"
 	"github.com/gin-gonic/gin"
 	"net/http"
 )
@@ -9,7 +10,10 @@ func InitRouter() {
 
 	r := gin.New()
 	r.GET("/", func(c *gin.Context) { c.String(http.StatusOK, "hello!") })
-
+	r2 := r.Group("/")
+	{
+		pdd.InitPddDdkRouter(r2)
+	}
 	_ = r.Run(":8080")
 
 }

+ 4 - 3
tools/app/model.go

@@ -1,7 +1,8 @@
 package app
 
 type Response struct {
-	ApiBaseOutputHead ApiBaseOutputHead `json:"apiBaseOutputHead"`
+	Code    int    `json:"code"`
+	Message string `json:"message"`
 	// 数据集
 	Data interface{} `json:"data"`
 }
@@ -46,12 +47,12 @@ type PageRes struct {
 }
 
 func (res *Response) ReturnOK() *Response {
-	res.ApiBaseOutputHead.Code = 200
+	res.Code = 200
 	return res
 }
 
 func (res *Response) ReturnError(code int) *Response {
-	res.ApiBaseOutputHead.Code = code
+	res.Code = code
 	return res
 }
 

+ 2 - 2
tools/app/return.go

@@ -30,10 +30,10 @@ import (
 func Error(c *gin.Context, code int, err error, msg string) {
 	var res Response
 	if err != nil {
-		res.ApiBaseOutputHead.Msg = err.Error()
+		res.Message = err.Error()
 	} else {
 		if msg != "" {
-			res.ApiBaseOutputHead.Msg = msg
+			res.Message = msg
 		}
 	}
 	c.JSON(http.StatusOK, res.ReturnError(code))