diff --git a/config.yaml b/config.yaml index 82b76b90..41e0fab6 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 d9475e42..5a27d6ab 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 00000000..65ba6a0a --- /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 6de3175a..044e36b3 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 00000000..76df1231 --- /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 +}