This commit is contained in:
richboo111
2023-10-25 09:27:05 +08:00
parent 528b1f4591
commit bee676e5cb
6 changed files with 79 additions and 52 deletions

View File

@@ -2,16 +2,16 @@ package baidunavi
import (
"crypto/md5"
"errors"
"fmt"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strings"
"time"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
const (
@@ -201,63 +201,73 @@ func (a *API) BatchCoordinateConvert(coords []*Coordinate, fromCoordSys, toCoord
for _, v := range coords {
coordsStrList = append(coordsStrList, fmt.Sprintf("%.6f,%.6f", v.Lng, v.Lat))
}
params := map[string]interface{}{
"coords": strings.Join(coordsStrList, ";"),
"from": fromCoordSys,
"to": toCoordSys,
params := url.Values{
"coords": []string{strings.Join(coordsStrList, ";")},
"from": []string{utils.Int2Str(fromCoordSys)},
"to": []string{utils.Int2Str(toCoordSys)},
}
result, err := a.AccessAPI("geoconv/v1", params)
result, err := a.AccessAPI2("geoconv/v1/", params)
if err == nil {
err = utils.Map2StructByJson(result, &outCoords, false)
}
return outCoords, err
}
// DirectionLiteRide 骑行路线规划
func (a *API) DirectionLiteRide(coords []*Coordinate) (retVal interface{}, err error) {
var (
sCoords string
uCoords string
timestamp = time.Now().Unix()
apiStr = "directionlite/v1/riding"
sCoords string
uCoords string
apiStr = "directionlite/v1/riding"
)
sCoords = utils.Float64ToStr(coords[0].Lat) + "," + utils.Float64ToStr(coords[0].Lng)
uCoords = utils.Float64ToStr(coords[1].Lat) + "," + utils.Float64ToStr(coords[1].Lng)
param := map[string]interface{}{
"origin": sCoords,
"destination": uCoords,
"timestamp": timestamp,
//"steps_info": 2,
}
//生成签名
params2 := utils.MergeMaps(utils.Params2Map("ak", a.ak, "output", "json"), param)
sn := a.signParams(apiStr, params2)
params := url.Values{
param := url.Values{
"origin": []string{sCoords},
"destination": []string{uCoords},
"ak": []string{a.ak},
"sn": []string{sn},
"timestamp": []string{utils.Int64ToStr(timestamp)},
}
// 发起请求
request, err := url.Parse(prodURL2 + "/" + apiStr + "?" + params.Encode())
if nil != err {
return nil, err
resp, err := a.AccessAPI2(apiStr, param)
if err == nil {
return resp, nil
}
resp, err1 := http.Get(request.String())
defer resp.Body.Close()
if err1 != nil {
return nil, err1
}
body, err2 := ioutil.ReadAll(resp.Body)
if err2 != nil {
return nil, err2
}
result := string(body)
return result, nil
return nil, nil
}
func (a *API) AccessAPI2(apiStr string, param url.Values) (retVal interface{}, err error) {
for _, v := range BaidunaviAKList {
param.Add("ak", v)
param.Add("output", "json")
params2 := map[string]interface{}{}
for i, j := range param {
params2[i] = j
}
params2[signKey] = a.signParams(apiStr, params2)
param.Add("timestamp", utils.Int64ToStr(time.Now().Unix()))
// 发起请求
request, err := url.Parse(prodURL2 + "/" + apiStr + "?" + param.Encode())
if err != nil {
continue
}
resp, err1 := http.Get(request.String())
//err1 = errors.New("测试退出退出")
//defer resp.Body.Close()
body, err2 := ioutil.ReadAll(resp.Body)
if err1 != nil || err2 != nil {
continue
}
result := string(body)
if err == nil {
return result, nil
}
}
return nil, errors.New("所有百度应用额度均用完")
}

View File

@@ -0,0 +1,12 @@
package baidunavi
const (
BaiduAK1 = "wW2AwzPS0hdaPy5QLalzso7ARX5uYZtZ" //京西菜市
BaiduAK2 = "LdvYnRCGsu2m2ZG2CeWR2nCG9NDPhR3G" //京西到家
BaiduAK3 = "vISH1MiDm67sb95sNUrdWPgjOmQMGFw3" //京西菜市成都
BaiduAK4 = "PGGAGMpF2dksZo37QhPlEmB8U78Qo5SG" //京西菜市北京
BaiduAK5 = "uOcPyMnToC5VFbosVhb2zimPRbizd72k" //冲天猴
)
var BaidunaviAKList = []string{BaiduAK1, BaiduAK2, BaiduAK3, BaiduAK4, BaiduAK5}

View File

@@ -21,7 +21,8 @@ func init() {
baseapi.Init(sugarLogger)
//api = New("eL94zToVOdGDTkNQxV8dnEQ1ZRcB2UKb", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
api = New("wW2AwzPS0hdaPy5QLalzso7ARX5uYZtZ", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
//api = New("wW2AwzPS0hdaPy5QLalzso7ARX5uYZtZ", "ZG0OOpOsOVURUwAkkmoHQFKRCbzn0zGb")
api = New("wW2AwzPS0hdaPy5QLalzso7ARX5uYZtZ", "")
}
func TestBatchCoordinateConvert(t *testing.T) {

View File

@@ -35,7 +35,7 @@ func TestFullDiscountBatchSave(t *testing.T) {
}
func TestFullDiscountList(t *testing.T) {
result, err := api.FullDiscountList("17626555", ActTypeStoreFullDiscount)
result, err := api.FullDiscountList("9846711", ActTypeStoreFullDiscount)
if err != nil {
t.Fatal(err)
}
@@ -84,7 +84,7 @@ func TestRetailDiscountBatchSave(t *testing.T) {
}
func TestRetailDiscountList(t *testing.T) { //56 1001
result, err := api.RetailDiscountList("17626555", 1001)
result, err := api.RetailDiscountList("9846711", 1001)
if err != nil {
t.Fatal(err)
}

View File

@@ -23,11 +23,11 @@ func init() {
//api = New("589", "a81eb3df418d83d6a1a4b7c572156d2f", "", "")
// 果园
//api = New("4123", "df2c88338b85f830cebce2a9eab56628", "", "")
api = New("4123", "df2c88338b85f830cebce2a9eab56628", "", "")
//商超
//api = New("5873", "41c479790a76f86326f89e8048964739", "", "token_nngsVP37s-HXY86xe85H7Q") //token_n4TwqCntWWuvQwAawzxC0w
api = New("5873", "41c479790a76f86326f89e8048964739", "", "token_rfAD6OxSVxkTeCO1XmNCZg") //token_n4TwqCntWWuvQwAawzxC0w
//api = New("5873", "41c479790a76f86326f89e8048964739", "", "token_rfAD6OxSVxkTeCO1XmNCZg") //token_n4TwqCntWWuvQwAawzxC0w
//api = New("5873", "41c479790a76f86326f89e8048964739", "", "token_r36FEse6_ywebQI65FNNWA") //token_n4TwqCntWWuvQwAawzxC0w
cookieStr := `
acctId=57396785; token=0bWbK5VbK50E2BmIhIH2zHB-am_y7mB37yXHm6RLZWx4*; wmPoiId=-1;

View File

@@ -50,10 +50,14 @@ func TestGetWaybillPrice(t *testing.T) {
ToAddress: "嘉华酒店",
CityName: "成都市",
SendType: "0",
ToLat: "30.691134",
ToLng: "104.042307",
FromLat: "30.693001",
FromLng: "104.04546",
ToLat: "29.604639",
ToLng: "103.743532",
FromLat: "29.608636",
FromLng: "103.728641",
//ToLat: "29.598951",
//ToLng: "103.736961",
//FromLat: "29.602826",
//FromLng: "103.722107",
}
price := utils.Struct2Map(param, "", false)
resp, err := api.GetOrderPrice(price)