manghe/app/admin/controller/Login.php
2025-03-21 19:25:07 +08:00

77 lines
2.1 KiB
PHP
Executable File

<?php
namespace app\admin\controller;
use think\captcha\facade\Captcha;
use app\MyController;
use think\facade\View;
use think\facade\Db;
use app\common\model\Admin;
use app\common\model\AdminLoginLog;
class Login extends MyController
{
// 登录页面
public function login()
{
$config = getConfig('base');
View::assign("title", $config['title']);
return View::fetch("Login/login");
}
// 执行登录
public function dologin()
{
$data = input('post.');
if (!Captcha::check($data['verify'])) {
return $this->renderError('验证码输入错误');
}
#登录条件
$where = [];
$where['username'] = $data['username'];
$where['password'] = admin_md5($data['password']);
$result = Admin::where($where)->find();
if ($result) {
if ($result['status'] !== 0) {
return $this->renderError('您的账号已被封号,请联系管理员');
}
$token = sha1(md5($result['id'] . $result['password'] . $result['random']));
Admin::where(['id' => $result['id']])
->field('id,get_time,token')
->update([
'get_time' => time(),
'token' => $token,
]);
#记录登录日志
AdminLoginLog::insert([
'a_id' => $result['id'],
'ip' => ip2long(request()->ip()),
'addtime' => time(),
]);
session('admin_id', $result['id']);
session('admin_user', $result['username']);
session('admin_token', $token);
return $this->renderSuccess('登录成功');
} else {
return $this->renderError('用户名或密码错误');
}
}
// 退出登录
public function loginout()
{
session('admin_id', null);
session('admin_user', null);
session('admin_token', null);
return redirect('/admin/login');
}
//验证码
public function verify()
{
return Captcha::create();
}
}