From 6b4ee9a49b7be7d021c71cd098aeef6c9e75e5c2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E8=8B=8F=E5=B0=B9=E5=B2=9A?= <770236076@qq.com>
Date: Fri, 29 May 2020 18:23:19 +0800
Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E5=9B=A2=E9=85=8D=E9=80=81cookie?=
=?UTF-8?q?=E8=AE=BE=E7=BD=AE?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
platformapi/jdshopapi/jxshopapi_test.go | 2 +-
platformapi/jdshopapi/store_page.go | 31 ++++++++++----
platformapi/jdshopapi/store_page_test.go | 7 ++--
platformapi/mtpsapi/mtpsapi.go | 8 ++--
platformapi/mtpsapi/mtpsapi_test.go | 2 +-
platformapi/mtpsapi/shop_page.go | 53 ++++++++++++++++++++++++
platformapi/mtpsapi/shop_page_test.go | 8 ++++
7 files changed, 95 insertions(+), 16 deletions(-)
diff --git a/platformapi/jdshopapi/jxshopapi_test.go b/platformapi/jdshopapi/jxshopapi_test.go
index 74304db3..566034e9 100644
--- a/platformapi/jdshopapi/jxshopapi_test.go
+++ b/platformapi/jdshopapi/jxshopapi_test.go
@@ -16,6 +16,6 @@ func init() {
baseapi.Init(sugarLogger)
api = New("de8157b447584885910f429011e49cb93yjq", "E1D746D42474D5F1F1A10CECE75D99F6", "efa7e1d1a22640fa990e6cf164b28608")
api.SetCookieWithStr(`
- thor=80FAF09E9A09B6E618A68057BDFCFCB88A0E4CE7743FBEC84F10D992F9C6A4119DF98DA3CAAE9C7F17BEB62884625B4E7BC82422A90F45F02EA293572D951B055EF0B5F603AEA568DFD4234138F841EC1AC1F67B30B48AAC9EAD5FBAE7943E1DCC99E99D8358C82F7832B71A2BCB31624E16BBF561720443DE966BDA3588406233A90224D9089710B102AA98B979B9B3;
+ thor=00513FC363111586B2C0E065A90E33C1356AC072AA79A28628FF01BE8B2903995775FF61D2F25840E8BAFB9ED95AF7978DAEDCE30A5A110010D64FEA48F85BB83677488A7432DE90F646CFF6D42A4BAD652ACAF7AE179C5C698CBB31F65EFECCAD53871820D05835E7F794768F7F046E75BE2E9350A2E78AC98970EEB4FCBD6EE51EF9DB61864A5777850F88564E7D42;
`)
}
diff --git a/platformapi/jdshopapi/store_page.go b/platformapi/jdshopapi/store_page.go
index 960642a9..8609df97 100644
--- a/platformapi/jdshopapi/store_page.go
+++ b/platformapi/jdshopapi/store_page.go
@@ -13,8 +13,14 @@ import (
)
var (
- regexpOrderDetailTable = regexp.MustCompile(`
`)
- regexpOrderDetailTd = regexp.MustCompile(`(.*?) | `)
+ regexpOrderDetailTable = regexp.MustCompile(``)
+ regexpOrderDetailTd = regexp.MustCompile(`(.*?) | `)
+ regexpOrderDetailMobile = regexp.MustCompile(`(.*?)`)
+)
+
+const (
+ JdsOrderDeliverTypeStore = 1274
+ JdsOrderDeleverTypeSelf = 332098
)
func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
@@ -369,6 +375,7 @@ func (a *API) AllOrders(allOrdersParam *AllOrdersParam) (allOrdersResult *AllOrd
"orderCreateDateRange": allOrdersParam.OrderCreateDateRange,
// "storeId": allOrdersParam.StoreID,
"orderStatusArray": allOrdersParam.OrderStatusArray,
+ "orderId": allOrdersParam.OrderID,
})
if err == nil {
utils.Map2StructByJson(result, &allOrdersResult, false)
@@ -376,23 +383,33 @@ func (a *API) AllOrders(allOrdersParam *AllOrdersParam) (allOrdersResult *AllOrd
return allOrdersResult, err
}
+type OrderDetailResult struct {
+ ConsigneeName string `json:"consigneeName"`
+ ConsigneeAddress string `json:"consigneeAddress"`
+ ConsigneeMobile string `json:"consigneeMobile"`
+}
+
//订单详情
//https://neworder.shop.jd.com/order/orderDetail?orderId=122367441996
-func (a *API) OrderDetail(orderId string) (consigneeName, consigneeAddress string, err error) {
+func (a *API) OrderDetail(orderId string) (orderDetailResult *OrderDetailResult, err error) {
result, err := a.AccessStorePage("https://neworder.shop.jd.com/order/orderDetail", map[string]interface{}{
"orderId": orderId,
}, false)
if err == nil {
+ orderDetailResult = &OrderDetailResult{}
body := result["fakeData"].(string)
consigneeTable := regexpOrderDetailTable.FindStringSubmatch(body)
- fmt.Println("test1", consigneeTable)
if len(consigneeTable) > 0 {
consigneeTd := regexpOrderDetailTd.FindAllStringSubmatch(consigneeTable[1], -1)
+ consigneeMobiles := regexpOrderDetailMobile.FindStringSubmatch(consigneeTable[1])
if len(consigneeTd) > 0 {
- consigneeName = consigneeTd[0][1]
- consigneeAddress = consigneeTd[1][1]
+ orderDetailResult.ConsigneeName = consigneeTd[0][1]
+ orderDetailResult.ConsigneeAddress = consigneeTd[1][1]
+ }
+ if len(consigneeMobiles) > 0 {
+ orderDetailResult.ConsigneeMobile = consigneeMobiles[1]
}
}
}
- return consigneeName, consigneeAddress, err
+ return orderDetailResult, err
}
diff --git a/platformapi/jdshopapi/store_page_test.go b/platformapi/jdshopapi/store_page_test.go
index dc007e06..76f2a2f3 100644
--- a/platformapi/jdshopapi/store_page_test.go
+++ b/platformapi/jdshopapi/store_page_test.go
@@ -58,8 +58,7 @@ func Test11(t *testing.T) {
func TestAllOrders(t *testing.T) {
result, err := api.AllOrders(&AllOrdersParam{
Current: 1,
- PageSize: 10,
- StoreID: "24339648",
+ PageSize: 1,
})
if err != nil {
t.Fatal(err)
@@ -68,9 +67,9 @@ func TestAllOrders(t *testing.T) {
}
func TestOrderDetail(t *testing.T) {
- result1, result2, err := api.OrderDetail("122367441996")
+ result1, result2, result3, err := api.OrderDetail("122367441996")
if err != nil {
t.Fatal(err)
}
- fmt.Println("test1", result1, result2)
+ fmt.Println("test1", result1, result2, result3)
}
diff --git a/platformapi/mtpsapi/mtpsapi.go b/platformapi/mtpsapi/mtpsapi.go
index 302ebd55..9c392ee9 100644
--- a/platformapi/mtpsapi/mtpsapi.go
+++ b/platformapi/mtpsapi/mtpsapi.go
@@ -182,6 +182,7 @@ type API struct {
appKey string
secret string
+ token string
client *http.Client
config *platformapi.APIConfig
}
@@ -227,9 +228,9 @@ func (a *API) signParams(params url.Values) string {
}
func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{}) (retVal *ResponseResult, err error) {
- if params == nil {
- panic("params is nil!")
- }
+ // if params == nil {
+ // panic("params is nil!")
+ // }
params2 := utils.Map2URLValues(params)
if baseURL == mtpsAPIURL {
@@ -248,6 +249,7 @@ func (a *API) AccessAPI2(baseURL, action string, params map[string]interface{})
request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
request.Header.Set("Referer", "https://page.peisong.meituan.com/open/admin/poilist")
if baseURL != mtpsAPIURL {
+ request.Header.Set("csrfToken", a.token)
a.FillRequestCookies(request)
}
return request
diff --git a/platformapi/mtpsapi/mtpsapi_test.go b/platformapi/mtpsapi/mtpsapi_test.go
index d6d1f5c9..554de098 100644
--- a/platformapi/mtpsapi/mtpsapi_test.go
+++ b/platformapi/mtpsapi/mtpsapi_test.go
@@ -25,7 +25,7 @@ func init() {
// prod
// api = New("3c0a05d464c247c19d7ec13accc78605", "b1M}9?:sTbsB[OF2gNORnN(|(iy9rB8(`7]|[wGLnbmt`evfM>E:A90DjHAW:UPE")
- api.SetCookie("token", "GsfR99YCT8leEBnY39YxPeWTJiSmetA3NGl8G8u1Mv29V4KLYIA9rH3fhw-uDL7VwM4jKPfNwH8D_vOPg3cRYg")
+ api.SetCookie("token", "oo3x6pAld4SskzvbEOnSslWUUQwc6w2m7uCfppCEnvRWzHVqctujL0xzLCkro6qj7mowU51zl99ExVUxgJjJ_Q")
}
func handleError(t *testing.T, err error) {
diff --git a/platformapi/mtpsapi/shop_page.go b/platformapi/mtpsapi/shop_page.go
index e9357ac7..8b8eb4c0 100644
--- a/platformapi/mtpsapi/shop_page.go
+++ b/platformapi/mtpsapi/shop_page.go
@@ -2,10 +2,52 @@ package mtpsapi
import (
"fmt"
+ "net/http"
+ "strings"
+ "git.rosy.net.cn/baseapi"
+ "git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
+func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
+ if a.GetCookieCount() == 0 {
+ return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法")
+ }
+ err = platformapi.AccessPlatformAPIWithRetry(a.client,
+ func() *http.Request {
+ var request *http.Request
+ if isPost {
+ request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(bizParams).Encode()))
+ request.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
+ request.Header.Set("Referer", "https://page.peisong.meituan.com/open/admin/poilist")
+ } else {
+ request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil)
+ }
+ a.FillRequestCookies(request)
+ return request
+ },
+ a.config,
+ func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) {
+ if jsonResult1 == nil {
+ return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
+ }
+ if strings.Contains(bodyStr, "登录") || strings.Contains(bodyStr, "访问的内容") {
+ return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了!")
+ }
+ if err == nil {
+ if jsonResult1["error_response"] != nil {
+ errLevel = platformapi.ErrLevelGeneralFail
+ err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string))
+ baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
+ }
+ retVal = jsonResult1
+ }
+ return errLevel, err
+ })
+ return retVal, err
+}
+
func (a *API) PagePoiUpdate(outerPoiID, contactName, contactPhone, contactEmail string) (err error) {
if outerPoiID == "" || contactName == "" || contactPhone == "" || contactEmail == "" {
return fmt.Errorf("所有参数必须都要有值")
@@ -16,10 +58,21 @@ func (a *API) PagePoiUpdate(outerPoiID, contactName, contactPhone, contactEmail
"contactPhone": contactPhone,
"contactEmail": contactEmail,
}
+ if token, err := a.RefreshToken(); err == nil {
+ a.token = token
+ }
_, err = a.AccessAPI2("https://page.peisong.meituan.com/api", "haikuiopen/haikui/open/partner/poi/update", params)
return err
}
+func (a *API) RefreshToken() (token string, err error) {
+ result, err := a.AccessStorePage("https://peisong.meituan.com/api/haikuiopen/haikui/open/auth/csrf/token/refresh", nil, true)
+ if err == nil {
+ token = result["tokens"].(map[string]interface{})["csrfToken"].(string)
+ }
+ return token, err
+}
+
func (a *API) GetAccountDetail() (err error) {
params := map[string]interface{}{}
_, err = a.AccessAPI2("https://peisong.meituan.com/api", "haikuiopen/haikui/open/partner/base/detail", params)
diff --git a/platformapi/mtpsapi/shop_page_test.go b/platformapi/mtpsapi/shop_page_test.go
index 16c832e0..cc99541f 100644
--- a/platformapi/mtpsapi/shop_page_test.go
+++ b/platformapi/mtpsapi/shop_page_test.go
@@ -27,3 +27,11 @@ func TestGetStoreStatus(t *testing.T) {
}
t.Log(utils.Format4Output(result, false))
}
+
+func TestRefreshToken(t *testing.T) {
+ result, err := api.RefreshToken()
+ if err != nil {
+ t.Fatal(err)
+ }
+ t.Log(utils.Format4Output(result, false))
+}