步驟如圖:

第一步:用戶同意授權,獲取code

第二步:通過code換取網頁授權access_token

第三步:刷新access_token

第" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網站運營 > 微信網頁登錄授權流程都不清楚,還說自己是3年前端?

微信網頁登錄授權流程都不清楚,還說自己是3年前端?

時間:2022-08-06 21:21:01 | 來源:網站運營

時間:2022-08-06 21:21:01 來源:網站運營

前段時間,面試官問到微信網頁登錄的授權流程分為幾步,一面懵逼的我不知所措...
下面是來自微信開發(fā)文檔

步驟如圖:

第一步:用戶同意授權,獲取code

第二步:通過code換取網頁授權access_token

第三步:刷新access_token

第四步:拉取用戶信息

接下來我將實際項目的代碼粘出來, 前端nuxt.js

前端部分

在插件plugins 下app.js中封裝方法

export default ({ app }, inject) => { inject('wxLogin', (pageURI) => { let url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + process.env.WXPAGEAPPID + "&scope=snsapi_userinfo&state=STATE&redirect_uri=" + encodeURIComponent(pageURI); return url })}


methods:{ login() { let url = location.href.split("#")[0]; location.href = this.$wxLogin(url); }}通過此微信登錄授權重定向當前頁面,會拿到code,將其傳入后端

后端部分

<?phpnamespace thago/util;use fast/Http;use think/Session;class Wechat{ private $app_id = ''; private $app_secret = ''; private $scope = 'snsapi_userinfo'; public function __construct($app_id, $app_secret) { $this->app_id = $app_id; $this->app_secret = $app_secret; } /** * 獲取授權token網頁授權 * * @param string $code * @return mixed|string */ public function getAccessToken($code = '') { $params = [ 'appid' => $this->app_id, 'secret' => $this->app_secret, 'code' => $code, 'grant_type' => 'authorization_code' ]; $ret = Http::sendRequest('https://api.weixin.qq.com/sns/oauth2/access_token', $params, 'GET'); if ($ret['ret']) { $ar = json_decode($ret['msg'], true); return $ar; } return []; } /** * 獲取用戶信息 * * @param string $code * @return mixed|string */ public function getUserinfo($access_token,$openid) { $params = [ 'access_token' => $access_token, 'openid' => $openid, 'lang' => 'zh_CN' ]; $ret = Http::sendRequest('https://api.weixin.qq.com/sns/userinfo', $params, 'GET'); if ($ret['ret']) { $ar = json_decode($ret['msg'], true); return $ar; } return []; }}


$wxchat = new /thago/util/Wechat($wxconfig['app_id'],$wxconfig['secret']); $token = $wxchat->getAccessToken($code); $openid = isset($token['openid']) ? $token['openid'] : ''; $access_token = isset($token['access_token']) ? $token['access_token'] : ''; if(!empty($openid)){ $wxUserinfo = $wxchat->getUserinfo($access_token,$openid); }


步驟如下:

1、根據前端傳入的code, 公眾號appid,secret獲取access_token

2、再根據access_token拉取用戶信息




總結:

在微信開發(fā)中遇到問題時,一定要結合微信官方文檔,先了解其原理再開發(fā)。

更多面試題:點擊這里



關鍵詞:清楚,流程,授權

74
73
25
news

版權所有? 億企邦 1997-2025 保留一切法律許可權利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點擊下載Chrome瀏覽器
關閉