config/auth.php
添加guard 'guards' => [ 'web' => [ 'driver' => 'session', 'provider' => 'users', ], 'admin' => [ 'driver' => 'session', 'provider' => 'admins', ], 'three' => [ 'driver' => 'session', 'provider' => 'threes', ], 'api' => [ 'driver' => 'token', 'provider' => 'users', ], 'admin_wxuser' => [ 'driver' => 'session', 'provider' => 'admin_wxusers', ], ], /* |-------------------------------------------------------------------------- | User Providers |-------------------------------------------------------------------------- | | All authentication drivers have a user provider. This defines how the | users are actually retrieved out of your database or other storage | mechanisms used by this application to persist your user's data. | | If you have multiple user tables or models you may configure multiple | sources which represent each model / table. These sources may then | be assigned to any extra authentication guards you have defined. | | Supported: "database", "eloquent" | */ 'providers' => [ 'users' => [ 'driver' => 'eloquent', 'model' => App\Models\User::class, ], 'admins' => [ 'driver' => 'eloquent', 'model' => App\Models\Admin::class, ], 'threes' => [ //第三方用户 'driver' => 'eloquent', 'model' => App\Models\ThreeUser::class, ], 'admin_wxusers' => [ // 'driver' => 'eloquent', 'model' => App\Models\AdminWxUser::class, ], // 'users' => [ // 'driver' => 'database', // 'table' => 'users', // ], ],
Models/
public function wx_outh() { if (!auth('admin')->check()) { return redirect('/admin/login'); } return Socialite::with('weixinweb')->redirectUrl("http://wang135.tunnel.2bdata.com/admin/wx_suc")->redirect(); } /** * 微信登陆返回 * 如果授权信息和数据库中匹配择登录 * 如果不匹配登录失败 * 如果没有则绑定 */ public function wx_suc() { if (!auth('admin')->check()) { return redirect('/admin/login'); } $admin_user = Auth::guard('admin')->user(); $wx_user = $admin_user->admin_wxuser; $weixin_user = Socialite::with('weixinweb')->stateless()->user(); //扫码用户的信息 if ($wx_user) { if ($wx_user->openid == $weixin_user->id) { auth('admin_wxuser')->login($wx_user); return redirect('/admin'); } else { header("refresh:3;url=/admin/wx_outh"); print("<h1 style='line-height: 500px;text-align: center'>登陆微信号不匹配,3秒后跳转到扫码页面</h1>"); } } else { $data['admin_id'] = Auth::guard('admin')->user()->id; $data['openid'] = $weixin_user->id; $data['nickname'] = $weixin_user->nickname; $data['headimgurl'] = $weixin_user->avatar; $result = AdminWxUser::create($data); if ($result) { auth('admin_wxuser')->login($result); header("refresh:3;url=/admin"); print("<h1 style='line-height: 500px;text-align: center'>登陆微信号绑定成功,3秒后跳转到管理页面</h1>"); } } } public function logout() { Auth::guard('admin')->logout(); Auth::guard('admin_wxuser')->logout(); session(['user' => null]); return redirect('admin/login'); }
联系客服