LoginBaseController.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use DB;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  8. class LoginBaseController extends Controller
  9. {
  10. /*
  11. |--------------------------------------------------------------------------
  12. | Login Controller
  13. |--------------------------------------------------------------------------
  14. |
  15. | This controller handles authenticating users for the application and
  16. | redirecting them to your home screen. The controller uses a trait
  17. | to conveniently provide its functionality to your applications.
  18. |
  19. */
  20. use AuthenticatesUsers;
  21. /**
  22. * 重写 Validate the user login request.
  23. *
  24. * @param \Illuminate\Http\Request $request
  25. * @return void
  26. */
  27. public function validateLogin(Request $request)
  28. {
  29. $this->validate($request, [
  30. $this->username() => 'required|string|max:25',
  31. 'password' => 'required|string|min:6|max:16',
  32. 'captcha' => 'required|captcha',
  33. ]);
  34. }
  35. /**
  36. * 重写 The user has been authenticated.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @param mixed $user
  40. * @return mixed
  41. */
  42. protected function authenticated(Request $request, $user)
  43. {
  44. $time = Carbon::now();
  45. DB::table($this->usertable())
  46. ->where('name', $user->name)
  47. ->update([
  48. 'last_time' => $time,
  49. ]);
  50. }
  51. /**
  52. * 重写 Get the login username to be used by the controller.
  53. *
  54. * @return string
  55. */
  56. public function username()
  57. {
  58. return 'name';
  59. }
  60. /**
  61. * 重写 Log the user out of the application.
  62. *
  63. * @param \Illuminate\Http\Request $request
  64. * @return \Illuminate\Http\Response
  65. */
  66. public function logout(Request $request)
  67. {
  68. $this->guard()->logout();
  69. return redirect($this->redirectToLogout());
  70. }
  71. /**
  72. * 重写 Get the post register / login redirect path.
  73. *
  74. * @return string
  75. */
  76. public function redirectPath()
  77. {
  78. if (method_exists($this, 'redirectTo')) {
  79. return $this->redirectTo();
  80. }
  81. return property_exists($this, 'redirectTo') ? $this->redirectTo : '/';
  82. }
  83. }