AuthController.php 5.4 KB

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