1:先獲取微信code,直接將本鏈接點擊后,微信會提示請通過認證頁面,點擊后,就會自動跳轉到http://www.ssss.com/pub/thirdlogin/這個方法中去,並且會在連接中帶有code參數
<a class="ico_wx" href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=787878778&redirect_uri=http://www.ssss.com/pub/thirdlogin/?type=wechat&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"><em></em><span>微信</span></a>
2:通過上步點擊鏈接,同意授權后,他會自動跳轉到本方法中
/**
* 第三方登陸跳轉
*/
public function action_thirdlogin()
{
$type = Arr::get($_GET, 'type');
//如果是微信登錄
if($type == "wechat"){
$appid = $GLOBALS['cfg_weixi_appkey'];
$appsecret = $GLOBALS['cfg_weixi_appsecret'];
$code = Arr::get($_GET, 'code');//這就是獲取的code
if (empty($code)) {
Common::session('login_num', 1);
$refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
Common::message(array('message' => '登陸失敗!', 'jumpUrl' => $refer_url ));
die;
}
$rs = Common::wechat_login($appid, $appsecret,$code);//這就是通過code獲取用戶信息的方法,只需調用即可,里面包含用戶信息,opemid
if (empty($rs)) {
Common::session('login_num', 1);
$refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
Common::message(array('message' => '登陸失敗!', 'jumpUrl' => $refer_url ));
die;
}
Common::session('captcha_response', null);
//在數據庫用戶表中建立wechatcode字段,在注冊后,讓用戶綁定微信信息,在綁定的時候也是獲取code,然后通過,方法獲取openid,存進去存到這個字段中去
//在這里通過openid查詢wechat這個字段用戶信息(本openid有沒有在這個字段中),進行判短 ,本登錄的微信有沒有在用戶表里面存在,如果沒存在讓他跳轉登錄界面,如果存在了,
//那么給他吧用戶信息查詢出來存到session中去
//然后跳轉到個人中心,微信登錄就這么簡單
$member = DB::select('*')->from('member')->where('wechatcode', '=', $rs['openid'])->execute()->current();
if (empty($member))
{
Common::session('login_num', 1);
$refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member/login";
Common::message(array('message' => '當前微信未綁定!', 'jumpUrl' => $refer_url ));//如果查不到這個微信信息,跳轉登錄讓他注冊並綁定微信
die;
}
else
{
Model_Member::write_session($member, $member['nickname']);//存session
//清空登錄次數
Common::session('login_num', null);
$refer_url = $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : $this->cmsurl."member";
Common::message(array('message' => '登陸成功!', 'jumpUrl' => $refer_url ));//注冊成功
}
}
}
:通過code換取網頁授權access_token,:拉取用戶信息(需scope為 snsapi_userinfo)
已經整理了方法:本方法是在獲取code跳轉到thirdlogin這個方法后,調用本方法進行獲取用戶信息用的,請看下文
/**
*
* 微信授權
*/
public static function wechat_login($appid, $appsecret,$code)
{
$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';
$token = json_decode(file_get_contents($token_url));
if (isset($token->errcode)) {
return false;
}
$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;
//轉成對象
$access_token = json_decode(file_get_contents($access_token_url));
if (isset($access_token->errcode)) {
return false;
}
$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';
//轉成對象
$user_info = json_decode(file_get_contents($user_info_url));
if (isset($user_info->errcode)) {
return false;
}
$result = json_decode(json_encode($user_info),true);//返回的json數組轉換成array數組
return $result;
//最后返回的就是個人信息
}
