-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthorizer.go
More file actions
198 lines (179 loc) · 5.65 KB
/
authorizer.go
File metadata and controls
198 lines (179 loc) · 5.65 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package component
import (
"encoding/json"
"log"
"time"
"github.com/wei193/component/wechat"
)
//Authorizer Authorizer
type Authorizer struct {
*wechat.Wechat
Component *Component
// AuthorizerAppid string
// AuthorizerAccessToken string
// AccessTokenExpires int64
AuthorizerRefreshToken string
}
//JAuthorizer 授权信息
type JAuthorizer struct {
AuthorizerInfo JAuthorizerInfo `json:"authorizer_info"`
AuthorizationInfo JAuthorizationInfo `json:"authorization_info"`
}
//JAuthorizerInfo 授权信息
type JAuthorizerInfo struct {
NickName string `json:"nick_name"`
HeadImg string `json:"head_img"`
ServiceTypeInfo JTypeInfo `json:"service_type_info"`
VerifyTypeInfo JTypeInfo `json:"verify_type_info"`
UserName string `json:"user_name"`
PrincipalName string `json:"principal_name"`
BusinessInfo JBusinessInfo `json:"business_info"`
Alias string `json:"alias"`
QrcodeURL string `json:"qrcode_url"`
}
//JBusinessInfo JBusinessInfo
type JBusinessInfo struct {
OpenStore int `json:"open_store"`
OpenScan int `json:"open_scan"`
OpenPay int `json:"open_pay"`
OpenCard int `json:"open_card"`
OpenShake int `json:"open_shake"`
}
//JAuthorizationInfo 授权信息
type JAuthorizationInfo struct {
AuthorizerAppid string `json:"authorizer_appid"`
AuthorizationAppid string `json:"authorization_appid"`
AuthorizerAccessToken string `json:"authorizer_access_token"`
ExpiresIn int `json:"expires_in"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
FuncInfo []JFuncInfo `json:"func_info"`
}
//JTypeInfo 类型信息
type JTypeInfo struct {
ID int `json:"id"`
}
//JFuncInfo 功能列表
type JFuncInfo struct {
FuncscopeCategory JFuncscopeCategory `json:"funcscope_category"`
}
// JFuncscopeCategory JFuncscopeCategory
type JFuncscopeCategory struct {
ID int `json:"id"`
}
//JAuthorizerAccessToken JAuthorizerAccessToken
type JAuthorizerAccessToken struct {
AuthorizerAccessToken string `json:"authorizer_access_token"`
ExpiresIn int `json:"expires_in"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
}
//JUserAccessToken 用户AccessToken
type JUserAccessToken struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Openid string `json:"openid"`
Scope string `json:"scope"`
}
//NewAuthorizer 新建Authorizer
func (c *Component) NewAuthorizer(appid, accesstoken string, tokenexpires int64, refreshtoken string) (authorizer *Authorizer, err error) {
authorizer = &Authorizer{
Component: c,
// AuthorizerAppid: appid,
// AuthorizerAccessToken: accesstoken,
// AccessTokenExpires: tokenexpires,
AuthorizerRefreshToken: refreshtoken,
Wechat: &wechat.Wechat{
Appid: appid,
AccessToken: accesstoken,
AccessTokenExpires: tokenexpires,
},
}
return
}
//GetAuthorizerInfo 获取授权详细信息
func (a *Authorizer) GetAuthorizerInfo() (authorizer *JAuthorizer, err error) {
type st struct {
ComponentAppid string `json:"component_appid"`
AuthorizerAppid string `json:"authorizer_appid"`
}
d := st{
ComponentAppid: a.Component.ComponentAppid,
AuthorizerAppid: a.Wechat.Appid,
}
req, err := createRequset("https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info?component_access_token="+a.Component.ComponentAccessToken,
"POST", nil, d)
if err != nil {
return nil, err
}
res, err := requsetJosn(req)
if err != nil {
return nil, err
}
log.Println(string(res))
var auth JAuthorizer
err = json.Unmarshal(res, &auth)
if err != nil {
return nil, err
}
return &auth, nil
}
//GetAuthorizerAccessToken 获取Authorizer AccessToken
func (a *Authorizer) GetAuthorizerAccessToken() (token *JAuthorizerAccessToken, err error) {
type st struct {
ComponentAppid string `json:"component_appid"`
AuthorizerAppid string `json:"authorizer_appid"`
AuthorizerRefreshToken string `json:"authorizer_refresh_token"`
}
d := st{
ComponentAppid: a.Component.ComponentAppid,
AuthorizerAppid: a.Appid,
AuthorizerRefreshToken: a.AuthorizerRefreshToken,
}
req, err := createRequset("https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token="+a.Component.ComponentAccessToken,
"POST", nil, d)
if err != nil {
return nil, err
}
res, err := requsetJosn(req)
if err != nil {
return nil, err
}
log.Println(string(res))
token = new(JAuthorizerAccessToken)
err = json.Unmarshal(res, token)
if err != nil {
return nil, err
}
a.AccessToken = token.AuthorizerAccessToken
a.AuthorizerRefreshToken = token.AuthorizerRefreshToken
a.AccessTokenExpires = time.Now().Unix() + int64(token.ExpiresIn)
return token, nil
}
//CodeToAccessToken 通过code换取access_token
func (a *Authorizer) CodeToAccessToken(code string) (token *JUserAccessToken, err error) {
param := make(map[string]string)
param["appid"] = a.Appid
param["code"] = code
param["grant_type"] = "authorization_code"
param["component_appid"] = a.Component.ComponentAppid
param["component_access_token"] = a.Component.ComponentAccessToken
req, err := createRequset("https://api.weixin.qq.com/sns/oauth2/component/access_token",
"GET", param, nil)
if err != nil {
return nil, err
}
res, err := requsetJosn(req)
if err != nil {
return nil, err
}
token = new(JUserAccessToken)
err = json.Unmarshal(res, token)
if err != nil {
return nil, err
}
return token, nil
}
//GetWecaht 获取微信方法
func (a *Authorizer) GetWecaht() (w *wechat.Wechat) {
return a.Wechat
}