From 78450aeace549e03ebc92b3ba8beb61f8944bda9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Tue, 16 Jul 2024 11:58:52 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E4=BC=98=E5=8C=96=E7=83=AD=E6=A6=9C?= =?UTF-8?q?=E7=9A=84=E9=93=BE=E6=8E=A5=E4=B8=BA=E7=9F=AD=E9=93=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.yaml | 2 ++ config/system.go | 1 + model/dto/urlc.go | 8 ++++++++ tasks/hottop/hottop.go | 5 ++++- utils/urlc.go | 37 +++++++++++++++++++++++++++++++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 model/dto/urlc.go create mode 100644 utils/urlc.go diff --git a/config.yaml b/config.yaml index 82b76b9..41e0fab 100644 --- a/config.yaml +++ b/config.yaml @@ -2,6 +2,8 @@ system: # 每日新闻接口 Token # 获取地址: https://admin.alapi.cn/api_manager/token_manager alApiToken: xxx + # urlc.cn的Token,用来生成短链接 + urlcApiToken: xxx # 系统访问域名 domain: https://wechat.abc.com # 添加新好友或群之后通知给指定的人 diff --git a/config/system.go b/config/system.go index d9475e4..5a27d6a 100644 --- a/config/system.go +++ b/config/system.go @@ -4,6 +4,7 @@ package config type system struct { Domain string `json:"domain" yaml:"domain"` // 域名 AlApiToken string `json:"alApiToken" yaml:"alApiToken"` // AL API Token + UrlcApiToken string `json:"urlcApiToken" yaml:"urlcApiToken"` // urlc.cn API Token NewFriendNotify newFriendNotify `json:"newFriendNotify" yaml:"newFriendNotify"` // 新好友通知 DefaultRule defaultRule `json:"defaultRule" yaml:"defaultRule"` // 默认规则 } diff --git a/model/dto/urlc.go b/model/dto/urlc.go new file mode 100644 index 0000000..65ba6a0 --- /dev/null +++ b/model/dto/urlc.go @@ -0,0 +1,8 @@ +package dto + +// ShortUrlResponse +// @description: 短链接返回结构体 +type ShortUrlResponse struct { + Error int `json:"error"` // 错误码,1表示发生错误,0则正常 + Short string `json:"short"` // 短链接 +} diff --git a/tasks/hottop/hottop.go b/tasks/hottop/hottop.go index 6de3175..044e36b 100644 --- a/tasks/hottop/hottop.go +++ b/tasks/hottop/hottop.go @@ -75,7 +75,10 @@ func getTopData() (data []string) { } d.Channel = "百度" newDatas = append(newDatas, d) - data = append(data, fmt.Sprintf("标题: %s\n热度: %s\n详情: %s", d.Title, d.Hot, d.Url)) + shortUrl := utils.GenShortUrl(d.Url) + if shortUrl != "" { + data = append(data, fmt.Sprintf("标题: %s\n热度: %s\n详情: %s", d.Title, d.Hot, shortUrl)) + } } // 保存新数据到数据库 if len(newDatas) > 0 { diff --git a/utils/urlc.go b/utils/urlc.go new file mode 100644 index 0000000..76df123 --- /dev/null +++ b/utils/urlc.go @@ -0,0 +1,37 @@ +package utils + +import ( + "encoding/json" + "github.com/go-resty/resty/v2" + "go-wechat/config" + "go-wechat/model/dto" + "log" +) + +// GenShortUrl +// @description: 生成短链接 +// @param url +// @return shortUrl +func GenShortUrl(url string) (shortUrl string) { + // 组装参数 + param := map[string]any{ + "url": url, + } + pbs, _ := json.Marshal(param) + + var respData dto.ShortUrlResponse + res := resty.New() + resp, err := res.R(). + SetHeader("Content-Type", "application/json;chartset=utf-8"). + SetAuthScheme("Token"). + SetAuthToken(config.Conf.System.UrlcApiToken). + SetBody(string(pbs)). + SetResult(&respData). + Post("https://www.urlc.cn/api/url/add") + if err != nil { + log.Printf("短链接获取失败: %s", err.Error()) + return + } + log.Printf("短链接获取结果: %s", unicodeToText(resp.String())) + return respData.Short +} -- 2.45.2