From 05bd2702efdba3f3de05006a32615e18a7427205 Mon Sep 17 00:00:00 2001 From: YuleiLan Date: Thu, 12 Nov 2020 14:48:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=9C=B0=E7=90=86=E4=BD=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=BC=80=E5=85=B3=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/settings.dev.yml | 2 ++ config/settings.yml | 2 ++ tools/ip.go | 44 ++++++++++++++++++++++------------------- 3 files changed, 28 insertions(+), 20 deletions(-) diff --git a/config/settings.dev.yml b/config/settings.dev.yml index cda76c8..33179a1 100644 --- a/config/settings.dev.yml +++ b/config/settings.dev.yml @@ -57,3 +57,5 @@ settings: ssl: key: keystring pem: temp/pem.pem + public: + isLocation: 0 diff --git a/config/settings.yml b/config/settings.yml index efe29b6..0d9fe38 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -57,3 +57,5 @@ settings: ssl: key: keystring pem: temp/pem.pem + public: + isLocation: 0 diff --git a/tools/ip.go b/tools/ip.go index a07287c..64a11cd 100644 --- a/tools/ip.go +++ b/tools/ip.go @@ -5,29 +5,33 @@ import ( "fmt" "io/ioutil" "net/http" + + "github.com/spf13/viper" ) 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) + var address = "已关闭位置获取" + if viper.GetBool("settings.public.isLocation") { + 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) - } - defer resp.Body.Close() - s, err := ioutil.ReadAll(resp.Body) - fmt.Printf(string(s)) + m := make(map[string]string) - m := make(map[string]string) - - err = json.Unmarshal(s, &m) - if err != nil { - fmt.Println("Umarshal failed:", err) + err = json.Unmarshal(s, &m) + if err != nil { + fmt.Println("Umarshal failed:", err) + } + if m["province"] == "" { + return "未知位置" + } + address = m["province"] + "-" + m["city"] } - if m["province"] == "" { - return "未知位置" - } - return m["province"] + "-" + m["city"] -} \ No newline at end of file + return address +}