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

53 lines
1.6 KiB

2 years ago
package wechatopen
import (
2 years ago
"context"
2 years ago
"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 // 请求
}
2 years ago
func newCgiBinShortUrlResult(result CgiBinShortUrlResponse, body []byte, http gorequest.Response) *CgiBinShortUrlResult {
return &CgiBinShortUrlResult{Result: result, Body: body, Http: http}
2 years ago
}
// CgiBinShortUrl 将二维码长链接转成短链接
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/qrcode/shorturl.html
2 years ago
func (c *Client) CgiBinShortUrl(ctx context.Context, longUrl string) (*CgiBinShortUrlResult, error) {
// 检查
err := c.checkComponentIsConfig()
if err != nil {
return nil, err
}
err = c.checkAuthorizerIsConfig()
if err != nil {
return nil, err
}
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(ctx, fmt.Sprintf(apiUrl+"/cgi-bin/shorturl?access_token=%s", c.GetAuthorizerAccessToken(ctx)), params, http.MethodPost)
2 years ago
// 定义
var response CgiBinShortUrlResponse
err = json.Unmarshal(request.ResponseBody, &response)
2 years ago
if err != nil {
return nil, err
}
return newCgiBinShortUrlResult(response, request.ResponseBody, request), nil
2 years ago
}