package jdapi import ( "fmt" "net/http" "net/url" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/platformapi" "git.rosy.net.cn/baseapi/utils" ) const ( accessStorePageCookieName = "shop.o2o.jd.com1" getStoreURL = "https://daojia.jd.com" ) type SkuPageImg struct { Big string `json:"big"` Share string `json:"share"` Small string `json:"small"` } func (a *API) SetStoreCookie(storeCookie string) { a.locker.Lock() defer a.locker.Unlock() a.storeCookie = storeCookie } func (a *API) GetStoreCookie() string { a.locker.RLock() defer a.locker.RUnlock() return a.storeCookie } func (a *API) AccessStorePage(fullURL string) (retVal map[string]interface{}, err error) { storeCookie := a.GetStoreCookie() if storeCookie == "" { return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") } err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { request, _ := http.NewRequest(http.MethodGet, fullURL, nil) if err != nil { return nil } request.AddCookie(&http.Cookie{ Name: accessStorePageCookieName, Value: storeCookie, }) return request }, a.config, func(jsonResult1 map[string]interface{}) (errLevel string, err error) { retVal = jsonResult1 code := jsonResult1["code"].(string) if code == ResponseCodeSuccess { retVal = jsonResult1["result"].(map[string]interface{}) return platformapi.ErrLevelSuccess, nil } newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code) if _, ok := exceedLimitCodes[code]; ok { return platformapi.ErrLevelExceedLimit, newErr } else if _, ok := canRetryCodes[code]; ok { return platformapi.ErrLevelRecoverableErr, newErr } else { baseapi.SugarLogger.Debugf("jd AccessStorePage failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) return platformapi.ErrLevelCodeIsNotOK, newErr } }) return retVal, err } func (a *API) GetRealMobile4Order(orderId, stationNo string) (mobile string, err error) { retVal, err := a.GetStoreOrderInfo(orderId, stationNo) if err == nil { return retVal["mobile"].(string), nil } return "", err } func (a *API) GetStoreOrderInfo(orderId, stationNo string) (storeOrderInfo map[string]interface{}, err error) { urlStr := "http://store.jddj.com/order/newManager/search?pageNo=1&pageSize=1&orderBy=&desc=true¶m=" + orderId if stationNo != "" { urlStr += "&stationNo=" + stationNo } retVal, err := a.AccessStorePage(urlStr) // baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) if err == nil { newOrderinfoMains := retVal["newOrderinfoMains"].(map[string]interface{}) resultList := newOrderinfoMains["resultList"].([]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) (storeOrderList []map[string]interface{}, err error) { pageSize := 100 pageNo := 1 urlTemplate := "http://store.jddj.com/order/newManager/tabQuery/all?o2oOrderType=10000&pageNo=%d&pageSize=%d&orderBy=&desc=true&startTimeQuery=%s&endTimeQuery=%s&stationNo=" for { retVal, err := a.AccessStorePage(fmt.Sprintf(urlTemplate, pageNo, pageSize, url.QueryEscape(fromTime), url.QueryEscape(toTime))) // baseapi.SugarLogger.Debug(utils.Format4Output(retVal, false)) if err == nil { newOrderinfoMains := retVal["newOrderinfoMains"].(map[string]interface{}) resultList := newOrderinfoMains["resultList"].([]interface{}) storeOrderList = append(storeOrderList, utils.Slice2MapSlice(resultList)...) if len(storeOrderList) >= int(utils.MustInterface2Int64(newOrderinfoMains["totalCount"])) { return storeOrderList, nil } pageNo++ } else { return nil, err } } return nil, err } func (a *API) GetSkuPageInfo(skuId int64) (skuPageInfo map[string]interface{}, err error) { skuIDMap := map[string]interface{}{ "skuId": utils.Int64ToStr(skuId), "storeId": "0", } skuPageInfo, err = a.AccessStorePage(fmt.Sprintf("https://daojia.jd.com/client?platCode=H5&functionId=product/detailV6_0&body=%s", utils.Format4Output(skuIDMap, true))) if err == nil { baseapi.SugarLogger.Debug(utils.Format4Output(skuPageInfo, false)) } return skuPageInfo, err } func (a *API) GetSkuPageImageInfo(skuId int64) (imgList []*SkuPageImg, err error) { skuPageInfo, err := a.GetSkuPageInfo(skuId) if err == nil { err = utils.Map2StructByJson(skuPageInfo["image"], &imgList, false) } return imgList, err } func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, err error) { retVal, err := a.AccessStorePage2(fmt.Sprintf("client?functionId=store/storeDetailV220&body={\"storeId\":\"%s\"}&appVersion=6.1.0", storeId)) return retVal, err } func (a *API) AccessStorePage2(subURL string) (retVal map[string]interface{}, err error) { err = platformapi.AccessPlatformAPIWithRetry(a.client, func() *http.Request { fullURL := utils.GenerateGetURL(getStoreURL, subURL, nil) request, _ := http.NewRequest(http.MethodGet, fullURL, nil) if err != nil { return nil } return request }, a.config, func(jsonResult1 map[string]interface{}) (errLevel string, err error) { code := jsonResult1["code"].(string) if code == ResponseCodeSuccess && jsonResult1["result"] != nil { retVal = jsonResult1["result"].(map[string]interface{}) return platformapi.ErrLevelSuccess, nil } newErr := utils.NewErrorCode(jsonResult1["msg"].(string), code) return platformapi.ErrLevelCodeIsNotOK, newErr }) return retVal, err } func (a *API) GetStoreList(lng string, lat string) (retVal map[string]interface{}, err error) { retVal, err = a.AccessStorePage2(fmt.Sprintf("client?platCode=h5&appVersion=6.5.0&functionId=zone/recommendStoreList&body={\"channelId\":\"3997\",\"currentPage\":1,\"pageSize\":999,\"coordType\":\"2\",\"platform\":\"1\"}&signKey=b63f63fa9e27123b84a0c80ef5cd210d&lng=%s&lat=%s", lng, lat)) return retVal, err }