220 lines
5.4 KiB
PHP
220 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Merchant;
|
||
|
||
use App\Http\Controllers\Controller;
|
||
use App\Models\SendCode;
|
||
use App\Models\User;
|
||
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 = '/merchant/admin/home/';
|
||
|
||
public $guard = 'merchant';
|
||
|
||
public function username()
|
||
{
|
||
return 'email';
|
||
}
|
||
|
||
protected function guard()
|
||
{
|
||
return Auth::guard($this->guard);
|
||
}
|
||
|
||
/**
|
||
* 全局返回表单错误消息
|
||
* @param $error
|
||
* @return array
|
||
*/
|
||
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_merchant_url('Home'));
|
||
}
|
||
|
||
public function logout(Request $request)
|
||
{
|
||
$this->guard($this->guard)->logout();
|
||
|
||
$request->session()->invalidate();
|
||
|
||
return redirect()->action('Merchant\LoginController@showLoginForm');
|
||
|
||
}
|
||
|
||
|
||
protected function sendFailedLoginResponse(Request $request)
|
||
{
|
||
return (['error' => 1, 'msg' => trans('auth.failed')]);
|
||
}
|
||
|
||
/**
|
||
* Create a new controller instance.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function __construct()
|
||
{
|
||
$this->middleware('guest')->except('logout');
|
||
}
|
||
|
||
public function showLoginForm()
|
||
{
|
||
if (Auth::guard($this->guard)->check()) {
|
||
return redirect()->to(admin_merchant_url('Home'));
|
||
}
|
||
return view('merchant.auth.login', ['title' => config('merchant_admin.name')]);
|
||
}
|
||
|
||
protected function credentials(Request $request)
|
||
{
|
||
return $request->only($this->username(), 'password');
|
||
}
|
||
|
||
protected function attemptLogin(Request $request)
|
||
{
|
||
$data = $this->credentials($request);
|
||
|
||
$r = $this->guard()->attempt(
|
||
$data, $request->filled('remember')
|
||
);
|
||
if ($r) {
|
||
//更新操作
|
||
$admin = \Illuminate\Support\Facades\Auth::guard($this->guard)->user();
|
||
$admin->last_time = date('Y-m-d H:i:s');
|
||
$admin->last_number = $admin->last_number + 1;
|
||
$admin->ip = $request->getClientIp();
|
||
$admin->save();
|
||
|
||
}
|
||
return $r;
|
||
}
|
||
|
||
protected function sendLockoutResponse(Request $request)
|
||
{
|
||
$seconds = $this->limiter()->availableIn(
|
||
$this->throttleKey($request)
|
||
);
|
||
return response()->json(['error' => 1, 'msg' => '登陆失败次数过多,请稍后重试' . $seconds]);
|
||
}
|
||
|
||
protected function validatorForm($request)
|
||
{
|
||
$is_mobile = 0;
|
||
$message_data = [
|
||
'email.required' => '请输入邮箱',
|
||
'password.required' => '请输入密码',
|
||
];
|
||
$check_data =
|
||
[
|
||
$this->username() => [
|
||
'required', 'email'
|
||
|
||
],
|
||
'password' => 'required',
|
||
];
|
||
if ($is_mobile) {
|
||
$check_data =
|
||
[
|
||
$this->username() => [
|
||
'required',
|
||
'regex:/^1\d{10}$/'
|
||
]
|
||
|
||
];
|
||
|
||
|
||
}
|
||
|
||
|
||
$validator = Validator::make($request->all(), $check_data, $message_data);
|
||
if ($validator->fails()) {
|
||
|
||
if ($request->ajax() || $request->wantsJson()) {
|
||
|
||
return $validator->errors();
|
||
|
||
}
|
||
}
|
||
return [];
|
||
}
|
||
|
||
public function authenticated()
|
||
{
|
||
return response()->json(['error' => 0, 'msg' => '登陆成功']);
|
||
}
|
||
|
||
|
||
public function login(Request $request)
|
||
{
|
||
|
||
$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)) {
|
||
//通过之后响应
|
||
return $this->sendLoginResponse($request);
|
||
}
|
||
|
||
//增加登陆尝试次数,默认尝试增加1次
|
||
$this->incrementLoginAttempts($request);
|
||
|
||
|
||
return $this->sendFailedLoginResponse($request);
|
||
}
|
||
|
||
|
||
}
|