THINKPHP6 AJAX登陆与退出
登陆
登陆按钮:
<button class="layui-btn layui-btn-fluid" onclick="login()">登 陆</button>
-------------------------
AJAX代码:
function login(){ $.post("/index.php/bews/Login/login",$('form').serialize(),function(res){ if(res.code==0){ layer.msg(res.msg,{'icon':1}); // 设置跳转时间 setTimeout(function(){ window.location.href="/index.php/bews/index/index"; },1000); }else{ layer.msg(res.msg,{icon: 2}); } },'json'); }
---------------------------
THINKPHP6代码:
public function login()
{
$account=trim(input('post.account'));
if(empty($account)){
echo json_encode(['code'=>1,'msg'=>'请输入帐号']);
exit;
}
$password=trim(input('post.password'));
if(empty($password)){
echo json_encode(['code'=>1,'msg'=>'请输入密码']);
exit;
}
$code=trim(input('post.code'));
if(empty($code)){
echo json_encode(['code'=>1,'msg'=>'请输入验证码']);
exit;
}
if(!captcha_check($code)){
echo json_encode(['code'=>1,'msg'=>'验证码出错']);
exit;
};
$user=Db::table('user')->where('account',$account)->find();
if($user['account']!=$account){
echo json_encode(['code'=>1,'msg'=>'用户名不正确']);
exit;
}
if($user['password']!=md5($password)){
echo json_encode(['code'=>1,'msg'=>'密码不正确']);
exit;
}
Cookie::set('admin_id',$user['uid']);
Cookie::set('admin_name',$user['name']);
// 更新登陆时间和次数
Db::table('user')->where('uid',$user['uid'])->update([
'times_login'=>$user['times_login']+1,
'time_last'=>time()
]);
echo json_encode(['code'=>0,'msg'=>'登录成功!']);
}
退出代码
退出按钮:
<dd layadmin-event="logout" style="text-align:center;" onclick="logout()">
<a>退出</a>
</dd>
-------------------------
AJAX代码:
function logout(){ layer.confirm('确定要退出吗?', { icon:3, btn: ['确定','取清'] //按钮 }, function(){ $.get("/index.php/bews/login/logout",function(res){ if(res.code==0){ layer.msg(res.msg,{'icon':1}); // 设置跳转时间 setTimeout(function(){ window.location.href="/index.php/bews/Login/index"; },1000); }else{ layer.msg(res.msg,{icon: 2}); } },'json') }) }
-------------------------
THINKPHP6 代码
function logout(){
layer.confirm('确定要退出吗?', {
icon:3,
btn: ['确定','取清'] //按钮
}, function(){
$.get("/index.php/bews/login/logout",function(res){
if(res.code==0){
layer.msg(res.msg,{'icon':1});
// 设置跳转时间
setTimeout(function(){
window.location.href="/index.php/bews/Login/index";
},1000);
}else{
layer.msg(res.msg,{icon: 2});
}
},'json')
})
}