You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.0 KiB
Go

package api
import (
"encoding/json"
"fmt"
"mygo/conf"
"strconv"
"github.com/asmcos/requests"
"github.com/fatih/color"
)
func GenID(tag string) int {
url := fmt.Sprintf("http://%s:%d/api/segment/get/%s", conf.IP, conf.PORT, tag)
resp, err := requests.Get(url)
if err != nil {
panic(err)
}
id, err := strconv.Atoi(resp.Text())
if err != nil {
panic(err)
}
return id
}
type TokenResp struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
}
// https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET
func GetAccessToken(appid, secret string) (string, error) {
result := TokenResp{}
url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s", appid, secret)
response, err := requests.Get(url)
if err != nil {
return "", err
}
err1 := json.Unmarshal(response.Content(), &result) //解析json字符串
if err1 != nil {
return "", err
}
return result.AccessToken, err
}
type Req struct {
Path string `json:"path"`
Query string `json:"query"`
ExpireType int64 `json:"expire_type"`
ExpireInterval int64 `json:"expire_interval"`
EnvVersion string `json:"env_version"`
}
type Resp struct {
Errcode int64 `json:"errcode"`
Errmsg string `json:"errmsg"`
URLLink string `json:"url_link"`
}
//https://api.weixin.qq.com/wxa/generate_urllink?access_token=ACCESS_TOKEN
func GetShareUrl(access_token string, body Req) (Resp, error) {
result := Resp{}
// url := fmt.Sprintf("https://api.weixin.qq.com/wxa/generate_urllink?access_token=%s", access_token)
url := fmt.Sprintf("http://127.0.0.1:8899/generate_urllink?access_token=%s", access_token)
// color.Red("GetShareUrl url = %v", url)
req := requests.Requests()
// req.Debug = 1
response, err := req.PostJson(url, body)
if err != nil {
color.Red("%v", err)
return result, err
}
err1 := json.Unmarshal(response.Content(), &result) //解析json字符串
if err1 != nil {
return result, err1
}
return result, nil
}