Files
baseapi/platformapi/mtwmapi/user_page.go
gazebo b98fbb9b83 up
2019-12-30 17:20:52 +08:00

143 lines
4.7 KiB
Go

package mtwmapi
import (
"fmt"
"net/http"
"strings"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
const (
userURL = "http://i.waimai.meituan.com"
orderURL = "https://shangoue.meituan.com"
)
const (
ResponseCodeSuccess = 0
)
type ListShopItem struct {
Address string `json:"address"`
AveragePriceTip string `json:"averagePriceTip"`
DeliveryTimeTip string `json:"deliveryTimeTip"`
DeliveryType int `json:"deliveryType"`
Distance string `json:"distance"`
MinPriceTip string `json:"minPriceTip"`
MonthSalesTip string `json:"monthSalesTip"`
MtWmPoiID string `json:"mtWmPoiId"`
PicURL string `json:"picUrl"`
PoiTypeIcon string `json:"poiTypeIcon"`
ShippingFeeTip string `json:"shippingFeeTip"`
ShippingTime string `json:"shipping_time"`
ShopName string `json:"shopName"`
Status int `json:"status"`
StatusDesc string `json:"statusDesc"`
WmPoiScore int `json:"wmPoiScore"`
}
var (
actionHostMap = map[string]string{
"v2/order/receive/processed/w/distribute/tipAmount/v2": orderURL,
"v2/order/receive/processed/r/distribute/list/v2": orderURL,
"reuse/sc/product/packageprice/w/update": orderURL,
"reuse/sc/product/packageprice/r/get": orderURL,
}
)
func getRootURL(subURL string) (rootURL string) {
rootURL = actionHostMap[subURL]
if rootURL == "" {
rootURL = userURL
}
return rootURL
}
func (a *API) AccessUserPage2(subURL string, params map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) {
if a.GetCookieCount() == 0 {
return nil, fmt.Errorf("需要设置User Cookie才能使用此方法")
}
rootURL := getRootURL(subURL)
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
var request *http.Request
if isPost {
fullURL := utils.GenerateGetURL(rootURL, subURL, nil)
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("charset", "UTF-8")
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.Header.Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1")
request.Header.Set("Pragma", "no-cache")
} else {
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(rootURL, subURL, params), 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")
}
retVal = jsonResult1
if jsonResult1["code"] == nil {
return platformapi.ErrLevelGeneralFail, fmt.Errorf("返回结果格式不正常")
}
code := int(utils.MustInterface2Int64(jsonResult1["code"]))
if code == ResponseCodeSuccess {
retVal, _ = jsonResult1["data"].(map[string]interface{})
return platformapi.ErrLevelSuccess, nil
}
newErr := utils.NewErrorIntCode(jsonResult1["msg"].(string), code)
baseapi.SugarLogger.Debugf("mtwm AccessUserPage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
return platformapi.ErrLevelCodeIsNotOK, newErr
})
return retVal, err
}
func (a *API) AccessUserPage(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
return a.AccessUserPage2(subURL, params, true)
}
func (a *API) GetStoreList(lng, lat float64) (shopList []*ListShopItem, err error) {
params := map[string]interface{}{
"sortId": 1,
"navigateType": 101578,
"firstCategoryId": 101578,
"secondCategoryId": 101578,
"initialLng": lng,
"initialLat": lat,
"geoType": 2,
"wm_longitude": int(lng * 1000000),
"wm_latitude": int(lat * 1000000),
}
startIndex := 0
for {
params["startIndex"] = startIndex
result, err := a.AccessUserPage("openh5/channel/kingkongshoplist", params)
if err != nil {
return shopList, err
}
var batchShopList []*ListShopItem
if err = utils.Map2StructByJson(result["shopList"], &batchShopList, false); err != nil {
return shopList, err
}
shopList = append(shopList, batchShopList...)
if poiHasNextPage, ok := result["poiHasNextPage"].(bool); !ok || !poiHasNextPage {
break
}
startIndex++
}
return shopList, nil
}
func (a *API) GetStoreInfo(storeID string) (storeInfo map[string]interface{}, err error) {
params := map[string]interface{}{
"mtWmPoiId": storeID,
}
retVal, err := a.AccessUserPage("openh5/poi/info", params)
return retVal, err
}