1.先查看官方文档
- 在登录页面logo.blade.php中引入如下:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<span id="login_container" style="margin: 0 auto;display: block;text-align:center; width: 100%;"></span>
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
<script>
var obj = new WxLogin({
id: "login_container",
appid: "{{ $wxcode['appid'] }}",
scope: "{{ $wxcode['agentid'] }}",
redirect_uri: "{{ $wxcode['url'] }}",
state:"{{ $wxcode['state'] }}",
style: "black",
href: ""
});
</script>
</body>
</html>
- 控制器LoginController内容:
public function indexs(Request $request) { $appid = ''; //微信签名 $agentid = 'snsapi_login'; //$url = urlencode(url('login/callback')); $url = urlencode("http://www.051996111.com/login/callback"); //生成唯一随机串防CSRF攻击 $state = bcrypt(uniqid(mt_rand(), TRUE)); Cache::put('state',$state,60); //这里使用laravel框架自带缓存,因为涉及跨域问题session失效 $WwLogin = ['appid'=>$appid,'agentid'=>$agentid,'url'=>$url,'state'=>$state]; $assign = [ 'wxcode'=>$WwLogin ]; return view('home.login.login',$assign); }
- 回调控制器内容如下:
public function callback(Request $request) { $reurl = Cache::get('reurl'); $urls = empty($reurl) ?'/personal/index':$reurl; $code = $request->input('code'); $state = $request->input('state'); $states =Cache::get('state'); if (!isset($states)) { return back()->withErrors('验证失败!'); } //验证CSRF攻击 if($state != $states){ return back()->withErrors('黑客攻击!'); } $appid = ''; $secret = ''; //通过code获得 access_token + openid $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid. "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code"; $jsonResult = file_get_contents($url); $resultArray = json_decode($jsonResult, true); $access_token = $resultArray["access_token"]; $openid = $resultArray["openid"]; //通过access_token + openid 获得用户所有信息,结果全部存储在$infoArray里,后面再写自己的代码逻辑 $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid; $infoResult = file_get_contents($infoUrl); $infoArray = json_decode($infoResult, true); $res = User::where(['openid'=>$infoArray['openid']])->first(); if(isset($res->openid)){ $request->session()->put('openid',$res->openid); $request->session()->put('username',$res->username); $request->session()->put('user_id',$res->id); if(!UserRoles::where(['user_id'=>$res->id,'role_id'=>1])->count()>0){ $roles=['user_id'=>$res->id,'role_id'=>1]; UserRoles::create($roles); } //查询用户的角色 这里涉及用户选角色登录问题 if( UserRoles::where(['user_id'=>$res->id])->count()==1){ return redirect('personal/index'); } else{ $role = UserRoles::where(['user_id'=>$res->id])->get(); return view('home.login.role',compact('role','urls')); } } $datas = [ 'username'=>$infoArray['nickname'], 'password'=>bcrypt('11111'), 'openid'=>$infoArray['openid'], 'unionid'=>$infoArray['unionid'], 'role_id'=>1, 'cover'=>$infoArray['headimgurl'], 'gender'=>$infoArray['sex'], 'created_at' => now(), 'updated_at' =>now() ]; $result = User::insertGetId($datas); if($result){ $request->session()->put('openid',$infoArray['openid']); $request->session()->put('username',$infoArray['nickname']); $request->session()->put('user_id',$result); //给用户添加角色默认学生家长 $roles=['user_id'=>$result,'role_id'=>1]; UserRoles::create($roles); //查询用户的角色 $role = UserRoles::where(['user_id'=>$result])->get(); return view('home.login.role',compact('role','urls')); } return back()->withErrors('数据错误'); }