//授权回调
public function auth(){
$params = $this->input->get(NULL,true);
if(isset($params['redirect_url'])){
$this->session->set_userdata('alipay_XXX',$params['redirect_XXX']);
}
if(isset($params['wx_uri'])){
$this->session->set_userdata('alipay_wx_XXX',$params['wx_XXX']);
}
if(isset($params['auth_XXX'])){
$this->session->set_userdata('alipay_auth_XXX',$params['auth_XXX']);
}
$auth_type = isset($params['auth_XXX']) ? $params['auth_XXX'] :$this->session->userdata('alipay_auth_XXX');//授权类型,如果需要静默授权请传值过来否则默认为auth_userinfo
$auth_type = $auth_type ? $auth_type :'auth_userinfo';
if(!isset($params['auth_code'])){
$redirect_url = urlencode(AUTH_DOMAIN.'auth/');
$auth_url = ALIPAY_AUTH_URL ."?app_id={$this->alipay_appid}&scope={$auth_type}&redirect_uri={$redirect_url}&state=XXX";
header("Location:$auth_url");exit;
}else{
$auth_code = $_GET['auth_code'];
$http_query = array(
'app_id' => $this->alipay_appid,
'method' => '',
'charset'=> $this->AopClient->charset,
'sign_type'=>'RSA',
'timestamp'=>date('Y-m-d H:i:s'),
'version'=> 1.0,
'grant_type'=>'authorization_code',
'code'=>$auth_code
);
$user_url = ALIPAY_API_URL ;
$sign = $this->AopClient->rsaSign($http_query);
$http_query['sign'] = $sign;
$res_str = $this->curl->simple_post($user_url,$http_query);
$res_str = iconv('GBK','UTF-8',$res_str);
$res_arr = json_decode($res_str,true);
//授权出错 终止授权
if(isset($res_arr['error_response'])||(isset($res_arr['alipay_system_oauth_token_response']) && isset($res_arr['alipay_system_oauth_token_response']['code']))){
$err_code = isset($res_arr['error_response'])? $res_arr['error_response']['code'] : $res_arr['alipay_system_oauth_token_response']['code'];
die("支付宝授权出错【出错码{$err_code}】");
}
//静默授权直接拼接数据
if($auth_type=='auth_base'){
$userinfo['openid'] = $res_arr['alipay_system_oauth_token_response']['user_id'];
$userinfo['nickname'] ='XXX';
$userinfo['headimgurl'] ='';
$userinfo['sex'] ='2';
}else{
$access_token = $res_arr['alipay_system_oauth_token_response']['access_token'];
$user_query = array(
'method'=>'',
'timestamp'=> date('Y-m-d H:i:s'),
'app_id'=>$this->alipay_appid,
'auth_token'=>$access_token,
'charset'=>$this->AopClient->charset,
'version'=>'1.0',
'sign_type'=>'RSA'
);
$usersign = $this->AopClient->rsaSign($user_query);
$user_query['sign'] = $usersign;
$user_info_str = $this->curl->simple_post($user_url,$user_query);
$user_info_str = iconv('GBK','UTF-8',$user_info_str);
$user_info = json_decode($user_info_str,true);
if(isset($user_info['error_response'])||(isset($user_info['alipay_user_userinfo_share_response']) && isset($user_info['alipay_user_userinfo_share_response']['code']))){
$err_code = isset($user_info['error_response'])? $user_info['error_response']['code'] : $user_info['alipay_user_userinfo_share_response']['code'];
}
$userinfo = array(
'openid' => $user_info['alipay_user_userinfo_share_response']['user_id'],
'nickname'=> $user_info['alipay_user_userinfo_share_response']['real_name'],
'headimgurl' =>$user_info['alipay_user_userinfo_share_response']['avatar'],
'sex' =>$user_info['alipay_user_userinfo_share_response']['gender'] == 'F' ? 2 : 1,
);
}
if(!$this->ci_redis->get($params['auth_code']) || $this->ci_redis->get($params['auth_code']) == 'false'){
$this->ci_redis->set($params['auth_code'],json_encode($userinfo),300);
}
$redirect_url = base64_decode($this->session->userdata('alipay_redirect_url'));
$wx_uri = $this->session->userdata('alipay_wx_XXX');
header("Location:".$redirect_url.'?code='.$params['auth_code'].'&wx_uri='.$wx_uri);exit;
}
}