🎨新增每日一言

This commit is contained in:
coward 2024-08-09 10:06:34 +08:00
parent c1d696ac19
commit 8f28f790e0
5 changed files with 68 additions and 2 deletions

View File

@ -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)
}

View File

@ -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

View File

@ -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) // 每日诗词
}
}

6
http/vo/daily_poetry.go Normal file
View File

@ -0,0 +1,6 @@
package vo
type Poetry struct {
Content string `json:"content"`
Author string `json:"author"`
}

44
utils/daily_poetry.go Normal file
View File

@ -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
}