97 lines
3.2 KiB
PHP
Executable File
97 lines
3.2 KiB
PHP
Executable File
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use think\Session;
|
||
use think\Controller;
|
||
use ValidateCode\ValidateCode;
|
||
use think\Request;
|
||
use think\Db;
|
||
|
||
class Login extends Controller{
|
||
/**
|
||
* 登录页面
|
||
*/
|
||
public function index(){
|
||
$user_info = Session::get('user_info');
|
||
if(!empty($user_info)){
|
||
$this->redirect('/',302);
|
||
}
|
||
return $this->fetch();
|
||
}
|
||
|
||
/**
|
||
* 处理登录
|
||
*/
|
||
public function do_login(){
|
||
if(Request::instance()->post()){
|
||
// 接收表单提交过来的数据,做数据验证
|
||
$admin = Request::instance()->post('username');
|
||
$password = Request::instance()->post('password');
|
||
$code = Request::instance()->post('code');
|
||
if(!isset($admin) || empty($admin)){
|
||
die(json_encode(['code'=>0,'msg'=>'用户名不能为空!']));
|
||
}
|
||
if(!isset($password) || empty($password)){
|
||
die(json_encode(['code'=>0,'msg'=>'密码不能为空!']));
|
||
}
|
||
if(!isset($code) || empty($code)){
|
||
die(json_encode(['code'=>0,'msg'=>'验证码不能为空!']));
|
||
}
|
||
|
||
// 从session里拿到验证码,进行验证码对比
|
||
$validate_code = Session::get('validate_code');
|
||
if($code != $validate_code){
|
||
die(json_encode(['code'=>0,'msg'=>'验证码错误!']));
|
||
}
|
||
|
||
// 查询客户信息,判断用户名和密码是否正确
|
||
$admin_info = Db::name('admin')->where('admin',$admin)->find();
|
||
if(empty($admin_info)){
|
||
die(json_encode(['code'=>0,'msg'=>'用户不存在!']));
|
||
}
|
||
|
||
if(think_ucenter_md5($password,UC_AUTH_KEY) != $admin_info['password']){
|
||
die(json_encode(['code'=>0,'msg'=>'密码错误!']));
|
||
}
|
||
if($admin_info['status'] != 1){
|
||
die(json_encode(['code'=>0,'msg'=>'该账户未激活!']));
|
||
}
|
||
$last_login_info = Db::name('admin')->where(array('id' => $admin_info['id']))->field(['last_login_time','last_login_ip'])->find();
|
||
Session::set('last_login_info',$last_login_info);
|
||
|
||
// 生成login_token用于WebSocket连接验证
|
||
$login_token = md5($admin_info['id'] . time() . uniqid());
|
||
Db::name('admin')->where(array('id' => $admin_info['id']))->update(array(
|
||
'last_login_time' => time(),
|
||
'last_login_ip' => getIP(),
|
||
'login_token' => $login_token
|
||
));
|
||
|
||
$user_info = Db::name('admin')->where('id',$admin_info['id'])->find();
|
||
Session::set('user_info',$user_info);
|
||
insertAdminLog('登录');
|
||
die(json_encode(['code'=>1,'msg'=>'登录成功!']));
|
||
}else{
|
||
die(json_encode(['code'=>0,'msg'=>'操作错误!']));
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 登出
|
||
*/
|
||
public function logout(){
|
||
insertAdminLog('退出');
|
||
Session::set('user_info',"");
|
||
Session::set('last_login_info',"");
|
||
$this->redirect('/login/index',302);
|
||
}
|
||
|
||
/**
|
||
* 验证码
|
||
*/
|
||
public function validateCode(){
|
||
$validate = new ValidateCode();
|
||
$validate->doimg();
|
||
}
|
||
} |