forked from dyxushuai/wechat_component
/
regular_api.go
86 lines (72 loc) · 1.91 KB
/
regular_api.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
// regular tasks api
// these api will be run on time
package wechat
import (
"fmt"
"log"
"github.com/franela/goreq"
)
type RegularApi interface {
GetAccessToken(ticket string) (string, float64)
GetPreAuthCode(accessToken string) (string, float64)
}
type regularApi struct {
wt *WechatThird
}
// 获取第三方平台令牌
func (ra *regularApi) GetAccessToken(ticket string) (string, float64) {
postData := struct {
Component_appid string `json:"component_appid"`
Component_appsecret string `json:"component_appsecret"`
Component_verify_ticket string `json:"component_verify_ticket"`
}{
Component_appid: ra.wt.appId,
Component_appsecret: ra.wt.appSecret,
Component_verify_ticket: ticket,
}
res, err := goreq.Request{
Method: "POST",
Uri: apiComponentToken,
Body: postData,
}.Do()
if err != nil {
log.Println("getAccessToken api failed: ", err.Error())
return "", 0
}
result := &struct {
CAT string `json:"component_access_token"`
ExpiresIn float64 `json:"expires_in"`
}{}
err = unmarshalResponseToJson(res, result)
if err != nil {
log.Println("Parse access token failed: ", err)
}
return result.CAT, result.ExpiresIn
}
// 获取预授权码,用于公众号oauth
func (ra *regularApi) GetPreAuthCode(accessToken string) (string, float64) {
postData := struct {
Component_appid string `json:"component_appid"`
}{
Component_appid: ra.wt.appId,
}
res, err := goreq.Request{
Method: "POST",
Uri: fmt.Sprintf(apiCreatePreAuthCode, accessToken),
Body: postData,
}.Do()
if err != nil {
log.Println("getPreAuthCode api failed: ", err.Error())
return "", 0
}
result := &struct {
PAC string `json:"pre_auth_code"`
ExpiresIn float64 `json:"expires_in"`
}{}
err = unmarshalResponseToJson(res, result)
if err != nil {
log.Println("Parse pre auth token failed: ", err)
return "", 0
}
return result.PAC, result.ExpiresIn
}