diff --git a/pkg/router/router.go b/pkg/router/router.go index 54af6a71..2ec3c7d9 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -76,7 +76,6 @@ func NewRouter() *gin.Engine { privateGroup.POST("generate/captcha", account.GenerateCaptcha) //生成滑块验证码 privateGroup.POST("validate/captcha", account.ValidateCaptcha) //验证滑块验证码 privateGroup.POST("check/register", account.CheckRegister) //校验是否注册 - privateGroup.GET("get/ip", account.GetIP) //获取用户ip acRoute := privateGroup.Group("/user") acRoute.Use(middleware.CheckLogin(service.AccountFieeProvider)) { diff --git a/pkg/service/account/account.go b/pkg/service/account/account.go index 1c39042d..067d8b69 100644 --- a/pkg/service/account/account.go +++ b/pkg/service/account/account.go @@ -528,10 +528,6 @@ func CheckRegister(c *gin.Context) { service.Success(c, res) return } -func GetIP(c *gin.Context) { - ip := c.ClientIP() - service.Success(c, ip) -} func UserLogout(c *gin.Context) { req := account.DecryptJwtRequest{} req.Token = c.GetHeader(e.Authorization) diff --git a/pkg/service/bundle/questionnaireSurvey.go b/pkg/service/bundle/questionnaireSurvey.go index a882bfb9..f22718ef 100644 --- a/pkg/service/bundle/questionnaireSurvey.go +++ b/pkg/service/bundle/questionnaireSurvey.go @@ -65,15 +65,21 @@ func QuestionnaireSurveyCreate(c *gin.Context) { service.Error(c, err) return } - if req.Longitude == "" || req.Latitude == "" { - service.Error(c, errors.New("获取定位失败")) - return - } - address, err := utils.ReverseGeo(req.Longitude, req.Latitude, "ZhCN") + ip := c.ClientIP() + address, err := utils.GetAddressByIP(ip) if err != nil { service.Error(c, errors.New("获取地址失败")) 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}) if err != nil { service.Error(c, err) diff --git a/pkg/utils/map.go b/pkg/utils/map.go index 356fa2d3..fc5f2e4e 100644 --- a/pkg/utils/map.go +++ b/pkg/utils/map.go @@ -2,11 +2,28 @@ package utils import ( "encoding/json" + "fmt" + "io" "io/ioutil" "net/http" + "net/url" "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 { Ak string `json:"ak"` Coordtype string `json:"coordtype"` @@ -86,3 +103,50 @@ func ReverseGeo(longitude, latitude string, language string) (address string, er 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 +}