130 lines
3.3 KiB
Go
130 lines
3.3 KiB
Go
|
package account
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/api/account"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/pkg/common"
|
||
|
config2 "github.com/fonchain_enterprise/fonchain-main/pkg/config"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/pkg/e"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/pkg/model/request"
|
||
|
"github.com/fonchain_enterprise/fonchain-main/pkg/service"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"github.com/gin-gonic/gin/binding"
|
||
|
"io/ioutil"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type BaiDuMapSt struct {
|
||
|
Status int `json:"status"`
|
||
|
Message string `json:"message"`
|
||
|
Result interface{} `json:"result"`
|
||
|
Address interface{} `json:"address"`
|
||
|
Content interface{} `json:"content"`
|
||
|
}
|
||
|
|
||
|
func SiteQuery(c *gin.Context) {
|
||
|
var req request.SiteLocationReq
|
||
|
|
||
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
//config2.BaiduMapAK
|
||
|
//https: //api.map.baidu.com/place/v2/suggestion?query=滨河路166号®ion=苏州&city_limit=true&output=json&ak=3bAjKGA0pv7qvszGe98RsVZ04Ob5r4ZZ
|
||
|
httpClient := http.Client{}
|
||
|
|
||
|
resp, err2 := httpClient.Get("https://api.map.baidu.com/place/v2/suggestion?query=" + req.Query + "®ion=" + req.Region + "&city_limit=false&output=json&ak=" + req.AK + "&ret_coordtype=" + req.RetCoordtype)
|
||
|
if err2 != nil {
|
||
|
service.Error(c, e.InvalidParams, err2)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
// 读取响应体
|
||
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var info *BaiDuMapSt
|
||
|
err = json.Unmarshal(body, &info)
|
||
|
|
||
|
if err != nil {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if info.Status != 0 && info.Message != "" {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, errors.New(info.Message))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var infoMap = make(map[string]interface{})
|
||
|
infoMap["result"] = info.Result
|
||
|
|
||
|
service.Success(c, infoMap)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func LocationNowIp(c *gin.Context) {
|
||
|
//创建一个UserLoginService对象
|
||
|
var req account.InfoRequest
|
||
|
|
||
|
if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
nowIp := c.ClientIP()
|
||
|
if config2.AppMode == "debug" && common.IsPrivateIP(nowIp) == true {
|
||
|
nowIp = "114.218.158.24"
|
||
|
}
|
||
|
|
||
|
httpClient := http.Client{}
|
||
|
//nowIp = "114.218.158.24"
|
||
|
resp, err2 := httpClient.Get(fmt.Sprintf("https://api.map.baidu.com/location/ip?ip=%s&ak=3bAjKGA0pv7qvszGe98RsVZ04Ob5r4ZZ", nowIp))
|
||
|
if err2 != nil {
|
||
|
service.Error(c, e.InvalidParams, err2)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
defer resp.Body.Close()
|
||
|
// 读取响应体
|
||
|
body, err := ioutil.ReadAll(resp.Body)
|
||
|
if err != nil {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var info *BaiDuMapSt
|
||
|
err = json.Unmarshal(body, &info)
|
||
|
|
||
|
if err != nil {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if info.Status != 0 && info.Message != "" {
|
||
|
fmt.Println("Error reading response body:", err)
|
||
|
service.Error(c, e.InvalidParams, errors.New(info.Message))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
var infoMap = make(map[string]interface{})
|
||
|
infoMap["content"] = info.Content
|
||
|
infoMap["address"] = info.Address
|
||
|
|
||
|
service.Success(c, infoMap)
|
||
|
return
|
||
|
|
||
|
}
|