-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpre_auth_code.go
156 lines (140 loc) · 4.59 KB
/
pre_auth_code.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package wechat3rd
import (
"errors"
"fmt"
"net/url"
"github.com/l306287405/wechat3rd/core"
)
type AuthType string
const (
PREAUTH_CODE_URL = WECHAT_API_URL + "/cgi-bin/component/api_create_preauthcode?component_access_token=%s"
WEB_AUTH_URL = WECHAT_MP_URL + "/cgi-bin/componentloginpage?component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%s"
MOBILE_AUTH_URL = WECHAT_MP_URL + "/safe/bindcomponent?action=bindcomponent&no_scan=1&component_appid=%s&pre_auth_code=%s&redirect_uri=%s&auth_type=%s"
QUERY_AUTH_URL = WECHAT_API_URL + "/cgi-bin/component/api_query_auth?component_access_token=%s"
REFRESH_TOKEN_URL = WECHAT_API_URL + "/cgi-bin/component/api_authorizer_token?component_access_token=%s"
PREAUTH_AUTH_TYPE_All AuthType = "3" // 全部
PREAUTH_AUTH_TYPE_MINIAPP AuthType = "2" // 小程序
PREAUTH_AUTH_TYPE_Service AuthType = "1" // 公众号
)
type PreAuthCodeReq struct {
ComponentAppid string `json:"component_appid"`
}
type PreAuthCodeResp struct {
core.Error
PreAuthCode string `json:"pre_auth_code"`
ExpiresIn int `json:"expires_in"`
}
func (s *Server) PreAuthCode() (resp *PreAuthCodeResp) {
resp = &PreAuthCodeResp{}
token, err := s.Token()
if err != nil {
resp.Err(err)
return
}
req := &PreAuthCodeReq{
ComponentAppid: s.cfg.AppID,
}
resp.Err(core.PostJson(getCompleteUrl(PREAUTH_CODE_URL, token), req, resp))
return
}
//说明
//https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/Before_Develop/Authorization_Process_Technical_Description.html
func (s *Server) AuthUrl(isWebAuth bool, redirectUri string, authType AuthType, bizAppid *string) (u string, err error) {
var (
resp *PreAuthCodeResp
)
resp = s.PreAuthCode()
if !resp.Success() {
err = errors.New(resp.ErrMsg)
return
}
tPreAuthCode := url.QueryEscape(resp.PreAuthCode)
redirectUri = url.QueryEscape(redirectUri)
if isWebAuth {
u = fmt.Sprintf(WEB_AUTH_URL, s.cfg.AppID, tPreAuthCode, redirectUri, authType)
} else {
u = fmt.Sprintf(MOBILE_AUTH_URL, s.cfg.AppID, tPreAuthCode, redirectUri, authType)
if bizAppid != nil && *bizAppid != "" {
u = u + "&biz_appid=" + *bizAppid
}
u += "#wechat_redirect"
}
return u, nil
}
type QueryAuthReq struct {
ComponentAppid string `json:"component_appid"`
AuthorizationCode string `json:"authorization_code"`
}
type QueryAuthResp struct {
core.Error
AuthorizationInfo struct {
AuthorizerAppid string `json:"authorizer_appid"`
AuthorizerAccessToken string `json:"authorizer_access_token"`
ExpiresIn int `json:"expires_in"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
FuncInfo []struct {
FuncscopeCategory struct {
ID int `json:"id"`
} `json:"funcscope_category"`
} `json:"func_info"`
} `json:"authorization_info"`
}
// 返回授权数据
func (s *Server) QueryAuth(code string) (resp *QueryAuthResp) {
resp = &QueryAuthResp{}
token, err := s.Token()
if err != nil {
resp.Err(err)
return
}
req := &QueryAuthReq{
ComponentAppid: s.cfg.AppID,
AuthorizationCode: code,
}
resp.Err(core.PostJson(getCompleteUrl(QUERY_AUTH_URL, token), req, resp))
return
}
type RefreshTokenReq struct {
ComponentAppid string `json:"component_appid"`
AuthorizerAppid string `json:"authorizer_appid"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
}
type RefreshTokenResp struct {
core.Error
AuthorizerAccessToken string `json:"authorizer_access_token"`
ExpiresIn int64 `json:"expires_in"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
}
// 刷新token
func (s *Server) RefreshToken(authAppID, refreshToken string) (resp *RefreshTokenResp) {
resp = &RefreshTokenResp{}
token, err := s.Token()
if err != nil {
resp.Err(err)
return
}
req := &RefreshTokenReq{
ComponentAppid: s.cfg.AppID,
AuthorizerAppid: authAppID,
AuthorizerRefreshToken: refreshToken,
}
resp.Err(core.PostJson(getCompleteUrl(REFRESH_TOKEN_URL, token), req, resp))
return
}
// 启动ticket推送服务
// https://developers.weixin.qq.com/doc/oplatform/Third-party_Platforms/2.0/api/ThirdParty/token/component_verify_ticket_service.html
func (s *Server) ApiStartPushTicket() (resp *core.Error) {
var (
u = CGIUrl + "/component/api_start_push_ticket"
req = &struct {
ComponentAppid string `json:"component_appid"`
ComponentSecret string `json:"component_secret"`
}{ComponentAppid: s.cfg.AppID, ComponentSecret: s.cfg.AppSecret}
)
resp = &core.Error{}
resp.Err(core.PostJson(u, req, resp))
return
}
func getCompleteUrl(uri, token string) string {
return fmt.Sprintf(uri, token)
}