- store page api for ebai.

This commit is contained in:
gazebo
2019-02-15 17:46:13 +08:00
parent 61d6840bdb
commit b1cf533db6
6 changed files with 178 additions and 3 deletions

View File

@@ -7,6 +7,7 @@ import (
"net/url"
"sort"
"strings"
"sync"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
@@ -19,6 +20,11 @@ const (
signKey = "sign"
secretKey = "secret"
)
const (
ResponseCodeSuccess = 0
)
const (
CmdOrderCreate = "order.create"
CmdOrderDeliveryStatus = "order.deliveryStatus.push"
@@ -40,6 +46,9 @@ type API struct {
client *http.Client
config *platformapi.APIConfig
speedLimiter *platformapi.Limiter
locker sync.RWMutex
storeCookies map[string]string
}
func New(source, secret string, config ...*platformapi.APIConfig) *API {
@@ -54,6 +63,7 @@ func New(source, secret string, config ...*platformapi.APIConfig) *API {
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
speedLimiter: platformapi.New(apiLimitConfigs, defaultAPILimitConfig),
storeCookies: make(map[string]string),
}
return api
}
@@ -105,7 +115,7 @@ func (a *API) AccessAPI(cmd string, body map[string]interface{}) (retVal *Respon
Error: utils.Interface2String(Body["error"]),
Data: Body["data"],
}
if retVal.ErrNo == 0 {
if retVal.ErrNo == ResponseCodeSuccess {
return platformapi.ErrLevelSuccess, nil
}
baseapi.SugarLogger.Debugf("ebai AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))

View File

@@ -30,6 +30,9 @@ func init() {
api = New("62923", "aa4cdc6c1108486b")
// prod
// api = New("34665", "c3db75b754ea2d89")
api.SetStoreCookie("WMUSS", "AADIBAABbDlEpGl47c1EyBFcJSidBTBJHFHZEXyMSdBllJTZ9AUNOKV0tZFB9FlRVM73gEAIHRjBVagwAAHh98X2oPJ34Gal0ofFJBYXZ2Xnc6LCEXWQVnVxs7LDlaKBlFNz9DPCogYyZxJQhoHGVfVRIBa2oFUkEfDm1YZxZwLEwvZMjpB18rjw%7E3CaMQAo")
api.SetStoreCookie("WMSTOKEN", "HwXAAB9SGxnTT8pbEwWRDQsNGB3Y09_PF5rO157QUcoLRQAAAoKlgUEiVTOhIf5LjkCoFpAwwCaAAAUr-GEu-yDBeNAQAAHNneF25-uRjhYtgX4rsAAGHurh8C5GAQAA")
}
func TestTest(t *testing.T) {

View File

@@ -0,0 +1,119 @@
package ebaiapi
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"
storeURL = "https://be.ele.me"
)
func (a *API) SetStoreCookie(key, value string) {
a.locker.Lock()
defer a.locker.Unlock()
a.storeCookies[key] = value
}
func (a *API) GetStoreCookie(key string) string {
a.locker.RLock()
defer a.locker.RUnlock()
return a.storeCookies[key]
}
func (a *API) AccessStorePage(subURL string) (retVal map[string]interface{}, err error) {
a.locker.RLock()
storeCookieLen := len(a.storeCookies)
a.locker.RUnlock()
if storeCookieLen == 0 {
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
}
a.locker.RLock()
for k, v := range a.storeCookies {
request.AddCookie(&http.Cookie{
Name: k,
Value: v,
})
}
a.locker.RUnlock()
return request
},
a.config,
func(jsonResult1 map[string]interface{}) (errLevel string, err error) {
retVal = jsonResult1
code := int(utils.MustInterface2Int64(jsonResult1["errno"]))
if code == ResponseCodeSuccess {
retVal = jsonResult1["data"].(map[string]interface{})
return platformapi.ErrLevelSuccess, nil
}
newErr := utils.NewErrorIntCode(jsonResult1["errmsg"].(string), code)
baseapi.SugarLogger.Debugf("ebai 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["order_basic"].(map[string]interface{})["user_phone_call"].(string), nil
}
return "", err
}
func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) {
retVal, err := a.AccessStorePage(fmt.Sprintf("crm/orderlist?keyword=%s", orderId))
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
if err == nil {
resultList := retVal["order_list"].([]interface{})
if len(resultList) > 0 {
return resultList[0].(map[string]interface{}), nil
}
return nil, fmt.Errorf("不能找到订单:%s的相关信息", orderId)
}
return nil, err
}
func (a *API) GetStoreOrderInfoList(fromTime, toTime string, shopID string, orderStatus int) (storeOrderList []map[string]interface{}, err error) {
// pageSize := 20
pageNo := 1
urlTemplate := "crm/orderlist?start_timestamp=%d&end_timestamp=%d&shop_id=%s"
params := []interface{}{
utils.Str2Time(fromTime).Unix(),
utils.Str2Time(toTime).Unix(),
shopID,
}
if orderStatus >= 0 {
urlTemplate += "&order_status=%d"
params = append(params, orderStatus)
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
for {
retVal, err := a.AccessStorePage(fixedURL + "&page=" + utils.Int2Str(pageNo))
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
if err == nil {
resultList := retVal["order_list"].([]interface{})
storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...)
if len(storeOrderList) >= int(utils.MustInterface2Int64(retVal["order_count"])) {
return storeOrderList, nil
}
pageNo++
} else {
return nil, err
}
}
return nil, err
}

View File

@@ -0,0 +1,42 @@
package ebaiapi
import (
"testing"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
)
func TestGetRealMobileNumber4Order(t *testing.T) {
orderId := "15501080494587"
desiredMobile := "18483673654"
mobile, err := api.GetRealMobile4Order(orderId)
if err != nil {
t.Fatal(err)
}
if mobile != desiredMobile {
t.Fatalf("orderId:%s's mobile is wrong, should be %s, but it's:%s", orderId, desiredMobile, mobile)
}
baseapi.SugarLogger.Debug(mobile)
}
func TestGetStoreOrderInfo(t *testing.T) {
orderId := "15501080494587"
// desiredMobile := "18483673654"
orderInfo, err := api.GetStoreOrderInfo(orderId)
if err != nil {
t.Fatal(err)
}
baseapi.SugarLogger.Debug(utils.Format4Output(orderInfo, false))
}
func TestGetStoreOrderInfoList(t *testing.T) {
orderInfoList, err := api.GetStoreOrderInfoList("2019-02-14 13:00:00", "2019-02-14 15:30:00", "", -1)
if err != nil {
t.Fatal(err)
}
if true {
baseapi.SugarLogger.Debug(utils.Format4Output(orderInfoList, false))
baseapi.SugarLogger.Debug(len(orderInfoList))
}
}

View File

@@ -78,7 +78,8 @@ func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]inter
retVal, err := a.AccessStorePage(fmt.Sprintf("order/newManager/search?pageNo=1&pageSize=1&orderBy=&desc=true&param=%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{})
newOrderinfoMains := retVal["result"].(map[string]interface{})["newOrderinfoMains"].(map[string]interface{})
resultList := newOrderinfoMains["resultList"].([]interface{})
if len(resultList) > 0 {
return resultList[0].(map[string]interface{}), nil
}

View File

@@ -15,7 +15,7 @@ func TestGetRealMobileNumber4Order(t *testing.T) {
t.Fatal(err)
}
if mobile != desiredMobile {
t.Fatalf("orderId:%s's mobile is wrong, should be 18569035610, but it's:%s", orderId, desiredMobile)
t.Fatalf("orderId:%s's mobile is wrong, should be %s, but it's:%s", orderId, desiredMobile, mobile)
}
baseapi.SugarLogger.Debug(mobile)
}