Client.php 3.68 KB
<?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
		);
	}
	
	
}