commit
bae310ce49
@ -57,3 +57,5 @@ settings:
|
|||||||
ssl:
|
ssl:
|
||||||
key: keystring
|
key: keystring
|
||||||
pem: temp/pem.pem
|
pem: temp/pem.pem
|
||||||
|
public:
|
||||||
|
isLocation: 0
|
||||||
|
@ -57,3 +57,5 @@ settings:
|
|||||||
ssl:
|
ssl:
|
||||||
key: keystring
|
key: keystring
|
||||||
pem: temp/pem.pem
|
pem: temp/pem.pem
|
||||||
|
public:
|
||||||
|
isLocation: 0
|
||||||
|
44
tools/ip.go
44
tools/ip.go
@ -5,29 +5,33 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetLocation(ip string) string {
|
func GetLocation(ip string) string {
|
||||||
if ip == "127.0.0.1" || ip == "localhost" {
|
var address = "已关闭位置获取"
|
||||||
return "内部IP"
|
if viper.GetBool("settings.public.isLocation") {
|
||||||
}
|
if ip == "127.0.0.1" || ip == "localhost" {
|
||||||
resp, err := http.Get("https://restapi.amap.com/v3/ip?ip=" + ip + "&key=3fabc36c20379fbb9300c79b19d5d05e")
|
return "内部IP"
|
||||||
if err != nil {
|
}
|
||||||
panic(err)
|
resp, err := http.Get("https://restapi.amap.com/v3/ip?ip=" + ip + "&key=3fabc36c20379fbb9300c79b19d5d05e")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
s, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
}
|
m := make(map[string]string)
|
||||||
defer resp.Body.Close()
|
|
||||||
s, err := ioutil.ReadAll(resp.Body)
|
|
||||||
fmt.Printf(string(s))
|
|
||||||
|
|
||||||
m := make(map[string]string)
|
err = json.Unmarshal(s, &m)
|
||||||
|
if err != nil {
|
||||||
err = json.Unmarshal(s, &m)
|
fmt.Println("Umarshal failed:", err)
|
||||||
if err != nil {
|
}
|
||||||
fmt.Println("Umarshal failed:", err)
|
if m["province"] == "" {
|
||||||
|
return "未知位置"
|
||||||
|
}
|
||||||
|
address = m["province"] + "-" + m["city"]
|
||||||
}
|
}
|
||||||
if m["province"] == "" {
|
return address
|
||||||
return "未知位置"
|
}
|
||||||
}
|
|
||||||
return m["province"] + "-" + m["city"]
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user