Compare commits
4 Commits
feat-cjy-c
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| b1e1d317ed | |||
| 469b0313ce | |||
| c33fa06382 | |||
| c982910bfb |
@ -65,15 +65,21 @@ func QuestionnaireSurveyCreate(c *gin.Context) {
|
|||||||
service.Error(c, err)
|
service.Error(c, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if req.Longitude == "" || req.Latitude == "" {
|
ip := c.ClientIP()
|
||||||
service.Error(c, errors.New("获取定位失败"))
|
address, err := utils.GetAddressByIP(ip)
|
||||||
return
|
|
||||||
}
|
|
||||||
address, err := utils.ReverseGeo(req.Longitude, req.Latitude, "ZhCN")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.Error(c, errors.New("获取地址失败"))
|
service.Error(c, errors.New("获取地址失败"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
//if req.Longitude == "" || req.Latitude == "" {
|
||||||
|
// service.Error(c, errors.New("获取定位失败"))
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
//address, err := utils.ReverseGeo(req.Longitude, req.Latitude, "ZhCN")
|
||||||
|
//if err != nil {
|
||||||
|
// service.Error(c, errors.New("获取地址失败"))
|
||||||
|
// return
|
||||||
|
//}
|
||||||
surveyInfo, err := service.BundleProvider.GetQuestionnaireSurveyInfo(c, &bundle.GetQuestionnaireSurveyInfoRequest{UserTel: req.UserTel})
|
surveyInfo, err := service.BundleProvider.GetQuestionnaireSurveyInfo(c, &bundle.GetQuestionnaireSurveyInfoRequest{UserTel: req.UserTel})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
service.Error(c, err)
|
service.Error(c, err)
|
||||||
|
|||||||
@ -2,11 +2,28 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const baiduIPLocationAPI = "https://api.map.baidu.com/location/ip"
|
||||||
|
|
||||||
|
// 建议把 ak 放配置;这里先保留你给的 key,后续可改为从配置读取。
|
||||||
|
const baiduMapAK = "T8DGBYxbZ1iAeXx1J57McKigyHJHulPQ"
|
||||||
|
|
||||||
|
type baiduIPResp struct {
|
||||||
|
Status int `json:"status"`
|
||||||
|
Address string `json:"address"`
|
||||||
|
Content struct {
|
||||||
|
Address string `json:"address"`
|
||||||
|
} `json:"content"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
type ReverseGeocodingReq struct {
|
type ReverseGeocodingReq struct {
|
||||||
Ak string `json:"ak"`
|
Ak string `json:"ak"`
|
||||||
Coordtype string `json:"coordtype"`
|
Coordtype string `json:"coordtype"`
|
||||||
@ -86,3 +103,50 @@ func ReverseGeo(longitude, latitude string, language string) (address string, er
|
|||||||
|
|
||||||
return address, nil
|
return address, nil
|
||||||
}
|
}
|
||||||
|
func GetAddressByIP(ip string) (string, error) {
|
||||||
|
ip = strings.TrimSpace(ip)
|
||||||
|
if ip == "" {
|
||||||
|
return "", fmt.Errorf("ip is empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
q := url.Values{}
|
||||||
|
q.Set("ip", ip)
|
||||||
|
q.Set("coor", "bd09ll")
|
||||||
|
q.Set("ak", baiduMapAK)
|
||||||
|
|
||||||
|
reqURL := baiduIPLocationAPI + "?" + q.Encode()
|
||||||
|
client := &http.Client{Timeout: 5 * time.Second}
|
||||||
|
resp, err := client.Get(reqURL)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return "", fmt.Errorf("baidu api status: %d", resp.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
body, err := io.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
|
||||||
|
var r baiduIPResp
|
||||||
|
if err := json.Unmarshal(body, &r); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
if r.Status != 0 {
|
||||||
|
if r.Message == "" {
|
||||||
|
r.Message = "baidu api error"
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("%s", r.Message)
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.TrimSpace(r.Content.Address) != "" {
|
||||||
|
return r.Content.Address, nil
|
||||||
|
}
|
||||||
|
if strings.TrimSpace(r.Address) != "" {
|
||||||
|
return r.Address, nil
|
||||||
|
}
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user