89 lines
2.4 KiB
PHP
89 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\admin\model\SystemAdmin;
|
|
use app\common\controller\AdminController;
|
|
use think\captcha\facade\Captcha;
|
|
use think\facade\Env;
|
|
use think\Response;
|
|
|
|
/**
|
|
* Class Login
|
|
* @package app\admin\controller
|
|
*/
|
|
class Login extends AdminController
|
|
{
|
|
|
|
/**
|
|
* 初始化方法
|
|
*/
|
|
public function initialize()
|
|
{
|
|
parent::initialize();
|
|
$action = $this->request->action();
|
|
if (!empty(session('admin')) && !in_array($action, ['out'])) {
|
|
$adminModuleName = config('app.admin_alias_name');
|
|
$this->success('已登录,无需再次登录', [], __url("@{$adminModuleName}"));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 用户登录
|
|
* @return string
|
|
* @throws \Exception
|
|
*/
|
|
public function index(): string
|
|
{
|
|
$captcha = Env::get('EASYADMIN.CAPTCHA', 1);
|
|
if ($this->request->isPost()) {
|
|
$post = $this->request->post();
|
|
$rule = [
|
|
'username|用户名' => 'require',
|
|
'password|密码' => 'require',
|
|
'keep_login|是否保持登录' => 'require',
|
|
];
|
|
$captcha == 1 && $rule['captcha|验证码'] = 'require|captcha';
|
|
$this->validate($post, $rule);
|
|
$admin = SystemAdmin::where(['username' => $post['username']])->find();
|
|
if (empty($admin)) {
|
|
$this->error('用户不存在');
|
|
}
|
|
if (password($post['password']) != $admin->password) {
|
|
$this->error('密码输入有误');
|
|
}
|
|
if ($admin->status == 0) {
|
|
$this->error('账号已被禁用');
|
|
}
|
|
$admin->login_num += 1;
|
|
$admin->save();
|
|
$admin = $admin->toArray();
|
|
unset($admin['password']);
|
|
$admin['expire_time'] = $post['keep_login'] == 1 ? true : time() + 7200;
|
|
session('admin', $admin);
|
|
$this->success('登录成功');
|
|
}
|
|
$this->assign('captcha', $captcha);
|
|
$this->assign('demo', $this->isDemo);
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 用户退出
|
|
*/
|
|
public function out()
|
|
{
|
|
session('admin', null);
|
|
$this->success('退出登录成功');
|
|
}
|
|
|
|
/**
|
|
* 验证码
|
|
* @return Response
|
|
*/
|
|
public function captcha(): Response
|
|
{
|
|
return Captcha::create();
|
|
}
|
|
}
|