123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace App\Http\Controllers\Traits;
- use Illuminate\Foundation\Auth\RedirectsUsers;
- use Illuminate\Foundation\Auth\ThrottlesLogins;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- trait AuthUserTrait
- {
- use RedirectsUsers, ThrottlesLogins;
- /**
- * Show the application's login form.
- *
- * @return \Illuminate\Http\Response
- */
- public function showLoginForm()
- {
- config()->set(['admin'=>['title'=>'后台登陆']]);
- return view('auth.login');
- }
- /**
- * Handle a login request to the application.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response
- */
- public function login(Request $request)
- {
- $this->validateLogin($request);
- // 验证码验证
- $captcha = captcha_check($request->input('captcha'));
- // 验证失败
- if( !$captcha ){
- return redirect()->back()
- ->withInput($request->only($this->username(), 'remember'))
- ->withErrors(['captcha'=>'验证错误']);
- }
- // 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)) {
- // $request->setTrustedProxies(array('10.32.0.1/16'));
- // $ip = $request->getClientIp();
- // // 记录登录信息
- // file_put_contents(
- // storage_path('logs/auth-login.txt'),
- // "登录时间:".(date('Y-m-d H:i:s'))." 登录IP:{$ip}"."\r\n", FILE_APPEND
- // );
- return $this->sendLoginResponse($request);
- }
- // If the login attempt was unsuccessful we will increment the number of attempts
- // to login and redirect the user back to the login form. Of course, when this
- // user surpasses their maximum number of attempts they will get locked out.
- $this->incrementLoginAttempts($request);
- return $this->sendFailedLoginResponse($request);
- }
- /**
- * Validate the user login request.
- *
- * @param \Illuminate\Http\Request $request
- * @return void
- */
- protected function validateLogin(Request $request)
- {
- $this->validate($request, [
- $this->username() => 'required|string',
- 'password' => 'required|string',
- 'captcha' => 'required|string',
- ],[
- 'required' => '必填'
- // $this->username().'.required' => '必填',
- // 'password.required' => '必填',
- // 'captcha.required' => '必填'
- ]);
- }
- /**
- * Attempt to log the user into the application.
- *
- * @param \Illuminate\Http\Request $request
- * @return bool
- */
- protected function attemptLogin(Request $request)
- {
- return $this->guard()->attempt(
- $this->credentials($request), $request->has('remember')
- );
- }
- /**
- * Get the needed authorization credentials from the request.
- *
- * @param \Illuminate\Http\Request $request
- * @return array
- */
- protected function credentials(Request $request)
- {
- return $request->only($this->username(), 'password');
- }
- /**
- * Send the response after the user was authenticated.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- protected function sendLoginResponse(Request $request)
- {
- $request->session()->regenerate();
- $this->clearLoginAttempts($request);
- return $this->authenticated($request, $this->guard()->user())
- ?: redirect()->intended($this->redirectPath());
- }
- /**
- * The user has been authenticated.
- *
- * @param \Illuminate\Http\Request $request
- * @param mixed $user
- * @return mixed
- */
- protected function authenticated(Request $request, $user)
- {
- //
- }
- /**
- * Get the failed login response instance.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\RedirectResponse
- */
- protected function sendFailedLoginResponse(Request $request)
- {
- // $errors = [$this->username() => trans('auth.failed')];
- $errors = [$this->username() => '账号或密码错误'];;
- if ($request->expectsJson()) {
- return response()->json($errors, 422);
- }
- return redirect()->back()
- ->withInput($request->only($this->username(), 'remember'))
- ->withErrors($errors);
- }
- /**
- * Get the login username to be used by the controller.
- *
- * @return string
- */
- public function username()
- {
- return 'email';
- }
- /**
- * Log the user out of the application.
- *
- * @param \Illuminate\Http\Request $request
- * @return \Illuminate\Http\Response
- */
- public function logout(Request $request)
- {
- $this->guard()->logout();
- $request->session()->flush();
- $request->session()->regenerate();
- return redirect('/admin/login');
- }
- /**
- * Get the guard to be used during authentication.
- *
- * @return \Illuminate\Contracts\Auth\StatefulGuard
- */
- protected function guard()
- {
- return Auth::guard();
- }
- }
|