AuthController.php 4.8 KB

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