diff --git a/http/api/dashboard.go b/http/api/dashboard.go index d8e8a9f..3d3b355 100644 --- a/http/api/dashboard.go +++ b/http/api/dashboard.go @@ -7,6 +7,7 @@ import ( "wireguard-ui/http/response" "wireguard-ui/http/vo" "wireguard-ui/service" + "wireguard-ui/utils" ) type DashboardApi struct{} @@ -38,3 +39,16 @@ func (DashboardApi) List(c *gin.Context) { } response.R(c).Paginate(data, total, p.Current, p.Size) } + +// DailyPoetry +// @description: 每日诗词 +// @receiver DashboardApi +// @param c +func (DashboardApi) DailyPoetry(c *gin.Context) { + data, err := utils.DailyPoetry().HitokotoPoetry() + if err != nil { + response.R(c).FailedWithError("获取失败") + return + } + response.R(c).OkWithData(data) +} diff --git a/http/middleware/authorization.go b/http/middleware/authorization.go index 8b42a1a..0af4f0b 100644 --- a/http/middleware/authorization.go +++ b/http/middleware/authorization.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/gin-gonic/gin" "github.com/google/uuid" + "slices" "strings" "time" "wireguard-ui/component" @@ -40,7 +41,7 @@ func Authorization() gin.HandlerFunc { } // 如果token的颁发者与请求的站点不一致那么就给它抬出去 - if userClaims.Issuer != utils.WebSite().GetHost(c.Request.Header.Get("Referer")) { + if !slices.Contains(strings.Split(userClaims.Issuer, ","), utils.WebSite().GetHost(c.Request.Header.Get("Referer"))) { response.R(c).AuthorizationFailed("未登陆") c.Abort() return diff --git a/http/router/dashboard.go b/http/router/dashboard.go index f2229a6..f045356 100644 --- a/http/router/dashboard.go +++ b/http/router/dashboard.go @@ -12,6 +12,7 @@ import ( func DashboardApi(r *gin.RouterGroup) { dashboard := r.Group("dashboard", middleware.Authorization(), middleware.RequestLog()) { - dashboard.GET("/request/list", api.Dashboard().List) // 请求日志 + dashboard.GET("/request/list", api.Dashboard().List) // 请求日志 + dashboard.GET("/daily-poetry", api.Dashboard().DailyPoetry) // 每日诗词 } } diff --git a/http/vo/daily_poetry.go b/http/vo/daily_poetry.go new file mode 100644 index 0000000..cc2d47b --- /dev/null +++ b/http/vo/daily_poetry.go @@ -0,0 +1,6 @@ +package vo + +type Poetry struct { + Content string `json:"content"` + Author string `json:"author"` +} diff --git a/utils/daily_poetry.go b/utils/daily_poetry.go new file mode 100644 index 0000000..328b41c --- /dev/null +++ b/utils/daily_poetry.go @@ -0,0 +1,44 @@ +package utils + +import ( + jsoniter "github.com/json-iterator/go" + "wireguard-ui/global/client" + "wireguard-ui/http/vo" +) + +type dailyPoetry struct{} + +func DailyPoetry() dailyPoetry { + return dailyPoetry{} +} + +// HitokotoPoetry +// @description: 获取每日一句【一言】 +// @receiver dailyPoetry +// @return data +// @return err +func (dailyPoetry) HitokotoPoetry() (data *vo.Poetry, err error) { + req := client.HttpClient.R() + req.SetQueryParam("c", "h") + req.SetQueryParam("c", "i") + req.SetQueryParam("c", "k") + req.SetQueryParam("encode", "json") + req.SetQueryParam("charset", "utf-8") + + response, err := req.Get("https://hitokoto.mrxqq.top") + if err != nil { + return nil, err + } + + author := jsoniter.Get(response.Body(), "from_who").ToString() + if author == "" { + author = "佚名" + } + + data = &vo.Poetry{ + Content: jsoniter.Get(response.Body(), "hitokoto").ToString(), + Author: author, + } + + return +}