日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WX公众号授权

發布時間:2024/9/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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之后就可以進行自己業務處理了,從體驗上來說用戶無感,沒有上面那個授權頁,個人認為此種授權方式比較適合較為獨立或者正在考慮脫離微信的網站,?

以snsapi_base為scope發起的網頁授權,是用來獲取進入頁面的用戶的openid的,并且是靜默授權并自動跳轉到回調頁的。用戶感知的就是直接進入了回調頁(往往是業務頁面)

授權登錄

此方式可以獲取比較多的信息,例如頭像、昵稱、openid、unionid、是否關注公眾號等信息?

以snsapi_userinfo為scope發起的網頁授權,是用來獲取用戶的基本信息的。但這種授權需要用戶手動同意,并且由于用戶同意過,所以無須關注,就可在授權后獲取該用戶的基本信息。

掃碼登錄

此種方式一般適用于電商網站的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 獲取網頁授權的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 ){//需要做一個隊列數據庫了//$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 );}}//設置登錄態$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();}

整體流程

總結

以上是生活随笔為你收集整理的WX公众号授权的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。