From 736c83e923cac91228069fdd21f85037accc8eb4 Mon Sep 17 00:00:00 2001 From: gggxbbb <2331490629@qq.com> Date: Sat, 14 May 2022 20:45:33 +0800 Subject: [PATCH] =?UTF-8?q?Feat:=20=E5=A4=84=E7=90=86=E6=9C=AA=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E9=85=8D=E7=BD=AE=E7=9A=84=20=E5=A4=A9=E5=9C=B0?= =?UTF-8?q?=E5=9B=BE=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- templates/error.html | 35 +++++++++++++++++++++++++++++++++++ utils/map.go | 6 +++++- web/error.go | 12 ++++++++++++ web/location.go | 16 ++++++++++++++++ web/web.go | 10 ++++++++++ 5 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 templates/error.html create mode 100644 web/error.go diff --git a/templates/error.html b/templates/error.html new file mode 100644 index 0000000..9cfc104 --- /dev/null +++ b/templates/error.html @@ -0,0 +1,35 @@ + + + + + + {{ block "head.html" . }}{{ end }} + + + + + +
+ + {{ block "nav.html" . }}{{ end }} + + +
+

糟糕😱, {{ .Nickname }}

+

你似乎遇到了什么麻烦

+
+ + +
+
+

{{ .Error.Title }}

+

{{ .Error.Intro }}

+
+
+ + +
+ + + + \ No newline at end of file diff --git a/utils/map.go b/utils/map.go index 4aab4db..b5ba087 100644 --- a/utils/map.go +++ b/utils/map.go @@ -30,7 +30,11 @@ func GetLocationByName(name string) (location Location) { return } else { log.Info("地点不存在, 查询 API") - //goland:noinspection HttpUrlsUsage + if AppConfig.Map.TokenApi == "" { + log.WithField("名称", name).Error("未配置 API Token, 查询取消") + location.Name = name + return + } resp, err := http.Get("https://api.tianditu.gov.cn/geocoder?ds={\"keyWord\":\"" + name + "\"}&tk=" + AppConfig.Map.TokenApi) if err != nil { log.WithError(err).WithField("名称", name).Error("获取地点失败") diff --git a/web/error.go b/web/error.go new file mode 100644 index 0000000..f711f57 --- /dev/null +++ b/web/error.go @@ -0,0 +1,12 @@ +package web + +var ( + errNoMapTokenWeb = simpleError{ + Title: "NoMapTokenWeb", + Intro: "天地图 浏览器端 API Token 未设置", + } + errNoMapTokenApi = simpleError{ + Title: "NoMapTokenApi", + Intro: "天地图 服务器端 API Token 未设置", + } +) diff --git a/web/location.go b/web/location.go index e71100a..838dd27 100644 --- a/web/location.go +++ b/web/location.go @@ -9,6 +9,22 @@ import ( ) func MapPage(c *gin.Context) { + if AppConfig.Map.TokenWeb == "" { + var data errorPageData + data.Title = "日记地图" + data.Nickname = AppConfig.Web.Nickname + data.Error = errNoMapTokenWeb + c.HTML(200, "error.html", data) + return + } + if AppConfig.Map.TokenApi == "" { + var data errorPageData + data.Title = "日记地图" + data.Nickname = AppConfig.Web.Nickname + data.Error = errNoMapTokenApi + c.HTML(200, "error.html", data) + return + } var data locationMapData timenoteData := loader.LoadLastDataFile() tempL := utils.GetLocationNotes(timenoteData.Notes) diff --git a/web/web.go b/web/web.go index 64d02ae..1f24ac6 100644 --- a/web/web.go +++ b/web/web.go @@ -88,3 +88,13 @@ type locationPageData struct { simpleLocation Notes []simpleNote `json:"notes"` } + +type simpleError struct { + Title string `json:"title"` + Intro string `json:"intro"` +} + +type errorPageData struct { + basicData + Error simpleError `json:"error"` +}