- 重构京东到家page api,去掉拼接URL

This commit is contained in:
gazebo
2019-07-09 21:20:10 +08:00
parent ac7ea4272d
commit 96733efa33
2 changed files with 54 additions and 18 deletions

View File

@@ -4,7 +4,6 @@ import (
"crypto/md5"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
@@ -153,7 +152,7 @@ func (a *API) GetStoreCookie() string {
return a.storeCookie
}
func (a *API) AccessStorePage(fullURL string, formData map[string]interface{}) (retVal map[string]interface{}, err error) {
func (a *API) AccessStorePage(fullURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
storeCookie := a.GetStoreCookie()
if storeCookie == "" {
return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
@@ -161,10 +160,10 @@ func (a *API) AccessStorePage(fullURL string, formData map[string]interface{}) (
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
if formData == nil {
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
if !isPost {
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", params), nil)
} else {
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(formData).Encode()))
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}
@@ -211,11 +210,16 @@ func (a *API) GetRealMobile4Order(orderId, stationNo string) (mobile string, err
}
func (a *API) GetStoreOrderInfo(orderId, stationNo string) (storeOrderInfo map[string]interface{}, err error) {
urlStr := "http://store.jddj.com/order/newManager/search?pageNo=1&pageSize=1&orderBy=&desc=true&param=" + orderId
if stationNo != "" {
urlStr += "&stationNo=" + stationNo
params := map[string]interface{}{
"pageNo": 1,
"pageSize": 1,
"desc": true,
"param": orderId,
}
retVal, err := a.AccessStorePage(urlStr, nil)
if stationNo != "" {
params["stationNo"] = stationNo
}
retVal, err := a.AccessStorePage("http://store.jddj.com/order/newManager/search", params, false)
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
if err == nil {
newOrderinfoMains := retVal["newOrderinfoMains"].(map[string]interface{})
@@ -231,9 +235,18 @@ func (a *API) GetStoreOrderInfo(orderId, stationNo string) (storeOrderInfo map[s
func (a *API) GetStoreOrderInfoList(fromTime, toTime string) (storeOrderList []map[string]interface{}, err error) {
pageSize := 100
pageNo := 1
urlTemplate := "http://store.jddj.com/order/newManager/tabQuery/all?o2oOrderType=10000&pageNo=%d&pageSize=%d&orderBy=&desc=true&startTimeQuery=%s&endTimeQuery=%s&stationNo="
params := map[string]interface{}{
"o2oOrderType": 10000,
"pageSize": pageSize,
"desc": true,
"startTimeQuery": fromTime,
"endTimeQuery": toTime,
// "stationNo": 0,
}
for {
retVal, err := a.AccessStorePage(fmt.Sprintf(urlTemplate, pageNo, pageSize, url.QueryEscape(fromTime), url.QueryEscape(toTime)), nil)
params["pageNo"] = pageNo
retVal, err := a.AccessStorePage("http://store.jddj.com/order/newManager/tabQuery/all", params, false)
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
if err == nil {
newOrderinfoMains := retVal["newOrderinfoMains"].(map[string]interface{})
@@ -255,7 +268,14 @@ func (a *API) GetSkuPageInfo(skuId int64) (skuPageInfo map[string]interface{}, e
"skuId": utils.Int64ToStr(skuId),
"storeId": "0",
}
skuPageInfo, err = a.AccessStorePage(fmt.Sprintf("https://daojia.jd.com/client?platCode=H5&functionId=product/detailV6_0&body=%s", utils.Format4Output(skuIDMap, true)), nil)
params := map[string]interface{}{
"platCode": "H5",
"functionId": "product/detailV6_0",
"appVersion": "6.7.0",
"body": utils.Format4Output(skuIDMap, true),
}
skuPageInfo, err = a.AccessStorePage("https://daojia.jd.com/client", params, false)
return skuPageInfo, err
}
@@ -271,7 +291,13 @@ func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, er
body := map[string]interface{}{
"storeId": storeId,
}
retVal, err := a.AccessStorePage(fmt.Sprintf("https://daojia.jd.com/client?functionId=store/storeDetailV220&body=%s&appVersion=6.1.0", utils.Format4Output(body, true)), nil)
params := map[string]interface{}{
"appVersion": "6.1.0",
"functionId": "store/storeDetailV220",
"body": utils.Format4Output(body, true),
}
retVal, err := a.AccessStorePage("https://daojia.jd.com/client", params, false)
return retVal, err
}
@@ -298,7 +324,17 @@ func (a *API) GetStoreList(lng string, lat string) (retVal map[string]interface{
}
bodyStr := utils.Format4Output(body, true)
signResult := signGetStoreList(bodyStr)
retVal, err = a.AccessStorePage(fmt.Sprintf("https://daojia.jd.com/client?platCode=h5&appVersion=6.5.0&functionId=zone/recommendStoreList&body=%s&signKey=%s&lng=%s&lat=%s", bodyStr, signResult, lng, lat), nil)
params := map[string]interface{}{
"platCode": "H5",
"appVersion": "6.5.0",
"functionId": "zone/recommendStoreList",
"body": utils.Format4Output(body, true),
"signKey": signResult,
"lng": lng,
"lat": lat,
}
retVal, err = a.AccessStorePage("https://daojia.jd.com/client", params, false)
return retVal, err
}
@@ -306,7 +342,7 @@ func (a *API) GetCorporationInfo(stationNo, qualifyNumber string) (corporatonInf
result, err := a.AccessStorePage("https://sta-store.jddj.com/store/requestQualify.o2o", map[string]interface{}{
"stationNo": stationNo,
"qualifyNumber": qualifyNumber,
})
}, true)
if err == nil {
err = utils.Map2StructByJson(result, &corporatonInfo, false)
}

View File

@@ -48,7 +48,7 @@ func TestGetSkuPageInfo(t *testing.T) {
}
func TestGetSkuPageImageInfo(t *testing.T) {
imgList, err := api.GetSkuPageImageInfo(2023524346)
imgList, err := api.GetSkuPageImageInfo(2025112058)
if err != nil {
t.Fatal(err)
}
@@ -64,7 +64,7 @@ func TestGetCorporationInfo(t *testing.T) {
}
func TestGetStoreList(t *testing.T) {
result, err := api.GetStoreList("104.057218", "30.6949")
result, err := api.GetStoreList("104.054195", "30.581782")
if err != nil {
t.Fatal(err)
}
@@ -80,7 +80,7 @@ func TestGetStoreInfo(t *testing.T) {
}
func TestGetStoreInfo2(t *testing.T) {
result, err := api.GetStoreInfo2("11734879")
result, err := api.GetStoreInfo2("11883852")
if err != nil {
t.Fatal(err)
}