diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 5e6eb907..8535207f 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -7,6 +7,7 @@ import ( "net/http" "sort" "strings" + "sync" "git.rosy.net.cn/baseapi" @@ -52,6 +53,8 @@ const ( signKey = "sign" AllPage = 0 DefaultPageSize = 50 + + storeURL = "http://store.jd.com" ) type API struct { @@ -60,6 +63,9 @@ type API struct { appSecret string client *http.Client config *platformapi.APIConfig + + locker sync.RWMutex + storeCookie string } var ( diff --git a/platformapi/jdapi/jdapi_test.go b/platformapi/jdapi/jdapi_test.go index 764f359a..1c8a7dac 100644 --- a/platformapi/jdapi/jdapi_test.go +++ b/platformapi/jdapi/jdapi_test.go @@ -24,6 +24,8 @@ func init() { jdapi = New("df97f334-f7d8-4b36-9664-5784d8ae0baf", "06692746f7224695ad4788ce340bc854", "d6b42a35a7414a5490d811654d745c84") // prod // jdapi = New("ccb10daf-e6f5-4a58-ada5-b97f9073a137", "1dba76d40cac446ca500c0391a0b6c9d", "a88d031a1e7b462cb1579f12e97fe7f4") + + jdapi.SetStoreCookie("AG37MCRDYAEOMUXKENJQ4QYZIYHYPVUX7CAXNBWPW777UQDYQ6N7HO7XXVQPYJISZMYRMOJXIM4Q35YTUFDGQMW3GQU6625AAJ7IQTUX23DKGA5NOYSENMJHDGM4HO4AHIAWZ7PTJGVBBT5OGAOFABPOHPZRYZ6MH6GL5QTVSIMCARP2IGG6DIB5ZUI5HQSTAJMJVRGRMUM5YT4TVA4NH65A6J6IFPRW7GAESIY") } func TestTest(t *testing.T) { diff --git a/platformapi/jdapi/store_page.go b/platformapi/jdapi/store_page.go new file mode 100644 index 00000000..b19cdf3a --- /dev/null +++ b/platformapi/jdapi/store_page.go @@ -0,0 +1,92 @@ +package jdapi + +import ( + "fmt" + "net/http" + + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/platformapi" + "git.rosy.net.cn/baseapi/utils" +) + +const ( + accessStorePageCookieName = "shop.o2o.jd.com1" +) + +func (a *API) SetStoreCookie(storeCookie string) { + a.locker.Lock() + defer a.locker.Unlock() + a.storeCookie = storeCookie +} + +func (a *API) GetStoreCookie() string { + a.locker.RLock() + defer a.locker.RUnlock() + return a.storeCookie +} + +func (a *API) AccessStorePage(subURL string) (retVal map[string]interface{}, err error) { + storeCookie := a.GetStoreCookie() + if storeCookie == "" { + return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") + } + err = platformapi.AccessPlatformAPIWithRetry(a.client, + func() *http.Request { + fullURL := utils.GenerateGetURL(storeURL, subURL, nil) + // baseapi.SugarLogger.Debug(fullURL) + request, _ := http.NewRequest(http.MethodGet, fullURL, nil) + if err != nil { + return nil + } + request.AddCookie(&http.Cookie{ + Name: accessStorePageCookieName, + Value: storeCookie, + }) + return request + }, + a.config, + func(response *http.Response) (errLevel string, err error) { + jsonResult1, err := utils.HTTPResponse2Json(response) + // baseapi.SugarLogger.Debug(utils.Format4Output(jsonResult1, false)) + if err != nil { + return platformapi.ErrLevelGeneralFail, platformapi.ErrResponseDataFormatWrong + } + retVal = jsonResult1 + code := jsonResult1["code"].(string) + if code == ResponseCodeSuccess { + retVal = jsonResult1 + return platformapi.ErrLevelSuccess, nil + } + newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code) + if _, ok := exceedLimitCodes[code]; ok { + return platformapi.ErrLevelExceedLimit, newErr + } else if _, ok := canRetryCodes[code]; ok { + return platformapi.ErrLevelRecoverableErr, newErr + } else { + baseapi.SugarLogger.Debugf("jd AccessStorePage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + return platformapi.ErrLevelCodeIsNotOK, newErr + } + }) + return retVal, err +} + +func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) { + retVal, err := a.GetStoreOrderInfo(orderId) + if err == nil { + return retVal["mobile"].(string), nil + } + return "", err +} + +func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) { + retVal, err := a.AccessStorePage(fmt.Sprintf("order/newManager/search?pageNo=1&pageSize=1&orderBy=&desc=true¶m=%s&stationNo=", orderId)) + // baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) + if err == nil { + resultList := retVal["result"].(map[string]interface{})["newOrderinfoMains"].(map[string]interface{})["resultList"].([]interface{}) + if len(resultList) > 0 { + return resultList[0].(map[string]interface{}), nil + } + return nil, fmt.Errorf("不能找到订单:%s的相关信息", orderId) + } + return nil, err +} diff --git a/platformapi/jdapi/store_page_test.go b/platformapi/jdapi/store_page_test.go new file mode 100644 index 00000000..5314cc43 --- /dev/null +++ b/platformapi/jdapi/store_page_test.go @@ -0,0 +1,20 @@ +package jdapi + +import ( + "testing" + + "git.rosy.net.cn/baseapi" +) + +func TestGetRealMobileNumber4Order(t *testing.T) { + orderId := "900658736000042" + desiredMobile := "18569035610" + mobile, err := jdapi.GetRealMobile4Order(orderId) + if err != nil { + t.Fatal(err) + } + if mobile != desiredMobile { + t.Fatalf("orderId:%s's mobile is wrong, should be 18569035610, but it's:%s", orderId, desiredMobile) + } + baseapi.SugarLogger.Debug(mobile) +}