123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- namespace App\Distributor\Controllers;
- use App\Models\DistAdminDistributor;
- use Dcat\Admin\Http\Controllers\AuthController as BaseAuthController;
- use Dcat\Admin\Admin;
- use Dcat\Admin\Form;
- use Dcat\Admin\Http\Repositories\Administrator;
- use Dcat\Admin\Layout\Content;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\Facades\Session;
- class AuthController extends BaseAuthController
- {
- protected $view = 'distributor.pages.login';
- /**
- * 重写登录控制器
- * @param Content $content
- * @return Content
- */
- function getLogin(Content $content)
- {
- $lang = request()->query('lang');
- if(!empty($lang))
- {
- switchLanguage($lang);
- return redirect()->to(request()->url()); // 刷新页面,去掉查询参数
- }
- if ($this->guard()->check()) {
- return redirect($this->getRedirectPath());
- }
- return $content->full()->body(view($this->view));
- }
- /**
- * Login interface.重写登录接口
- * @param Request $request
- * @return
- */
- public function postLogin(Request $request)
- {
- $credentials = $request->only([$this->username(), 'password', 'captcha']);
- $remember = (bool)$request->input('remember', false);
- /** @var \Illuminate\Validation\Validator $validator */
- $validator = Validator::make($credentials, [
- $this->username() => 'required',
- 'password' => 'required',
- 'captcha' => 'required',
- ]);
- if ($request->input('captcha') != Session::get('captcha'))
- {
- session()->forget('captcha');
- return response()->json([
- 'success' => false,
- 'message' => 'The captcha is incorrect. Please refresh the page and try again.',
- 'refresh_captcha' => true, // 通知前端刷新验证码
- ], 422);; // 422 表示 Unprocessable Entity
- }
- else
- {
- session()->forget('captcha');
- }
- unset($credentials['captcha']);
- if ($validator->fails()) {
- return $this->validationErrorsResponse($validator);
- }
- if ($this->guard()->attempt($credentials, $remember)) {
- //登录成功后从dist_admin_distributor表中取出当前登录用户的公司信息
- $distributor = DistAdminDistributor::where('id', Admin::user()->dist_id)->first();
- if (!$distributor) {
- $this->guard()->logout();
- return $this->validationErrorsResponse([
- $this->username() => $this->getFailedLoginMessage(),
- ]);
- }
- //将当前登录用户的公司信息存入session
- Session::put('distributor', $distributor->toArray());
- // 登录成功后返回登录响应
- return $this->sendLoginResponse($request);
- }
- return $this->validationErrorsResponse([
- $this->username() => $this->getFailedLoginMessage(),
- ]);
- }
- public function getSetting(Content $content)
- {
- $form = $this->settingForm();
- $form->tools(
- function (Form\Tools $tools) {
- $tools->disableList();
- }
- );
- return $content
- ->view('distributor.layouts.content')
- ->title(trans('admin.user_setting'))
- ->body($form->edit(Admin::user()->getKey()));
- }
- /**
- * Model-form for user setting.
- *
- * @return Form
- */
- protected function settingForm()
- {
- return new Form(new Administrator(), function (Form $form) {
- $form->action(admin_url('auth/setting'));
- $form->disableCreatingCheck();
- $form->disableEditingCheck();
- $form->disableViewCheck();
- $form->tools(function (Form\Tools $tools) {
- $tools->disableView();
- $tools->disableDelete();
- });
- $form->display('username', trans('admin.username'));
- $form->text('name', trans('admin.name'))->required();
- //$form->image('avatar', trans('admin.avatar'))->autoUpload();
- $form->password('old_password', trans('admin.old_password'));
- $form->password('password', trans('admin.password'))
- ->minLength(5)
- ->maxLength(20)
- ->customFormat(function ($v) {
- if ($v == $this->password) {
- return;
- }
- return $v;
- });
- $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
- $form->ignore(['password_confirmation', 'old_password']);
- // 添加语言选择的下拉框
- $form->select('language', trans('admin.language'))
- ->options(config('dictionary.languages'))
- ->default('en')
- ->required();; // 设置默认语言
- $form->saving(function (Form $form) {
- if ($form->password && $form->model()->password != $form->password) {
- $form->password = bcrypt($form->password);
- }
- if (! $form->password) {
- $form->deleteInput('password');
- }
- });
- $form->saved(function (Form $form) {
- return $form
- ->response()
- ->success(trans('admin.update_succeeded'))
- //->redirect('/');
- ->script('setTimeout(() => {location.reload();}, 1000);');//保存成功后刷新页面
- });
- // // 在从数据库中取出记录时,如果 language 为空,则默认给它一个值
- // $form->model()->language = $form->model()->language ?: 'en';
- });
- }
- protected function sendLoginResponse(Request $request)
- {
- $request->session()->regenerate();
- $path = $this->getRedirectPath();
- return $this->response()
- ->success(trans('admin.login_successful'))
- ->locationToIntended($path)
- ->locationIf(Admin::app()->getEnabledApps(), $path)
- ->send();
- }
- }
|