Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Feat: 处理未正确配置的 天地图 API
Browse files Browse the repository at this point in the history
  • Loading branch information
gggxbbb committed May 14, 2022
1 parent 9b4ed03 commit 736c83e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
35 changes: 35 additions & 0 deletions templates/error.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<!doctype html>
<html lang="zh-CN">

<!--suppress HtmlRequiredTitleElement -->
<head>
{{ block "head.html" . }}{{ end }}
</head>

<body>


<main class="container">

{{ block "nav.html" . }}{{ end }}

<!-- 欢迎 -->
<hgroup>
<h1>糟糕😱, {{ .Nickname }}</h1>
<h2>你似乎遇到了什么麻烦</h2>
</hgroup>

<!-- 错误信息 -->
<article>
<hgroup>
<h2>{{ .Error.Title }}</h2>
<h3>{{ .Error.Intro }}</h3>
</hgroup>
</article>


</main>

</body>

</html>
6 changes: 5 additions & 1 deletion utils/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("获取地点失败")
Expand Down
12 changes: 12 additions & 0 deletions web/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package web

var (
errNoMapTokenWeb = simpleError{
Title: "NoMapTokenWeb",
Intro: "天地图 浏览器端 API Token 未设置",
}
errNoMapTokenApi = simpleError{
Title: "NoMapTokenApi",
Intro: "天地图 服务器端 API Token 未设置",
}
)
16 changes: 16 additions & 0 deletions web/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 10 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 736c83e

Please sign in to comment.