136 lines
4.1 KiB
Go
136 lines
4.1 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"
|
|
)
|
|
|
|
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"`
|
|
}
|
|
|
|
func (a *API) SetUserCookie(key, value string) {
|
|
a.locker.Lock()
|
|
defer a.locker.Unlock()
|
|
a.userCookies[key] = value
|
|
}
|
|
|
|
func (a *API) GetUserCookie(key string) string {
|
|
a.locker.RLock()
|
|
defer a.locker.RUnlock()
|
|
return a.userCookies[key]
|
|
}
|
|
|
|
func (a *API) AccessUserPage(subURL string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
|
|
a.locker.RLock()
|
|
storeCookieLen := len(a.userCookies)
|
|
a.locker.RUnlock()
|
|
if storeCookieLen == 0 {
|
|
return nil, fmt.Errorf("需要设置User Cookie才能使用此方法")
|
|
}
|
|
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
|
func() *http.Request {
|
|
var request *http.Request
|
|
fullURL := utils.GenerateGetURL(userURL, 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")
|
|
|
|
a.locker.RLock()
|
|
for k, v := range a.userCookies {
|
|
request.AddCookie(&http.Cookie{
|
|
Name: k,
|
|
Value: v,
|
|
})
|
|
}
|
|
a.locker.RUnlock()
|
|
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
|
|
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("ebai AccessStorePage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
|
return platformapi.ErrLevelCodeIsNotOK, newErr
|
|
})
|
|
return retVal, err
|
|
}
|
|
|
|
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
|
|
}
|