AuthController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $timeZoneName = $request->input('timeZoneName');
  57. // 获取所有合法时区名称
  58. $validTimeZones = DateTimeZone::listIdentifiers();
  59. if (!in_array($timeZoneName, $validTimeZones)) {
  60. // 如果时区不合法,则使用默认时区
  61. $timeZoneName = 'UTC';
  62. }
  63. // 写入时区到session
  64. Session::put('timeZoneName', $timeZoneName);
  65. #写入时区 end
  66. // 登录成功后返回登录响应
  67. return $this->sendLoginResponse($request);
  68. }
  69. return $this->validationErrorsResponse([
  70. $this->username() => $this->getFailedLoginMessage(),
  71. ]);
  72. }
  73. /**
  74. * 重写登录控制器
  75. * @param Content $content
  76. * @return Content
  77. */
  78. function getLogin(Content $content)
  79. {
  80. $lang = request()->query('lang');
  81. if(!empty($lang))
  82. {
  83. switchLanguage($lang);
  84. return response()->json(['success' => true, 'lang' => $lang]);
  85. }
  86. if ($this->guard()->check()) {
  87. return redirect($this->getRedirectPath());
  88. }
  89. return $content->full()->body(view($this->view));
  90. }
  91. /**
  92. * Model-form for user setting.
  93. *
  94. * @return Form
  95. */
  96. protected function settingForm()
  97. {
  98. return new Form(new Administrator(), function (Form $form) {
  99. $form->action(admin_url('auth/setting'));
  100. $form->disableCreatingCheck();
  101. $form->disableEditingCheck();
  102. $form->disableViewCheck();
  103. $form->tools(function (Form\Tools $tools) {
  104. $tools->disableView();
  105. $tools->disableDelete();
  106. });
  107. $form->display('username', trans('admin.username'));
  108. $form->text('name', trans('admin.name'))->required();
  109. $form->password('old_password', trans('admin.old_password'));
  110. $form->password('password', trans('admin.password'))
  111. ->minLength(5)
  112. ->maxLength(20)
  113. ->customFormat(function ($v) {
  114. if ($v == $this->password) {
  115. return;
  116. }
  117. return $v;
  118. });
  119. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  120. $form->ignore(['password_confirmation', 'old_password']);
  121. // 添加语言选择的下拉框
  122. // $form->select('language', trans('admin.language'))
  123. // ->options(config('dictionary.languages'))
  124. // ->default('en')
  125. // ->required();; // 设置默认语言
  126. $form->saving(function (Form $form) {
  127. if ($form->password && $form->model()->password != $form->password) {
  128. $form->password = bcrypt($form->password);
  129. }
  130. if (! $form->password) {
  131. $form->deleteInput('password');
  132. }
  133. });
  134. $form->saved(function (Form $form) {
  135. return $form
  136. ->response()
  137. ->success(trans('admin.update_succeeded'))
  138. //->redirect('/');
  139. ->script('setTimeout(() => {location.reload();}, 1000);');//保存成功后刷新页面
  140. });
  141. });
  142. }
  143. }