This commit is contained in:
YuleiLan
2020-07-13 20:33:20 +08:00
commit cca0845f24
106 changed files with 17580 additions and 0 deletions

33
tools/ip.go Normal file
View File

@@ -0,0 +1,33 @@
package tools
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func GetLocation(ip string) string {
if ip == "127.0.0.1" || ip == "localhost" {
return "内部IP"
}
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)
fmt.Printf(string(s))
m := make(map[string]string)
err = json.Unmarshal(s, &m)
if err != nil {
fmt.Println("Umarshal failed:", err)
}
if m["province"] == "" {
return "未知位置"
}
return m["province"] + "-" + m["city"]
}