You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.6 KiB
64 lines
1.6 KiB
2 years ago
|
package biz
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"github.com/go-kratos/kratos/v2/log"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type MapServiceBiz struct {
|
||
|
log *log.Helper
|
||
|
}
|
||
|
|
||
|
func NewMapServiceBiz(logger log.Logger) *MapServiceBiz {
|
||
|
return &MapServiceBiz{log: log.NewHelper(logger)}
|
||
|
}
|
||
|
|
||
|
type DirectionDrivingResp struct {
|
||
|
Status string `json:"status,omitempty"`
|
||
|
Info string `json:"info,omitempty"`
|
||
|
Infocode string `json:"infocode,omitempty"`
|
||
|
Count string `json:"count,omitempty"`
|
||
|
Route struct {
|
||
|
Origin string `json:"origin,omitempty"`
|
||
|
Destination string `json:"destination,omitempty"`
|
||
|
Paths []Path `json:"paths,omitempty"`
|
||
|
} `json:"route"`
|
||
|
}
|
||
|
|
||
|
type Path struct {
|
||
|
Distance string `json:"distance,omitempty"`
|
||
|
Duration string `json:"duration,omitempty"`
|
||
|
Strategy string `json:"strategy,omitempty"`
|
||
|
}
|
||
|
|
||
|
func (msbiz *MapServiceBiz) GetDriverInfo(origin, destination string) (string, string, error) {
|
||
|
key := "bf898b32c1748a2668684e4bcbe4c496"
|
||
|
api := "https://restapi.amap.com/v3/direction/driving"
|
||
|
parameters := fmt.Sprintf("origin=%s&destination=%s&extensions=base&output=json&key=%s", origin, destination, key)
|
||
|
url := api + "?" + parameters
|
||
|
resp, err := http.Get(url)
|
||
|
if err != nil {
|
||
|
return "", "", err
|
||
|
}
|
||
|
defer func() {
|
||
|
_ = resp.Body.Close()
|
||
|
}()
|
||
|
body, err := io.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
return "", "", err
|
||
|
}
|
||
|
ddResp := DirectionDrivingResp{}
|
||
|
if err := json.Unmarshal(body, &ddResp); err != nil {
|
||
|
return "", "", err
|
||
|
}
|
||
|
if ddResp.Status == "0" {
|
||
|
return "", "", errors.New(ddResp.Info)
|
||
|
}
|
||
|
path := ddResp.Route.Paths[0]
|
||
|
return path.Distance, path.Duration, nil
|
||
|
}
|