187 lines
5.0 KiB
PHP
187 lines
5.0 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Classc\GoogleAuthenticator;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
||
use Auth;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Validation\ValidationException;
|
||
use Validator;
|
||
|
||
class LoginController extends Controller
|
||
{
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| Login Controller
|
||
|--------------------------------------------------------------------------
|
||
|
|
||
| This controller handles authenticating users for the application and
|
||
| redirecting them to your home screen. The controller uses a trait
|
||
| to conveniently provide its functionality to your applications.
|
||
|
|
||
*/
|
||
|
||
use AuthenticatesUsers;
|
||
|
||
/**
|
||
* Where to redirect users after login.
|
||
*
|
||
* @var string
|
||
*/
|
||
protected $redirectTo = '/admin/home/';
|
||
|
||
public function username()
|
||
{
|
||
return 'account';
|
||
}
|
||
|
||
protected function guard()
|
||
{
|
||
return Auth::guard('admin');
|
||
}
|
||
|
||
protected function sendFailedLoginResponse(Request $request)
|
||
{
|
||
return (['error' => 1, 'msg' => trans('auth.failed')]);
|
||
}
|
||
|
||
/**
|
||
* Create a new controller instance.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function __construct()
|
||
{
|
||
|
||
//$this->middleware('admin')->except('logout');
|
||
}
|
||
|
||
protected function formError($error)
|
||
{
|
||
$error = $error->all();
|
||
if (count($error) <= 0) return [];
|
||
$error_str = '';
|
||
foreach ($error as $k => $v) {
|
||
|
||
$error_str .= $v . "*<br/>";
|
||
|
||
}
|
||
return response()->json(['error' => 1, 'msg' => $error_str, 'type' => 'validator']);
|
||
|
||
|
||
}
|
||
|
||
//跳转地址登陆
|
||
public function redirectTo()
|
||
{
|
||
return redirect()->to(admin_url('Home'));
|
||
}
|
||
|
||
public function authenticated()
|
||
{
|
||
return response()->json(['error' => 0, 'msg' => '登录成功']);
|
||
}
|
||
|
||
public function showLoginForm()
|
||
{
|
||
|
||
if (Auth::guard('admin')->check()) {
|
||
return redirect()->to(admin_url('Home'));
|
||
}
|
||
return view('admin.auth.login', ['title' => '管理系统']);
|
||
}
|
||
|
||
protected function validatorForm($request)
|
||
{
|
||
$is_mobile = 0;
|
||
$message_data = [
|
||
$this->username() . '.required' => '请输入账号',
|
||
'password.required' => '请输入密码',
|
||
];
|
||
$check_data =
|
||
[
|
||
$this->username() => 'required|string',
|
||
'password' => 'required|string',
|
||
];
|
||
|
||
|
||
$validator = Validator::make($request->all(), $check_data, $message_data);
|
||
if ($validator->fails()) {
|
||
|
||
if ($request->ajax() || $request->wantsJson()) {
|
||
|
||
return $validator->errors();
|
||
|
||
}
|
||
}
|
||
return [];
|
||
}
|
||
|
||
protected function sendLockoutResponse(Request $request)
|
||
{
|
||
$seconds = $this->limiter()->availableIn(
|
||
$this->throttleKey($request)
|
||
);
|
||
return response()->json(['error' => 1, 'msg' => '登陆失败次数过多,请稍后重试' . $seconds]);
|
||
}
|
||
|
||
public function login(Request $request)
|
||
{
|
||
$whtelist = config('whitelist.login');
|
||
$ip = $request->getClientIp();
|
||
if (!in_array($ip, $whtelist)) {
|
||
// return response()->json(['error' => 1, 'msg' => 'err']);
|
||
}
|
||
|
||
$error = $this->validatorForm($request);
|
||
if (count($error) > 0) {
|
||
return $this->formError($error);
|
||
};
|
||
// If the class is using the ThrottlesLogins trait, we can automatically throttle
|
||
// the login attempts for this application. We'll key this by the username and
|
||
// the IP address of the client making these requests into this application.
|
||
//确定用户是否有太多失败的登录尝试。
|
||
if ($this->hasTooManyLoginAttempts($request)) {
|
||
$this->fireLockoutEvent($request);
|
||
|
||
//太多次返回的信息
|
||
return $this->sendLockoutResponse($request);
|
||
}
|
||
|
||
|
||
if ($this->attemptLogin($request)) {
|
||
$this->guard()->user();
|
||
$code = $request->post('code');
|
||
$user = admin();
|
||
if ($user->google_secret != '') {
|
||
if (empty($code)) {
|
||
return response()->json(['error' => 1, 'msg' => '验证码必填']);
|
||
}
|
||
$ga = new GoogleAuthenticator();
|
||
if (!$ga->verifyCode($user->google_secret, $code)) {
|
||
return response()->json(['error' => 1, 'msg' => '验证码错误']);
|
||
}
|
||
}
|
||
//通过之后响应
|
||
return $this->sendLoginResponse($request);
|
||
}
|
||
|
||
//增加登陆尝试次数,默认尝试增加1次
|
||
$this->incrementLoginAttempts($request);
|
||
|
||
|
||
return $this->sendFailedLoginResponse($request);
|
||
}
|
||
|
||
public function logout(Request $request)
|
||
{
|
||
$this->guard()->logout();
|
||
|
||
$request->session()->invalidate();
|
||
|
||
return $this->loggedOut($request) ?: redirect()->route('admin.login');
|
||
}
|
||
}
|