Client.php
3.68 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
<?php
/**
* 支付宝app联合登陆
* @author tongdesheng
*
*/
class YHMAuth_Package_Alimobile_Client extends YHMAuth_Package_Alimobile_Abstract{
/**
* 通过token获取用户信息,用于快捷登陆
* @param unknown $alipay_user_id
* @param unknown $auth_code
* @param unknown $cur_uid
*/
public function getUserinfoByToken($alipay_user_id, $token) {
//获取支付宝关联的账户
$userRelate = QINPassport_Models_Alipaypassport_Client::getByAlipayUserId($alipay_user_id, 1);
//有可能客户端先调用,而还没有收到服务器反馈,或者收到服务器的反馈了,但是没有创建过该用户,需要创建该用户,此时直接去支付宝服务器去认证。
if(!empty($userRelate) && !empty($userRelate['uid']) && $userRelate['token'] == $token) {
//已经获取过用户信息,且token相同,直接返回用户信息
$userInfo = QINPassport_Client::getProfile($userRelate['uid']);
if(!empty($userInfo)) {
return array(
'email' => $userInfo['email'],
'mobile' => $userInfo['mobile'],
'open_id' => $alipay_user_id
);
}
}
//需要重新根据token获取用户
$ret = $this->reqUserInfoByToken($token);
$email = ''; $user_name=''; $mobile = '';
if($ret['is_success'] == 'T') {
$email = isset($ret['response']['user_info']['email']) ? $ret['response']['user_info']['email'] : '';
$user_name = isset($ret['response']['user_info']['user_name']) ? $ret['response']['user_info']['user_name'] : '';
$mobile = isset($ret['response']['user_info']['mobile']) ? $ret['response']['user_info']['mobile'] : '';
if(empty($email) && empty($mobile)) {
error_log('通过结果解析出用户email信息', 3, '/tmp/ali_direct_log_error.log');
return array();
}
$email = empty($email) ? $mobile . '@yohoinc.com' : $email;
return array(
'email' => $email,
'mobile' => $mobile,
'user_name' => $user_name,
'open_id' => $alipay_user_id
);
}else {
//无法通过token获取用户信息,说明token无效,直接返回空用户信息
error_log('没有通过token获取到用户email信息:' . var_export($ret, true) . "\n", 3, '/tmp/ali_direct_log_error.log');
}
return array();
}
/**
* 通过授权码获取用户信息,用于钱包
* @param integer $alipay_user_id
* @param string $auth_code
*/
public function getUserInfoByAuth($alipay_user_id, $auth_code, $cur_uid=0) {
if(!empty($cur_uid)) {
$userInfo = QINPassport_Client::getProfile($cur_uid);
if(!empty($userInfo)) {
return array(
'uid' => $userInfo['uid'],
'email' => $userInfo['email'],
'mobile' => $userInfo['mobile'],
'user_name' => $userInfo['profile_name']
);
}
}
$uid = 0; $email = ''; $real_name = ''; $mobile='';
//每次都要刷新一下refresh_token,供以后获取短token使用。
$ret = $this->reqOauthToken($auth_code, '', 'authorization_code');
if(!empty($ret->alipay_system_oauth_token_response)) {
$refresh_token = $ret->alipay_system_oauth_token_response->refresh_token;
$access_token = $ret->alipay_system_oauth_token_response->access_token;
} else {
return array();
}
$ret = $this->reqAlipayUserinfo($access_token);
if(!empty($ret->alipay_user_userinfo_share_response)) {
//成功获取账户
$email = $ret->alipay_user_userinfo_share_response->email;
$real_name = $ret->alipay_user_userinfo_share_response->real_name;
$mobile = $ret->alipay_user_userinfo_share_response->mobile;
}
if(empty($email) && empty($mobile)) {
return array();
}
$email = empty($email) ? $mobile . '@yohoinc.com' : $email;
return array(
'email' => $email,
'mobile' => $mobile,
'user_name' => $real_name,
'open_id' => $alipay_user_id
);
}
}