AuthController.php 6.9 KB

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