LoginController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Modules\Admin\Http\Controllers;
  3. use Illuminate\Foundation\Auth\AuthenticatesUsers;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Routing\Controller;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Support\Facades\Log;
  8. use Modules\Admin\Services\FilterService;
  9. use Modules\Admin\Services\SettingService;
  10. use DB;
  11. class LoginController extends Controller
  12. {
  13. use AuthenticatesUsers;
  14. /**
  15. * 登陆后的跳转页面
  16. *
  17. * @var string
  18. */
  19. protected $redirectTo = '/admin';
  20. /**
  21. * Create a new controller instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. $this->middleware('guest')->except('logout');
  28. }
  29. //登录页面
  30. public function loginView(Request $request)
  31. {
  32. $filter = FilterService::check_ip();
  33. if (Auth::guard('web')->check()) {
  34. return redirect('/admin');
  35. }
  36. return view('admin::login');
  37. }
  38. //登录动作
  39. public function doLogin(Request $request)
  40. {
  41. $request['password'] = base64_decode($request->input('password'));
  42. $this->validateLogin($request);
  43. // If the class is using the ThrottlesLogins trait, we can automatically throttle
  44. // the login attempts for this application. We'll key this by the username and
  45. // the IP address of the client making these requests into this application.
  46. if (method_exists($this, 'hasTooManyLoginAttempts') &&
  47. $this->hasTooManyLoginAttempts($request)) {
  48. $this->fireLockoutEvent($request);
  49. return $this->sendLockoutResponse($request);
  50. }
  51. $valid = validPass($request['password']);
  52. if (is_string($valid)) {
  53. $this->guard()->logout();
  54. $request->session()->invalidate();
  55. return [
  56. 'code' => 1,
  57. 'message' => '密码限制:' . $valid . ',请找管理员修改',
  58. ];
  59. }
  60. if ($this->attemptLogin($request)) {
  61. // $roles = $this->guard()->user()->getRoleNames()->toArray();
  62. // if (count($roles) == 0 || (count($roles) == 1 && $roles[0] == 'PuTongZhiYuan')) {
  63. // $this->guard()->logout();
  64. // $request->session()->invalidate();
  65. // return [
  66. // 'code' => 1,
  67. // 'message' => '没有登录权限',
  68. // ];
  69. // }
  70. $token = $request->input('staff_num') . "," . $request->input('password') . "," . $request->getClientIp();
  71. $token = base64_encode($token);
  72. setcookie("token", $token, time() + 7200, "/", ".nxjiewei.com");
  73. return $this->sendLoginResponse($request);
  74. }
  75. // If the login attempt was unsuccessful we will increment the number of attempts
  76. // to login and redirect the user back to the login form. Of course, when this
  77. // user surpasses their maximum number of attempts they will get locked out.
  78. $this->incrementLoginAttempts($request);
  79. return $this->sendFailedLoginResponse($request);
  80. }
  81. protected function sendLoginResponse(Request $request)
  82. {
  83. $request->session()->regenerate();
  84. $this->clearLoginAttempts($request);
  85. return [
  86. 'code' => 0,
  87. 'message' => '登录成功',
  88. ];
  89. }
  90. /**
  91. * 用户名验证字段
  92. * @return string
  93. */
  94. public function username()
  95. {
  96. return 'staff_num';
  97. }
  98. }