🎨 优化热榜的链接为短链

This commit is contained in:
李寻欢
2024-07-16 11:58:52 +08:00
parent 9e4f151623
commit 78450aeace
5 changed files with 52 additions and 1 deletions

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
}