AuthController.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Models\DistAdminDistributor;
  4. use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
  5. use Dcat\Admin\Admin;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Http\Repositories\Administrator;
  8. use Dcat\Admin\Layout\Content;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Support\Facades\Session;
  12. class AuthController extends BaseAuthController
  13. {
  14. protected $view = 'distributor.pages.login';
  15. /**
  16. * 重写登录控制器
  17. * @param Content $content
  18. * @return Content
  19. */
  20. function getLogin(Content $content)
  21. {
  22. $lang = request()->query('lang');
  23. if(!empty($lang))
  24. {
  25. switchLanguage($lang);
  26. return redirect()->to(request()->url()); // 刷新页面,去掉查询参数
  27. }
  28. if ($this->guard()->check()) {
  29. return redirect($this->getRedirectPath());
  30. }
  31. return $content->full()->body(view($this->view));
  32. }
  33. /**
  34. * Login interface.重写登录接口
  35. * @param Request $request
  36. * @return
  37. */
  38. public function postLogin(Request $request)
  39. {
  40. $credentials = $request->only([$this->username(), 'password', 'captcha']);
  41. $remember = (bool)$request->input('remember', false);
  42. /** @var \Illuminate\Validation\Validator $validator */
  43. $validator = Validator::make($credentials, [
  44. $this->username() => 'required',
  45. 'password' => 'required',
  46. 'captcha' => 'required',
  47. ]);
  48. if ($request->input('captcha') != Session::get('captcha'))
  49. {
  50. session()->forget('captcha');
  51. return response()->json([
  52. 'success' => false,
  53. 'message' => 'The captcha is incorrect. Please refresh the page and try again.',
  54. 'refresh_captcha' => true, // 通知前端刷新验证码
  55. ], 422);; // 422 表示 Unprocessable Entity
  56. }
  57. else
  58. {
  59. session()->forget('captcha');
  60. }
  61. unset($credentials['captcha']);
  62. if ($validator->fails()) {
  63. return $this->validationErrorsResponse($validator);
  64. }
  65. if ($this->guard()->attempt($credentials, $remember)) {
  66. //登录成功后从dist_admin_distributor表中取出当前登录用户的公司信息
  67. $distributor = DistAdminDistributor::where('id', Admin::user()->dist_id)->first();
  68. if (!$distributor) {
  69. $this->guard()->logout();
  70. return $this->validationErrorsResponse([
  71. $this->username() => $this->getFailedLoginMessage(),
  72. ]);
  73. }
  74. //将当前登录用户的公司信息存入session
  75. Session::put('distributor', $distributor->toArray());
  76. // 登录成功后返回登录响应
  77. return $this->sendLoginResponse($request);
  78. }
  79. return $this->validationErrorsResponse([
  80. $this->username() => $this->getFailedLoginMessage(),
  81. ]);
  82. }
  83. public function getSetting(Content $content)
  84. {
  85. $form = $this->settingForm();
  86. $form->tools(
  87. function (Form\Tools $tools) {
  88. $tools->disableList();
  89. }
  90. );
  91. return $content
  92. ->view('distributor.layouts.content')
  93. ->title(trans('admin.user_setting'))
  94. ->body($form->edit(Admin::user()->getKey()));
  95. }
  96. /**
  97. * Model-form for user setting.
  98. *
  99. * @return Form
  100. */
  101. protected function settingForm()
  102. {
  103. return new Form(new Administrator(), function (Form $form) {
  104. $form->action(admin_url('auth/setting'));
  105. $form->disableCreatingCheck();
  106. $form->disableEditingCheck();
  107. $form->disableViewCheck();
  108. $form->tools(function (Form\Tools $tools) {
  109. $tools->disableView();
  110. $tools->disableDelete();
  111. });
  112. $form->display('username', trans('admin.username'));
  113. $form->text('name', trans('admin.name'))->required();
  114. //$form->image('avatar', trans('admin.avatar'))->autoUpload();
  115. $form->password('old_password', trans('admin.old_password'));
  116. $form->password('password', trans('admin.password'))
  117. ->minLength(5)
  118. ->maxLength(20)
  119. ->customFormat(function ($v) {
  120. if ($v == $this->password) {
  121. return;
  122. }
  123. return $v;
  124. });
  125. $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
  126. $form->ignore(['password_confirmation', 'old_password']);
  127. // 添加语言选择的下拉框
  128. $form->select('language', trans('admin.language'))
  129. ->options(config('dictionary.languages'))
  130. ->default('en')
  131. ->required();; // 设置默认语言
  132. $form->saving(function (Form $form) {
  133. if ($form->password && $form->model()->password != $form->password) {
  134. $form->password = bcrypt($form->password);
  135. }
  136. if (! $form->password) {
  137. $form->deleteInput('password');
  138. }
  139. });
  140. $form->saved(function (Form $form) {
  141. return $form
  142. ->response()
  143. ->success(trans('admin.update_succeeded'))
  144. //->redirect('/');
  145. ->script('setTimeout(() => {location.reload();}, 1000);');//保存成功后刷新页面
  146. });
  147. // // 在从数据库中取出记录时,如果 language 为空,则默认给它一个值
  148. // $form->model()->language = $form->model()->language ?: 'en';
  149. });
  150. }
  151. protected function sendLoginResponse(Request $request)
  152. {
  153. $request->session()->regenerate();
  154. $path = $this->getRedirectPath();
  155. return $this->response()
  156. ->success(trans('admin.login_successful'))
  157. ->locationToIntended($path)
  158. ->locationIf(Admin::app()->getEnabledApps(), $path)
  159. ->send();
  160. }
  161. }