🎨 优化热榜的链接为短链
This commit is contained in:
37
utils/urlc.go
Normal file
37
utils/urlc.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user