-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic.go
111 lines (95 loc) · 2.63 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package basic
import (
"fmt"
"github.com/fideism/golang-wechat/officialaccount/context"
"github.com/fideism/golang-wechat/util"
)
var (
//获取微信服务器IP地址
//文档:https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_the_WeChat_server_IP_address.html
getCallbackIPURL = "https://api.weixin.qq.com/cgi-bin/getcallbackip"
getAPIDomainIPURL = "https://api.weixin.qq.com/cgi-bin/get_api_domain_ip"
//清理接口调用次数
clearQuotaURL = "https://api.weixin.qq.com/cgi-bin/clear_quota"
//shortURL短链接
shortURL = "https://api.weixin.qq.com/cgi-bin/shorturl"
)
//Basic struct
type Basic struct {
*context.Context
}
//NewBasic 实例
func NewBasic(context *context.Context) *Basic {
basic := new(Basic)
basic.Context = context
return basic
}
//IPListRes 获取微信服务器IP地址 返回结果
type IPListRes struct {
util.CommonError
IPList []string `json:"ip_list"`
}
//GetCallbackIP 获取微信callback IP地址
func (basic *Basic) GetCallbackIP() ([]string, error) {
ak, err := basic.GetAccessToken()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?access_token=%s", getCallbackIPURL, ak)
data, err := util.HTTPGet(url)
if err != nil {
return nil, err
}
ipListRes := &IPListRes{}
err = util.DecodeWithError(data, ipListRes, "GetCallbackIP")
return ipListRes.IPList, err
}
//GetAPIDomainIP 获取微信API接口 IP地址
func (basic *Basic) GetAPIDomainIP() ([]string, error) {
ak, err := basic.GetAccessToken()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s?access_token=%s", getAPIDomainIPURL, ak)
data, err := util.HTTPGet(url)
if err != nil {
return nil, err
}
ipListRes := &IPListRes{}
err = util.DecodeWithError(data, ipListRes, "GetAPIDomainIP")
return ipListRes.IPList, err
}
//ClearQuota 清理接口调用次数
func (basic *Basic) ClearQuota() error {
ak, err := basic.GetAccessToken()
if err != nil {
return err
}
url := fmt.Sprintf("%s?access_token=%s", clearQuotaURL, ak)
data, err := util.PostJSON(url, map[string]string{
"appid": basic.AppID,
})
if err != nil {
return err
}
return util.DecodeWithCommonError(data, "ClearQuota")
}
// GetShortURL 短链接
func (basic *Basic) GetShortURL(url string) (string, error) {
ak, err := basic.GetAccessToken()
if err != nil {
return "", err
}
data, err := util.PostJSON(fmt.Sprintf("%s?access_token=%s", shortURL, ak), map[string]string{
"long_url": url,
"action": "long2short",
})
var res struct {
util.CommonError
ShortURL string `json:"short_url"`
}
if err := util.DecodeWithError(data, &res, "GetShortURL"); err != nil {
return "", err
}
return res.ShortURL, nil
}