Skip to content

Commit d63646c

Browse files
committed
add global userinfo get function(based on unionid)
1 parent 7e3012d commit d63646c

File tree

2 files changed

+110
-4
lines changed

2 files changed

+110
-4
lines changed

src/filters.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,17 @@
2424
if (Input::has('code')) {
2525
//获取openid
2626
try {
27-
get_access_token(Input::get('code'));
27+
$accessToken = get_access_token(Input::get('code'));
2828
} catch (Exception $e) {
2929
Log::error($e->getMessage());
3030
return '获取token错误';
3131
}
32+
try {
33+
$userinfo = get_global_userinfo($accessToken['openid']);
34+
Session::put('wechat_userinfo', $userinfo);
35+
} catch (Exception $e) {
36+
Log::error($e->getMessage());
37+
}
3238
return Redirect::intended(Request::fullUrl());
3339
} else {
3440
try {
@@ -64,11 +70,19 @@
6470
}
6571

6672
try {
67-
get_userinfo($token['access_token'], $token['openid']);
73+
$userinfo = get_userinfo($token['access_token'], $token['openid']);
6874
} catch (Exception $e) {
6975
Log::error($e->getMessage());
7076
return '获取userinfo错误';
7177
}
78+
try {
79+
$globalUserinfo = get_global_userinfo($token['openid']);
80+
$userinfo = array_merge($userinfo, $globalUserinfo);
81+
} catch (Exception $e) {
82+
Log::error($e->getMessage());
83+
$userinfo['subscribe'] = 0;
84+
}
85+
Session::put('wechat_userinfo', $userinfo);
7286
return Redirect::intended(Request::fullUrl());
7387
} else {
7488
try {

src/helpers.php

+94-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Xjchen\WechatFilter\WechatConfigRequiredException;
1212
use Xjchen\WechatFilter\GetAccessTokenException;
1313
use Xjchen\WechatFilter\GetUserinfoException;
14+
use Carbon\Carbon;
1415

1516
if (!function_exists('get_code_url')) {
1617

@@ -30,6 +31,23 @@ function get_code_url($appid, $redirect_uri, $scope='snsapi_userinfo')
3031
}
3132
}
3233

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+
3351
if (!function_exists('get_code_redirect')) {
3452

3553
/**
@@ -67,6 +85,23 @@ function get_access_token_url($appid, $secret, $code)
6785
}
6886
}
6987

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+
70105
if (!function_exists('get_userinfo_url')) {
71106
/**
72107
* 生成获取userinfo的url
@@ -103,14 +138,71 @@ function curl_get_wrapper($url, $return_json=true)
103138
throw new CurlGetException($error);
104139
} else {
105140
if ($return_json) {
106-
return json_decode($curl->response, true);
141+
return json_decode($curl->raw_response, true);
107142
} else {
108-
return $curl->response;
143+
return $curl->raw_response;
109144
}
110145
}
111146
}
112147
}
113148

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+
114206
if (!function_exists('get_access_token')) {
115207
/**
116208
* 获取access_token数组

0 commit comments

Comments
 (0)