1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-04 12:52:27 +08:00
Files
wechat/officialaccount/basic/short_url.go
markwang d28ca4f334 feat: optimized-error-handling&remove unused constant (#772)
* feat: optimized-error-handling&remove unused constant

* feat: optimized-error-handling

---------

Co-authored-by: markwang <www.wang61@qq.com>
2024-04-16 10:51:51 +08:00

50 lines
1.1 KiB
Go

package basic
import (
"fmt"
"github.com/silenceper/wechat/v2/util"
)
const (
// 将一条长链接转成短链接
// https://developers.weixin.qq.com/doc/offiaccount/Account_Management/URL_Shortener.html
long2shortURL = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=%s"
long2shortAction = "long2short"
)
type (
reqLong2ShortURL struct {
Action string `json:"action"`
LongURL string `json:"long_url"`
}
resplong2ShortURL struct {
ShortURL string `json:"short_url"`
util.CommonError
}
)
// Long2ShortURL 将一条长链接转成短链接
func (basic *Basic) Long2ShortURL(longURL string) (shortURL string, err error) {
var (
req = &reqLong2ShortURL{
Action: long2shortAction,
LongURL: longURL,
}
resp = new(resplong2ShortURL)
ac, uri string
responseBytes []byte
)
ac, err = basic.GetAccessToken()
if err != nil {
return
}
uri = fmt.Sprintf(long2shortURL, ac)
responseBytes, err = util.PostJSON(uri, req)
if err != nil {
return
}
err = util.DecodeWithError(responseBytes, resp, long2shortAction)
return resp.ShortURL, err
}