- 重构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
import (
"fmt"
"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) {
pageSize := maxPageSize4ActSkuList
pageNo := 1
urlTemplate := "commodity/activity/begetactivitylist?perpage=%d&supplier_id=%d&show_status=%d&activity_type=%d"
params := []interface{}{
pageSize,
supplierID,
showStatus,
activityType,
params := map[string]interface{}{
"perpage": pageSize,
"supplier_id": supplierID,
"show_status": showStatus,
"activity_type": activityType,
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
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 {
var listInfo *PageActListInfo
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) {
pageSize := maxPageSize4ActSkuList
pageNo := 1
urlTemplate := "commodity/activity/begetactskulist?activity_id=%d&perpage=%d&supplier_id=%d"
params := []interface{}{
activityID,
pageSize,
supplierID,
params := map[string]interface{}{
"perpage": pageSize,
"supplier_id": supplierID,
"activity_id": activityID,
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
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 {
var pageActivityInfo *PageActivityInfo
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]
}
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()
storeCookieLen := len(a.storeCookies)
a.locker.RUnlock()
@@ -332,12 +332,10 @@ func (a *API) AccessStorePage2(subURL string, params map[string]interface{}, coo
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
fullURL := utils.GenerateGetURL(storeURL, subURL, nil)
if params == nil {
// baseapi.SugarLogger.Debug(fullURL)
request, _ = http.NewRequest(http.MethodGet, fullURL, nil)
if !isPost {
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(storeURL, subURL, params), nil)
} 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("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
}
func (a *API) AccessStorePage(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
return a.AccessStorePage2(subURL, params, nil)
func (a *API) AccessStorePage(subURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
return a.AccessStorePage2(subURL, params, isPost, nil)
}
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) {
retVal, err := a.AccessStorePage(fmt.Sprintf("crm/orderlist?keyword=%s", orderId), nil)
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
retVal, err := a.AccessStorePage("crm/orderlist", map[string]interface{}{
"keyword": orderId,
}, false)
if err == nil {
resultList := retVal["order_list"].([]interface{})
if len(resultList) > 0 {
@@ -409,23 +408,21 @@ func (a *API) GetStoreOrderInfo(orderId string) (storeOrderInfo map[string]inter
return nil, err
}
// todo 与上一个函数重了
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,
params := map[string]interface{}{
"start_timestamp": utils.Str2Time(fromTime).Unix(),
"end_timestamp": utils.Str2Time(toTime).Unix(),
"shop_id": shopID,
}
if orderStatus >= 0 {
urlTemplate += "&order_status=%d"
params = append(params, orderStatus)
params["order_status"] = orderStatus
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
for {
retVal, err2 := a.AccessStorePage(fixedURL+"&page="+utils.Int2Str(pageNo), nil)
// baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false))
params["page"] = pageNo
retVal, err2 := a.AccessStorePage("crm/orderlist", params, false)
if err = err2; err == nil {
resultList := retVal["order_list"].([]interface{})
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) {
pageSize := 200
pageNo := 1
urlTemplate := "crm/%s?start_time=%s&end_time=%s&page_count=%d"
params := []interface{}{
[]string{
"getelecommentlist",
"getcommentlist",
}[1-utils.Bool2Int(isElm)],
utils.Time2Str(fromTime),
utils.Time2Str(toTime),
pageSize,
params := map[string]interface{}{
"start_time": utils.Time2Str(fromTime),
"end_time": utils.Time2Str(toTime),
"page_count": pageSize,
}
if shopID != "" {
urlTemplate += "&shop_id=%s"
params = append(params, shopID)
params["shop_id"] = shopID
} else if supplierID != "" {
urlTemplate += "&supplier_id=%s"
params = append(params, supplierID)
params["supplier_id"] = supplierID
}
if replyStatus != ReplyStatusAll {
urlTemplate += "&reply_status=%d"
params = append(params, replyStatus)
params["reply_status"] = replyStatus
}
if level != CommentLevelAll {
urlTemplate += "&level=%d"
params = append(params, level)
params["level"] = level
}
if nonEmpty != CommentContentAll {
urlTemplate += "&nonempty=%d"
params = append(params, replyStatus)
params["nonempty"] = nonEmpty
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
fixedURL := fmt.Sprintf("crm/%s", []string{
"getelecommentlist",
"getcommentlist",
}[1-utils.Bool2Int(isElm)])
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 {
for _, comment := range retVal["comment_list"].([]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) {
pageSize := 200
pageNo := 1
urlTemplate := "commodity/getskulist?wid=%d&perpage=%d&upc_type=2&weight=2"
params := []interface{}{
baiduShopID,
pageSize,
params := map[string]interface{}{
"upc_type": 2,
"weight": 2,
"wid": baiduShopID,
"perpage": pageSize,
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
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 {
for _, sku := range retVal["sku_list"].([]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) {
urlTemplate := "commodity/getCustomSkuList?wid=%d&custom_cat_id=%d"
params := []interface{}{
baiduShopID,
customCatID,
params := map[string]interface{}{
"wid": baiduShopID,
"custom_cat_id": customCatID,
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
retVal, err := a.AccessStorePage(fixedURL, nil)
retVal, err := a.AccessStorePage("commodity/getCustomSkuList", params, false)
if err == 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) {
urlTemplate := "commodity/GetCustomCatList?wid=%d"
params := []interface{}{
baiduShopID,
params := map[string]interface{}{
"wid": baiduShopID,
}
fixedURL := fmt.Sprintf(urlTemplate, params...)
retVal, err := a.AccessStorePage(fixedURL, nil)
retVal, err := a.AccessStorePage("commodity/GetCustomCatList", params, false)
if err == 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) {
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
}
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 {
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) {
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 {
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) {
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 retVal != nil {
retVal["shop_id"] = storeID
err = utils.Map2StructByJson(retVal, &storeInfo, true)
}
}
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,
func() *http.Request {
fullURL := utils.GenerateGetURL(getStoreURL, subURL, nil)
fullURL := utils.GenerateGetURL(getStoreURL, subURL, params)
request, _ := http.NewRequest(http.MethodGet, fullURL, nil)
if err != 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) {
result, err := a.AccessStorePage("crm/manager/switchshop", map[string]interface{}{
"switch_shop_id": baiduShopID,
})
}, true)
if err == nil {
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) {
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,
})
if err == nil {
@@ -652,7 +652,7 @@ func (a *API) GetShopHealthByDetail2(switchShopCookie string) (shopHealthDetail
}
result, err := a.AccessStorePage2("crm/getshophealthydetail", map[string]interface{}{
"shop_id": shopInfo.EleID,
}, map[string]string{
}, true, map[string]string{
"SWITCH_SHOP": switchShopCookie,
})
if err == nil {

View File

@@ -42,7 +42,7 @@ func TestGetStoreOrderInfoList(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 {
t.Fatal(err)
}
@@ -115,7 +115,7 @@ func TestGetStoreList(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 {
t.Fatal(err)
}
@@ -131,7 +131,7 @@ func TestGetStoreInfo(t *testing.T) {
}
func TestGetStoreInfo2(t *testing.T) {
result, err := api.GetStoreInfo2("170879219")
result, err := api.GetStoreInfo2("2233065925")
if err != nil {
t.Fatal(err)
}