AuthController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Models\DistAdminDistributor;
  4. use Dcat\Admin\Admin;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
  7. use Dcat\Admin\Http\Repositories\Administrator;
  8. use Dcat\Admin\Layout\Content;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Session;
  11. use Illuminate\Support\Facades\Validator;
  12. class AuthController extends BaseAuthController
  13. {
  14. protected $view = 'admin.pages.login';
  15. /**
  16. * Login interface.重写登录接口
  17. * @param Request $request
  18. * @return
  19. */
  20. public function postLogin(Request $request)
  21. {
  22. $credentials = $request->only([$this->username(), 'password', 'captcha']);
  23. //去除前后空格
  24. foreach (['username', 'password', 'captcha'] as $key) {
  25. if (isset($credentials[$key])) {
  26. $credentials[$key] = trim($credentials[$key]);
  27. }
  28. }
  29. $remember = (bool)$request->input('remember', false);
  30. /** @var \Illuminate\Validation\Validator $validator */
  31. $validator = Validator::make($credentials, [
  32. $this->username() => 'required',
  33. 'password' => 'required',
  34. 'captcha' => 'required',
  35. ]);
  36. if (trim($request->input('captcha')) != Session::get('captcha'))
  37. {
  38. $session_captcha = Session::get('captcha');
  39. //Session::forget('captcha');
  40. return response()->json([
  41. 'success' => false,
  42. 'message' => 'The captcha['.$session_captcha.'] is incorrect. Please refresh the page and try again.',
  43. 'refresh_captcha' => true, // 通知前端刷新验证码
  44. ], 422);; // 422 表示 Unprocessable Entity
  45. }
  46. else
  47. {
  48. //Session::forget('captcha');
  49. }
  50. unset($credentials['captcha']);
  51. if ($validator->fails()) {
  52. return $this->validationErrorsResponse($validator);
  53. }
  54. if ($this->guard()->attempt($credentials, $remember)) {
  55. // 登录成功后返回登录响应
  56. return $this->sendLoginResponse($request);
  57. }
  58. return $this->validationErrorsResponse([
  59. $this->username() => $this->getFailedLoginMessage(),
  60. ]);
  61. }
  62. /**
  63. * 重写登录控制器
  64. * @param Content $content
  65. * @return Content
  66. */
  67. function getLogin(Content $content)
  68. {
  69. $lang = request()->query('lang');
  70. if(!empty($lang))
  71. {
  72. switchLanguage($lang);
  73. return response()->json(['success' => true, 'lang' => $lang]);
  74. }
  75. if ($this->guard()->check()) {
  76. return redirect($this->getRedirectPath());
  77. }
  78. return $content->full()->body(view($this->view));
  79. }
  80. /**
  81. * Model-form for user setting.
  82. *
  83. * @return Form
  84. */
  85. protected function settingForm()
  86. {
  87. return new Form(new Administrator(), function (Form $form) {
  88. $form->action(admin_url('auth/setting'));
  89. $form->disableCreatingCheck();
  90. $form->disableEditingCheck();
  91. $form->disableViewCheck();
  92. $form->tools(function (Form\Tools $tools) {
  93. $tools->disableView();
  94. $tools->disableDelete();
  95. });
  96. $form->display('username', trans('admin.username'));
  97. $form->text('name', trans('admin.name'))->required();
  98. $form->password('old_password', trans('admin.old_password'));
  99. $form->password('password', trans('admin.password'))
  100. ->minLength(5)
  101. ->maxLength(20)
  102. ->customFormat(function ($v) {
  103. if ($v == $this->password) {
  104. return;
  105. }
  106. return $v;
  107. });
  108. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  109. $form->ignore(['password_confirmation', 'old_password']);
  110. // 添加语言选择的下拉框
  111. // $form->select('language', trans('admin.language'))
  112. // ->options(config('dictionary.languages'))
  113. // ->default('en')
  114. // ->required();; // 设置默认语言
  115. $form->saving(function (Form $form) {
  116. if ($form->password && $form->model()->password != $form->password) {
  117. $form->password = bcrypt($form->password);
  118. }
  119. if (! $form->password) {
  120. $form->deleteInput('password');
  121. }
  122. });
  123. $form->saved(function (Form $form) {
  124. return $form
  125. ->response()
  126. ->success(trans('admin.update_succeeded'))
  127. //->redirect('/');
  128. ->script('setTimeout(() => {location.reload();}, 1000);');//保存成功后刷新页面
  129. });
  130. });
  131. }
  132. }