pull/3/head
parent
cbc671aaeb
commit
d0eae0fcb2
@ -1,87 +1,90 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace App\Admin\Controllers;
|
namespace App\Admin\Controllers;
|
||||||
|
|
||||||
use Encore\Admin\Auth\Database\Administrator;
|
use Encore\Admin\Auth\Database\Administrator; // 引入管理员模型
|
||||||
use Encore\Admin\Controllers\AuthController as BaseAuthController;
|
use Encore\Admin\Controllers\AuthController as BaseAuthController; // 继承 Encore\Admin 的 AuthController
|
||||||
use Encore\Admin\Facades\Admin;
|
use Encore\Admin\Facades\Admin; // 引入 Admin 门面
|
||||||
use Encore\Admin\Form;
|
use Encore\Admin\Form; // 引入表单处理类
|
||||||
use Encore\Admin\Grid;
|
use Encore\Admin\Grid; // 引入 Grid 类
|
||||||
use Encore\Admin\Layout\Content;
|
use Encore\Admin\Layout\Content; // 引入布局类
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request; // 引入请求类
|
||||||
use Illuminate\Support\Facades\Validator;
|
use Illuminate\Support\Facades\Validator; // 引入验证器
|
||||||
|
|
||||||
class AuthController extends BaseAuthController
|
class AuthController extends BaseAuthController
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 覆盖默认的登录方法
|
* 覆盖默认的登录方法
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
*
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function postLogin(Request $request)
|
public function postLogin(Request $request)
|
||||||
{
|
{
|
||||||
|
// 获取用户名和密码
|
||||||
$credentials = $request->only([$this->username(), 'password']);
|
$credentials = $request->only([$this->username(), 'password']);
|
||||||
|
|
||||||
/** @var \Illuminate\Validation\Validator $validator */
|
/** @var \Illuminate\Validation\Validator $validator */
|
||||||
|
// 对输入的凭证进行验证
|
||||||
$validator = Validator::make($credentials, [
|
$validator = Validator::make($credentials, [
|
||||||
$this->username() => 'required',
|
$this->username() => 'required', // 用户名为必填
|
||||||
'password' => 'required',
|
'password' => 'required', // 密码为必填
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
// 如果验证失败,返回错误信息
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
return back()->withInput()->withErrors($validator);
|
return back()->withInput()->withErrors($validator);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果验证通过,尝试进行身份验证
|
||||||
if ($this->guard()->attempt($credentials)) {
|
if ($this->guard()->attempt($credentials)) {
|
||||||
|
// 登录成功后,执行身份验证后的操作(比如记录 IP 地址)
|
||||||
// 验证是否登录的 ip
|
|
||||||
$this->authenticated($this->guard()->user());
|
$this->authenticated($this->guard()->user());
|
||||||
|
|
||||||
// 记录登录日期
|
// 记录登录日期并返回成功响应
|
||||||
return $this->sendLoginResponse($request);
|
return $this->sendLoginResponse($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 如果身份验证失败,返回失败信息
|
||||||
return back()->withInput()->withErrors([
|
return back()->withInput()->withErrors([
|
||||||
$this->username() => $this->getFailedLoginMessage(),
|
$this->username() => $this->getFailedLoginMessage(), // 获取失败的登录信息
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录之后提示 ip 地址
|
* 登录之后提示 IP 地址
|
||||||
*
|
*
|
||||||
* @param Administrator $user
|
* @param Administrator $user
|
||||||
*/
|
*/
|
||||||
protected function authenticated(Administrator $user)
|
protected function authenticated(Administrator $user)
|
||||||
{
|
{
|
||||||
$ip = request()->getClientIp();
|
$ip = request()->getClientIp(); // 获取当前登录用户的 IP 地址
|
||||||
|
|
||||||
// 如果两次 ip 不一样, 提示风险
|
// 如果当前登录 IP 与上次登录的 IP 不一致,提示风险
|
||||||
if (! is_null($user->login_ip) && $ip != $user->login_ip) {
|
if (! is_null($user->login_ip) && $ip != $user->login_ip) {
|
||||||
|
|
||||||
admin_info('上一次登录的地址与本次不同,如果不是本人操作,建议及时修改密码');
|
admin_info('上一次登录的地址与本次不同,如果不是本人操作,建议及时修改密码');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 更新用户的登录 IP 地址
|
||||||
$user->login_ip = $ip;
|
$user->login_ip = $ip;
|
||||||
$user->save();
|
$user->save(); // 保存更新后的 IP 地址
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置更新操作
|
||||||
|
*/
|
||||||
public function putSetting()
|
public function putSetting()
|
||||||
{
|
{
|
||||||
$form = $this->settingForm();
|
$form = $this->settingForm(); // 获取设置表单
|
||||||
|
|
||||||
|
// 表单提交时的处理逻辑
|
||||||
$form->submitted(function (Form $form) {
|
$form->submitted(function (Form $form) {
|
||||||
|
// 如果是开发环境,则禁止操作
|
||||||
if (app()->environment('dev')) {
|
if (app()->environment('dev')) {
|
||||||
|
admin_toastr('开发环境不允许操作', 'error'); // 显示错误提示
|
||||||
admin_toastr('开发环境不允许操作', 'error');
|
return back()->withInput(); // 返回上一页面并保留输入
|
||||||
return back()->withInput();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return $form->update(Admin::user()->id);
|
// 执行设置更新
|
||||||
|
return $form->update(Admin::user()->id); // 更新当前登录用户的设置
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
// 引入 WangEditor 扩展类
|
||||||
use App\Admin\Extensions\WangEditor;
|
use App\Admin\Extensions\WangEditor;
|
||||||
|
// 引入 Encore\Admin\Form 类
|
||||||
use Encore\Admin\Form;
|
use Encore\Admin\Form;
|
||||||
|
|
||||||
|
// 忘记默认的 'map' 和 'editor' 字段,以便自定义处理
|
||||||
Form::forget(['map', 'editor']);
|
Form::forget(['map', 'editor']);
|
||||||
|
|
||||||
|
// 扩展表单功能,注册 WangEditor 作为一个新的表单字段类型
|
||||||
Form::extend('editor', WangEditor::class);
|
Form::extend('editor', WangEditor::class);
|
||||||
|
Loading…
Reference in new issue