(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);
}
easywechat网页授权文档地址:https://easywechat.org/zh-cn/docs/oauth.html
如果对你有帮助,就顶我!