go-wxhelper/utils/urlc.go

38 lines
841 B
Go
Raw Normal View History

2024-07-16 11:58:52 +08:00
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)).
Post("https://www.urlc.cn/api/url/add")
if err != nil {
log.Printf("短链接获取失败: %s", err.Error())
return
}
2024-07-16 13:30:06 +08:00
log.Printf("短链接获取结果: %s", resp.String())
_ = json.Unmarshal(resp.Body(), &respData)
2024-07-16 11:58:52 +08:00
return respData.Short
}