Qrcode生成二維碼連結地址,網頁授權獲取微信使用者資訊
阿新 • • 發佈:2019-02-19
(1)一個簡單的二維碼地址生成使用easywechat掃描獲取微信使用者的資訊:
首先生成二維碼連結這裡引用的QrCode生成的
QrCode::size(300)->generate($url]);
url為掃描二維碼所跳轉的地址
(2)當用戶掃描二維碼跳轉至後臺處理方法,我這裡這樣引用
public function getWxUserOpenid(Request $request)
{
//此處判斷是否是微信瀏覽器
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strpos($user_agent , 'MicroMessenger') === false) {
$targetUrl=如果不是直接越過授權,跳至需要授權的頁面地址;
return redirect()->away($targetUrl);
}else{
$options = [
'debug' => true,
'app_id' => "你的appid",
'secret' => "你的secret",
'token' => '你的token',
'oauth' => [
'scopes' =>['snsapi_userinfo'],
'callback' => "授權回撥URL地址",
],
'log' => [
'level' => 'debug',
'file' => storage_path('logs/campaign'.date('Ymd').'.log'),
],
];
$app = new Application($options);
$oauth = $app->oauth;
if (empty(Session::get('wechat_user'))) {
$target_url= 需要授權才能訪問的頁面;
Session::put('target_url',$target_url);
return $oauth->redirect();
}
$user = Session::get('wechat_user');
}
}
(3)授權回撥頁處理:
public function getCallback()
{
$config = [
配置詳情
];
$app = new Application($config);
$oauth = $app->oauth;
$user = $oauth->user();
Session::put('user',$user);
Session::put('wechat_user',$user->toArray());
$target_Url = empty(Session::get('target_url')) ? '/' : Session::get('target_url');
return redirect($target_Url);
}
如果對你有幫助,就頂我!