🎨 优化热榜的链接为短链 #82

Merged
李寻欢 merged 1 commits from hotfix into main 2024-07-16 11:59:19 +08:00
5 changed files with 52 additions and 1 deletions
Showing only changes of commit 78450aeace - Show all commits

View File

@ -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
# 添加新好友或群之后通知给指定的人

View File

@ -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"` // 默认规则
}

8
model/dto/urlc.go Normal file
View File

@ -0,0 +1,8 @@
package dto
// ShortUrlResponse
// @description: 短链接返回结构体
type ShortUrlResponse struct {
Error int `json:"error"` // 错误码1表示发生错误0则正常
Short string `json:"short"` // 短链接
}

View File

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

37
utils/urlc.go Normal file
View File

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