github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/base/shorturl.go (about) 1 package base 2 3 import ( 4 "github.com/chanxuehong/wechat/mp/core" 5 ) 6 7 // ShortURL 将一条长链接转成短链接. 8 func ShortURL(clt *core.Client, longURL string) (shortURL string, err error) { 9 const incompleteURL = "https://api.weixin.qq.com/cgi-bin/shorturl?access_token=" 10 11 var request = struct { 12 Action string `json:"action"` 13 LongURL string `json:"long_url"` 14 }{ 15 Action: "long2short", 16 LongURL: longURL, 17 } 18 var result struct { 19 core.Error 20 ShortURL string `json:"short_url"` 21 } 22 if err = clt.PostJSON(incompleteURL, &request, &result); err != nil { 23 return 24 } 25 if result.ErrCode != core.ErrCodeOK { 26 err = &result.Error 27 return 28 } 29 shortURL = result.ShortURL 30 return 31 }