11
11
use Xjchen \WechatFilter \WechatConfigRequiredException ;
12
12
use Xjchen \WechatFilter \GetAccessTokenException ;
13
13
use Xjchen \WechatFilter \GetUserinfoException ;
14
+ use Carbon \Carbon ;
14
15
15
16
if (!function_exists ('get_code_url ' )) {
16
17
@@ -30,6 +31,23 @@ function get_code_url($appid, $redirect_uri, $scope='snsapi_userinfo')
30
31
}
31
32
}
32
33
34
+ if (!function_exists ('get_global_access_token_url ' )) {
35
+
36
+ /**
37
+ * 生成获取access_token的url
38
+ *
39
+ * @param $appid
40
+ * @param $secret
41
+ * @return mixed
42
+ */
43
+ function get_global_access_token_url ($ appid , $ secret )
44
+ {
45
+ $ template_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET ' ;
46
+ $ url = str_replace (['APPID ' , 'APPSECRET ' ], [$ appid , $ secret ], $ template_url );
47
+ return $ url ;
48
+ }
49
+ }
50
+
33
51
if (!function_exists ('get_code_redirect ' )) {
34
52
35
53
/**
@@ -67,6 +85,23 @@ function get_access_token_url($appid, $secret, $code)
67
85
}
68
86
}
69
87
88
+ if (!function_exists ('get_global_userinfo_url ' )) {
89
+ /**
90
+ * 生成获取userinfo的url
91
+ *
92
+ * @param $access_token
93
+ * @param $openid
94
+ * @param string $lang
95
+ * @return mixed
96
+ */
97
+ function get_global_userinfo_url ($ access_token , $ openid , $ lang ='zh_CN ' )
98
+ {
99
+ $ template_url = 'https://api.weixin.qq.com/cgi-bin/user/info?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN ' ;
100
+ $ url = str_replace (['ACCESS_TOKEN ' , 'OPENID ' , 'LANG ' ], [$ access_token , $ openid , $ lang ], $ template_url );
101
+ return $ url ;
102
+ }
103
+ }
104
+
70
105
if (!function_exists ('get_userinfo_url ' )) {
71
106
/**
72
107
* 生成获取userinfo的url
@@ -103,14 +138,71 @@ function curl_get_wrapper($url, $return_json=true)
103
138
throw new CurlGetException ($ error );
104
139
} else {
105
140
if ($ return_json ) {
106
- return json_decode ($ curl ->response , true );
141
+ return json_decode ($ curl ->raw_response , true );
107
142
} else {
108
- return $ curl ->response ;
143
+ return $ curl ->raw_response ;
109
144
}
110
145
}
111
146
}
112
147
}
113
148
149
+ if (!function_exists ('get_global_access_token ' )) {
150
+ /**
151
+ * 获取global_access_token数组
152
+ *
153
+ * @return mixed|null
154
+ * @throws CurlGetException
155
+ * @throws GetAccessTokenException
156
+ * @throws WechatConfigRequiredException
157
+ */
158
+ function get_global_access_token ()
159
+ {
160
+ if (Cache::has ('global_access_token ' )) {
161
+ return Cache::get ('global_access_token ' );
162
+ }
163
+ $ appid = Config::get ('wechat.appid ' , '' );
164
+ $ secret = Config::get ('wechat.secret ' , '' );
165
+ if (!$ appid or !$ secret ) {
166
+ throw new WechatConfigRequiredException ('get_global_access_token中缺少appid或secret ' );
167
+ }
168
+ $ getAccessTokenUrl = get_global_access_token_url ($ appid , $ secret );
169
+ $ result = curl_get_wrapper ($ getAccessTokenUrl );
170
+ if (isset ($ result ['errcode ' ])) {
171
+ throw new GetAccessTokenException ('get_global_access_token get access token error: ' .json_encode ($ result ));
172
+ }
173
+ $ expiresAt = Carbon::now ()->addMinutes (($ result ['expires_in ' ]/60 )-1 );
174
+ Cache::put ('global_access_token ' , $ result ['access_token ' ], $ expiresAt );
175
+ return $ result ['access_token ' ];
176
+ }
177
+ }
178
+
179
+ if (!function_exists ('get_global_userinfo ' )) {
180
+
181
+ /**
182
+ * 获取userinfo
183
+ *
184
+ * @param $openid
185
+ * @param string $lang
186
+ * @return mixed|null
187
+ * @throws CurlGetException
188
+ * @throws GetUserinfoException
189
+ */
190
+
191
+ function get_global_userinfo ($ openid , $ lang ='zh_CN ' )
192
+ {
193
+ $ access_token = get_global_access_token ();
194
+ $ getUserinfoUrl = get_global_userinfo_url ($ access_token , $ openid , $ lang );
195
+ $ result = curl_get_wrapper ($ getUserinfoUrl );
196
+ if (isset ($ result ['errcode ' ])) {
197
+ throw new GetUserinfoException ('get_global_userinfo error: ' .json_encode ($ result ));
198
+ }
199
+ Session::put ('global_wechat_userinfo ' , $ result );
200
+ return $ result ;
201
+ }
202
+ }
203
+
204
+
205
+
114
206
if (!function_exists ('get_access_token ' )) {
115
207
/**
116
208
* 获取access_token数组
0 commit comments