WX公众号授权
思路
請求->服務器獲取code 再請求->服務器access_token? 再請求->服務器userinfo
先獲取code:微信給了一個url,里面有scope/appid/redict_url,拼接之后進行向微信請求,跳轉至redirect_url里面;
callback:微信會給我們返回code;
然后通過code/appid等再拼接一個url,進行curl請求;
微信返回給我們的信息就有openid、access_token等;
我們在通過access_token,組裝一個url,curl請求,就會獲取我們需要的用戶信息。
概述
微信登錄三種登錄方法(授權登錄、靜默登錄、掃碼登錄)
靜默登錄
用appid去獲取code然后獲取用戶的openid,拿到openid之后就可以進行自己業(yè)務處理了,從體驗上來說用戶無感,沒有上面那個授權頁,個人認為此種授權方式比較適合較為獨立或者正在考慮脫離微信的網(wǎng)站,?
以snsapi_base為scope發(fā)起的網(wǎng)頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業(yè)務頁面)
授權登錄
此方式可以獲取比較多的信息,例如頭像、昵稱、openid、unionid、是否關注公眾號等信息?
以snsapi_userinfo為scope發(fā)起的網(wǎng)頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,并且由于用戶同意過,所以無須關注,就可在授權后獲取該用戶的基本信息。
掃碼登錄
此種方式一般適用于電商網(wǎng)站的PC登錄,如下圖?
源碼
步驟
代碼
class OauthController extends BaseController {public function actionLogin(){$scope = $this->get( "scope","snsapi_base" );$appid = \Yii::$app->params['weixin']['appid'];$redirect_uri = UrlService::buildMUrl( "/oauth/callback" );$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state=#wechat_redirect";return $this->redirect( $url );}public function actionCallback(){ // 會返回一個code 通過code獲取access_token$code = $this->get( "code","" );if( !$code ){return $this->goHome();}//通過code 獲取網(wǎng)頁授權的access_token$appid = \Yii::$app->params['weixin']['appid'];$sk = \Yii::$app->params['weixin']['sk'];$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$sk}&code={$code}&grant_type=authorization_code";// 進行get請求$ret = HttpClient::get( $url );$ret = @json_decode( $ret,true ); // 返回的token值$ret_token = isset( $ret['access_token'] )?$ret['access_token']:'';if( !$ret_token ){return $this->goHome();}$openid = isset( $ret['openid'] )?$ret['openid']:'';$scope = isset( $ret['scope'] )?$ret['scope']:'';$this->setCookie( $this->auth_cookie_current_openid,$openid );$reg_bind = OauthMemberBind::find()->where([ 'openid' => $openid,'type' => ConstantMapService::$client_type_wechat ])->one();if( $reg_bind ){$member_info = Member::findOne( [ 'id' => $reg_bind['member_id'],'status' => 1 ] );if( !$member_info ){$reg_bind->delete();return $this->goHome();}if( $scope == "snsapi_userinfo" ){$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$ret_token}&openid={$openid}&lang=zh_CN";$wechat_user_info = HttpClient::get( $url );$wechat_user_info = @json_decode( $wechat_user_info,true );//這個時候做登錄特殊處理,例如更新用戶名和頭像等等新if( $member_info->avatar == ConstantMapService::$default_avatar ){//需要做一個隊列數(shù)據(jù)庫了//$wechat_user_info['headimgurl']QueueListService::addQueue( "member_avatar",['member_id' => $member_info['id'],'avatar_url' => $wechat_user_info['headimgurl'],] );}if( $member_info['nickname'] == $member_info['mobile'] ){$member_info->nickname = isset( $wechat_user_info['nickname'] )?$wechat_user_info['nickname']:$member_info->nickname;$member_info->update( 0 );}}//設置登錄態(tài)$this->setLoginStatus( $member_info );}else{$this->removeLoginStatus();}return $this->redirect( UrlService::buildMUrl( "/default/index" ) );}public function actionLogout(){$this->removeLoginStatus();$this->removeCookie( $this->auth_cookie_current_openid );return $this->goHome();}整體流程
總結
- 上一篇: 以《家乡的风俗》为题写一篇作文400字次
- 下一篇: 二炮军装一套多重啊?