You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
wechatopen/cgi-bin.shorturl.go

41 lines
1.4 KiB

2 years ago
package wechatopen
import (
"encoding/json"
"fmt"
"go.dtapp.net/gorequest"
"net/http"
)
type CgiBinShortUrlResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
ShortUrl string `json:"short_url"` // 短链接。
}
type CgiBinShortUrlResult struct {
Result CgiBinShortUrlResponse // 结果
Body []byte // 内容
Http gorequest.Response // 请求
Err error // 错误
}
2 years ago
func newCgiBinShortUrlResult(result CgiBinShortUrlResponse, body []byte, http gorequest.Response, err error) *CgiBinShortUrlResult {
2 years ago
return &CgiBinShortUrlResult{Result: result, Body: body, Http: http, Err: err}
}
// CgiBinShortUrl 将二维码长链接转成短链接
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/qrcode/shorturl.html
2 years ago
func (c *Client) CgiBinShortUrl(longUrl string) *CgiBinShortUrlResult {
2 years ago
// 参数
2 years ago
params := gorequest.NewParams()
2 years ago
params["action"] = "long2short" // 此处填long2short代表长链接转短链接
params["long_url"] = longUrl // 需要转换的长链接支持http://、https://、weixin://wxpay 格式的url
// 请求
2 years ago
request, err := c.request(fmt.Sprintf(apiUrl+"/cgi-bin/shorturl?access_token=%s", c.GetAuthorizerAccessToken()), params, http.MethodPost)
2 years ago
// 定义
var response CgiBinShortUrlResponse
err = json.Unmarshal(request.ResponseBody, &response)
2 years ago
return newCgiBinShortUrlResult(response, request.ResponseBody, request, err)
2 years ago
}