- 重构ebaiapi.StorePage相关API,不拼接URL

This commit is contained in:
gazebo
2019-07-09 20:21:08 +08:00
parent ab2fcd5c9b
commit ac7ea4272d
3 changed files with 84 additions and 88 deletions

View File

@@ -1,8 +1,6 @@
package ebaiapi package ebaiapi
import ( import (
"fmt"
"git.rosy.net.cn/baseapi/utils" "git.rosy.net.cn/baseapi/utils"
) )
@@ -90,16 +88,15 @@ type PageActivityInfo struct {
func (a *API) BegetActivityList(supplierID int64, showStatus, activityType int) (actList []*PageActItem, err error) { func (a *API) BegetActivityList(supplierID int64, showStatus, activityType int) (actList []*PageActItem, err error) {
pageSize := maxPageSize4ActSkuList pageSize := maxPageSize4ActSkuList
pageNo := 1 pageNo := 1
urlTemplate := "commodity/activity/begetactivitylist?perpage=%d&supplier_id=%d&show_status=%d&activity_type=%d" params := map[string]interface{}{
params := []interface{}{ "perpage": pageSize,
pageSize, "supplier_id": supplierID,
supplierID, "show_status": showStatus,
showStatus, "activity_type": activityType,
activityType,
} }
fixedURL := fmt.Sprintf(urlTemplate, params...)
for { for {
retVal, err2 := a.AccessStorePage(fixedURL+"&curpage="+utils.Int2Str(pageNo), nil) params["curpage"] = pageNo
retVal, err2 := a.AccessStorePage("commodity/activity/begetactivitylist", params, false)
if err = err2; err == nil { if err = err2; err == nil {
var listInfo *PageActListInfo var listInfo *PageActListInfo
if err = utils.Map2StructByJson(retVal, &listInfo, false); err != nil { if err = utils.Map2StructByJson(retVal, &listInfo, false); err != nil {
@@ -120,15 +117,14 @@ func (a *API) BegetActivityList(supplierID int64, showStatus, activityType int)
func (a *API) BegetActSkuList(activityID, supplierID int64) (actSkuList []*PageActSku, err error) { func (a *API) BegetActSkuList(activityID, supplierID int64) (actSkuList []*PageActSku, err error) {
pageSize := maxPageSize4ActSkuList pageSize := maxPageSize4ActSkuList
pageNo := 1 pageNo := 1
urlTemplate := "commodity/activity/begetactskulist?activity_id=%d&perpage=%d&supplier_id=%d" params := map[string]interface{}{
params := []interface{}{ "perpage": pageSize,
activityID, "supplier_id": supplierID,
pageSize, "activity_id": activityID,
supplierID,
} }
fixedURL := fmt.Sprintf(urlTemplate, params...)
for { for {
retVal, err2 := a.AccessStorePage(fixedURL+"&curpage="+utils.Int2Str(pageNo), nil) params["curpage"] = pageNo
retVal, err2 := a.AccessStorePage("commodity/activity/begetactskulist", params, false)
if err = err2; err == nil { if err = err2; err == nil {
var pageActivityInfo *PageActivityInfo var pageActivityInfo *PageActivityInfo
if err = utils.Map2StructByJson(retVal, &pageActivityInfo, false); err != nil { if err = utils.Map2StructByJson(retVal, &pageActivityInfo, false); err != nil {

View File

@@ -322,7 +322,7 @@ func (a *API) GetStoreCookie(key string) string {
return a.storeCookies[key] return a.storeCookies[key]
} }
func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, cookies map[string]string) (retVal map[string]interface{}, err error) { func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, isPost bool, cookies map[string]string) (retVal map[string]interface{}, err error) {
a.locker.RLock() a.locker.RLock()
storeCookieLen := len(a.storeCookies) storeCookieLen := len(a.storeCookies)
a.locker.RUnlock() a.locker.RUnlock()
@@ -332,12 +332,10 @@ func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, coo
err = platformapi.AccessPlatformAPIWithRetry(a.client, err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request { func() *http.Request {
var request *http.Request var request *http.Request
fullURL := utils.GenerateGetURL(storeURL, subURL, nil) if !isPost {
if params == nil { request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(storeURL, subURL, params), nil)
// baseapi.SugarLogger.Debug(fullURL)
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
} else { } else {
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode())) request, _ = http.NewRequest(http.MethodPost, utils.GenerateGetURL(storeURL, subURL, nil), strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("charset", "UTF-8") request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded") request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
} }
@@ -384,8 +382,8 @@ func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, coo
return retVal, err return retVal, err
} }
func (a *API) AccessStorePage(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) { func (a *API) AccessStorePage(subURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
return a.AccessStorePage2(subURL, params, nil) return a.AccessStorePage2(subURL, params, isPost, nil)
} }
func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) { func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) {
@@ -397,8 +395,9 @@ func (a *API) GetRealMobile4Order(orderId string) (mobile string, err error) {
} }
func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) { func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]interface{}, err error) {
retVal, err := a.AccessStorePage(fmt.Sprintf("crm/orderlist?keyword=%s", orderId), nil) retVal, err := a.AccessStorePage("crm/orderlist", map[string]interface{}{
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) "keyword": orderId,
}, false)
if err == nil { if err == nil {
resultList := retVal["order_list"].([]interface{}) resultList := retVal["order_list"].([]interface{})
if len(resultList) > 0 { if len(resultList) > 0 {
@@ -409,23 +408,21 @@ func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]inter
return nil, err return nil, err
} }
// todo 与上一个函数重了
func (a *API) GetStoreOrderInfoList(fromTime, toTime string, shopID string, orderStatus int) (storeOrderList []map[string]interface{}, err error) { func (a *API) GetStoreOrderInfoList(fromTime, toTime string, shopID string, orderStatus int) (storeOrderList []map[string]interface{}, err error) {
// pageSize := 20 // pageSize := 20
pageNo := 1 pageNo := 1
urlTemplate := "crm/orderlist?start_timestamp=%d&end_timestamp=%d&shop_id=%s" params := map[string]interface{}{
params := []interface{}{ "start_timestamp": utils.Str2Time(fromTime).Unix(),
utils.Str2Time(fromTime).Unix(), "end_timestamp": utils.Str2Time(toTime).Unix(),
utils.Str2Time(toTime).Unix(), "shop_id": shopID,
shopID,
} }
if orderStatus >= 0 { if orderStatus >= 0 {
urlTemplate += "&order_status=%d" params["order_status"] = orderStatus
params = append(params, orderStatus)
} }
fixedURL := fmt.Sprintf(urlTemplate, params...)
for { for {
retVal, err2 := a.AccessStorePage(fixedURL+"&page="+utils.Int2Str(pageNo), nil) params["page"] = pageNo
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) retVal, err2 := a.AccessStorePage("crm/orderlist", params, false)
if err = err2; err == nil { if err = err2; err == nil {
resultList := retVal["order_list"].([]interface{}) resultList := retVal["order_list"].([]interface{})
storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...) storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...)
@@ -443,38 +440,33 @@ func (a *API) GetStoreOrderInfoList(fromTime, toTime string, shopID string, orde
func (a *API) getCommentList(isElm bool, fromTime, toTime time.Time, shopID, supplierID string, replyStatus, level, nonEmpty int) (commentList []map[string]interface{}, err error) { func (a *API) getCommentList(isElm bool, fromTime, toTime time.Time, shopID, supplierID string, replyStatus, level, nonEmpty int) (commentList []map[string]interface{}, err error) {
pageSize := 200 pageSize := 200
pageNo := 1 pageNo := 1
urlTemplate := "crm/%s?start_time=%s&end_time=%s&page_count=%d"
params := []interface{}{ params := map[string]interface{}{
[]string{ "start_time": utils.Time2Str(fromTime),
"getelecommentlist", "end_time": utils.Time2Str(toTime),
"getcommentlist", "page_count": pageSize,
}[1-utils.Bool2Int(isElm)],
utils.Time2Str(fromTime),
utils.Time2Str(toTime),
pageSize,
} }
if shopID != "" { if shopID != "" {
urlTemplate += "&shop_id=%s" params["shop_id"] = shopID
params = append(params, shopID)
} else if supplierID != "" { } else if supplierID != "" {
urlTemplate += "&supplier_id=%s" params["supplier_id"] = supplierID
params = append(params, supplierID)
} }
if replyStatus != ReplyStatusAll { if replyStatus != ReplyStatusAll {
urlTemplate += "&reply_status=%d" params["reply_status"] = replyStatus
params = append(params, replyStatus)
} }
if level != CommentLevelAll { if level != CommentLevelAll {
urlTemplate += "&level=%d" params["level"] = level
params = append(params, level)
} }
if nonEmpty != CommentContentAll { if nonEmpty != CommentContentAll {
urlTemplate += "&nonempty=%d" params["nonempty"] = nonEmpty
params = append(params, replyStatus)
} }
fixedURL := fmt.Sprintf(urlTemplate, params...) fixedURL := fmt.Sprintf("crm/%s", []string{
"getelecommentlist",
"getcommentlist",
}[1-utils.Bool2Int(isElm)])
for { for {
retVal, err2 := a.AccessStorePage(fixedURL+"&page_num="+utils.Int2Str(pageNo), nil) params["page_num"] = pageNo
retVal, err2 := a.AccessStorePage(fixedURL, params, false)
if err = err2; err == nil { if err = err2; err == nil {
for _, comment := range retVal["comment_list"].([]interface{}) { for _, comment := range retVal["comment_list"].([]interface{}) {
commentMap := comment.(map[string]interface{}) commentMap := comment.(map[string]interface{})
@@ -507,14 +499,15 @@ func (a *API) GetCommentList(fromTime, toTime time.Time, shopID, supplierID stri
func (a *API) PageGetSkuList(baiduShopID int64) (skuList []map[string]interface{}, err error) { func (a *API) PageGetSkuList(baiduShopID int64) (skuList []map[string]interface{}, err error) {
pageSize := 200 pageSize := 200
pageNo := 1 pageNo := 1
urlTemplate := "commodity/getskulist?wid=%d&perpage=%d&upc_type=2&weight=2" params := map[string]interface{}{
params := []interface{}{ "upc_type": 2,
baiduShopID, "weight": 2,
pageSize, "wid": baiduShopID,
"perpage": pageSize,
} }
fixedURL := fmt.Sprintf(urlTemplate, params...)
for { for {
retVal, err2 := a.AccessStorePage(fixedURL+"&curpage="+utils.Int2Str(pageNo), nil) params["curpage"] = pageNo
retVal, err2 := a.AccessStorePage("commodity/getskulist", params, false)
if err = err2; err == nil { if err = err2; err == nil {
for _, sku := range retVal["sku_list"].([]interface{}) { for _, sku := range retVal["sku_list"].([]interface{}) {
skuList = append(skuList, sku.(map[string]interface{})) skuList = append(skuList, sku.(map[string]interface{}))
@@ -531,13 +524,11 @@ func (a *API) PageGetSkuList(baiduShopID int64) (skuList []map[string]interface{
} }
func (a *API) PageGetCustomSkuList(baiduShopID int64, customCatID int64) (skuList []map[string]interface{}, err error) { func (a *API) PageGetCustomSkuList(baiduShopID int64, customCatID int64) (skuList []map[string]interface{}, err error) {
urlTemplate := "commodity/getCustomSkuList?wid=%d&custom_cat_id=%d" params := map[string]interface{}{
params := []interface{}{ "wid": baiduShopID,
baiduShopID, "custom_cat_id": customCatID,
customCatID,
} }
fixedURL := fmt.Sprintf(urlTemplate, params...) retVal, err := a.AccessStorePage("commodity/getCustomSkuList", params, false)
retVal, err := a.AccessStorePage(fixedURL, nil)
if err == nil { if err == nil {
return utils.Slice2MapSlice(retVal["sku_list"].([]interface{})), nil return utils.Slice2MapSlice(retVal["sku_list"].([]interface{})), nil
} }
@@ -545,12 +536,10 @@ func (a *API) PageGetCustomSkuList(baiduShopID int64, customCatID int64) (skuLis
} }
func (a *API) PageGetCustomCatList(baiduShopID int64) (catList []map[string]interface{}, err error) { func (a *API) PageGetCustomCatList(baiduShopID int64) (catList []map[string]interface{}, err error) {
urlTemplate := "commodity/GetCustomCatList?wid=%d" params := map[string]interface{}{
params := []interface{}{ "wid": baiduShopID,
baiduShopID,
} }
fixedURL := fmt.Sprintf(urlTemplate, params...) retVal, err := a.AccessStorePage("commodity/GetCustomCatList", params, false)
retVal, err := a.AccessStorePage(fixedURL, nil)
if err == nil { if err == nil {
return utils.Slice2MapSlice(retVal["cat_list"].([]interface{})), nil return utils.Slice2MapSlice(retVal["cat_list"].([]interface{})), nil
} }
@@ -558,12 +547,19 @@ func (a *API) PageGetCustomCatList(baiduShopID int64) (catList []map[string]inte
} }
func (a *API) GetStoreList(lng string, lat string) (retVal map[string]interface{}, err error) { func (a *API) GetStoreList(lng string, lat string) (retVal map[string]interface{}, err error) {
retVal, err = a.AccessStorePageNoCookie(fmt.Sprintf("/newretail/main/shoplist?channel=kitchen&pn=1&rn=999&lng=%s&lat=%s", lng, lat)) params := map[string]interface{}{
"channel": "kitchen",
"pn": 1,
"rn": 999,
"lng": lng,
"lat": lat,
}
retVal, err = a.AccessStorePageNoCookie("/newretail/main/shoplist", params)
return retVal, err return retVal, err
} }
func (a *API) GetStoreList2(lng float64, lat float64) (shopListInfo *PageListInfo, err error) { func (a *API) GetStoreList2(lng float64, lat float64) (shopListInfo *PageListInfo, err error) {
retVal, err := a.AccessStorePageNoCookie(fmt.Sprintf("/newretail/main/shoplist?channel=kitchen&pn=1&rn=999&lng=%s&lat=%s", fmt.Sprintf("%.6f", lng), fmt.Sprintf("%.6f", lat))) retVal, err := a.GetStoreList(fmt.Sprintf("%.6f", lng), fmt.Sprintf("%.6f", lat))
if err == nil { if err == nil {
err = utils.Map2StructByJson(retVal, &shopListInfo, true) err = utils.Map2StructByJson(retVal, &shopListInfo, true)
} }
@@ -571,7 +567,12 @@ func (a *API) GetStoreList2(lng float64, lat float64) (shopListInfo *PageListInf
} }
func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, err error) { func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, err error) {
retVal, err := a.AccessStorePageNoCookie(fmt.Sprintf("newretail/shop/getshopinfo?&lat=0&lng=0&shop_id=%s", storeId)) params := map[string]interface{}{
"shop_id": storeId,
"lng": 0,
"lat": 0,
}
retVal, err := a.AccessStorePageNoCookie("newretail/shop/getshopinfo", params)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -582,20 +583,19 @@ func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, er
} }
func (a *API) GetStoreInfo2(storeID string) (storeInfo *PageShopInfo, err error) { func (a *API) GetStoreInfo2(storeID string) (storeInfo *PageShopInfo, err error) {
retVal, err := a.AccessStorePageNoCookie(fmt.Sprintf("newretail/shop/getshopinfo?&lat=0&lng=0&shop_id=%s", storeID)) retVal, err := a.GetStoreInfo(storeID)
if err == nil { if err == nil {
if retVal != nil { if retVal != nil {
retVal["shop_id"] = storeID
err = utils.Map2StructByJson(retVal, &storeInfo, true) err = utils.Map2StructByJson(retVal, &storeInfo, true)
} }
} }
return storeInfo, err return storeInfo, err
} }
func (a *API) AccessStorePageNoCookie(subURL string) (retVal map[string]interface{}, err error) { func (a *API) AccessStorePageNoCookie(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
err = platformapi.AccessPlatformAPIWithRetry(a.client, err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request { func() *http.Request {
fullURL := utils.GenerateGetURL(getStoreURL, subURL, nil) fullURL := utils.GenerateGetURL(getStoreURL, subURL, params)
request, _ := http.NewRequest(http.MethodGet, fullURL, nil) request, _ := http.NewRequest(http.MethodGet, fullURL, nil)
if err != nil { if err != nil {
return nil return nil
@@ -620,7 +620,7 @@ func (a *API) AccessStorePageNoCookie(subURL string) (retVal map[string]interfac
func (a *API) SwitchShop(baiduShopID int64) (switchShopCookie string, err error) { func (a *API) SwitchShop(baiduShopID int64) (switchShopCookie string, err error) {
result, err := a.AccessStorePage("crm/manager/switchshop", map[string]interface{}{ result, err := a.AccessStorePage("crm/manager/switchshop", map[string]interface{}{
"switch_shop_id": baiduShopID, "switch_shop_id": baiduShopID,
}) }, true)
if err == nil { if err == nil {
switchShopCookie = utils.Interface2String(result["Value"]) switchShopCookie = utils.Interface2String(result["Value"])
} }
@@ -628,7 +628,7 @@ func (a *API) SwitchShop(baiduShopID int64) (switchShopCookie string, err error)
} }
func (a *API) GetShopUserInfo2(switchShopCookie string) (shopUserInfo *PageShopUserInfo, err error) { func (a *API) GetShopUserInfo2(switchShopCookie string) (shopUserInfo *PageShopUserInfo, err error) {
shopInfo, err := a.AccessStorePage2("crm/account/getshopuserinfo", nil, map[string]string{ shopInfo, err := a.AccessStorePage2("crm/account/getshopuserinfo", nil, true, map[string]string{
"SWITCH_SHOP": switchShopCookie, "SWITCH_SHOP": switchShopCookie,
}) })
if err == nil { if err == nil {
@@ -652,7 +652,7 @@ func (a *API) GetShopHealthByDetail2(switchShopCookie string) (shopHealthDetail
} }
result, err := a.AccessStorePage2("crm/getshophealthydetail", map[string]interface{}{ result, err := a.AccessStorePage2("crm/getshophealthydetail", map[string]interface{}{
"shop_id": shopInfo.EleID, "shop_id": shopInfo.EleID,
}, map[string]string{ }, true, map[string]string{
"SWITCH_SHOP": switchShopCookie, "SWITCH_SHOP": switchShopCookie,
}) })
if err == nil { if err == nil {

View File

@@ -42,7 +42,7 @@ func TestGetStoreOrderInfoList(t *testing.T) {
} }
func TestGetEleCommentList(t *testing.T) { func TestGetEleCommentList(t *testing.T) {
commentList, err := api.GetEleCommentList(utils.Str2Time("2019-02-25 00:00:00"), utils.Str2Time("2019-02-25 23:30:00"), "", "", ReplyStatusAll, CommentLevelAll, CommentContentAll) commentList, err := api.GetEleCommentList(utils.Str2Time("2019-06-25 00:00:00"), utils.Str2Time("2019-06-25 23:30:00"), "", "", ReplyStatusAll, CommentLevelAll, CommentContentAll)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -115,7 +115,7 @@ func TestGetStoreList(t *testing.T) {
} }
func TestGetStoreList2(t *testing.T) { func TestGetStoreList2(t *testing.T) {
result, err := api.GetStoreList2(104.057218, 30.6949) result, err := api.GetStoreList2(120.074911, 29.306863)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@@ -131,7 +131,7 @@ func TestGetStoreInfo(t *testing.T) {
} }
func TestGetStoreInfo2(t *testing.T) { func TestGetStoreInfo2(t *testing.T) {
result, err := api.GetStoreInfo2("170879219") result, err := api.GetStoreInfo2("2233065925")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }