🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
控制器代码: ~~~ <?php /** * Created by PhpStorm. * User: 小灰灰 * Date: 2019/12/28 0028 * Time: 21:03 */ namespace app\admin\controller; use think\Controller; use think\captcha\Captcha; use think\Db; use think\facade\{Cookie,Session,Cache}; class Login extends Controller { public function index() { $captcha = new Captcha(); if(input('post.dosubmit')){ if(config('huiadmin.login_captcha')){ if( !captcha_check(input('code') )){ return json(['status'=>1001,'msg'=>'验证码输出错误,请重新输入!!!']); } } $user_info = Db::name('admin')->where('username',input('post.username'))->find(); if(empty($user_info['username'])){ return json(['status'=>1002,'msg'=>'用户名或密码错误!']); } $login_failure_retry = config('huiadmin.login_failure_retry'); $login_failure_times = config('huiadmin.login_failure_times'); $login_failure_min = config('huiadmin.login_failure_min'); if( $login_failure_retry && $user_info['loginfailure'] >=$login_failure_times && (time()-$user_info['updatetime'])< $login_failure_min*60 ) { return json(['status'=>1002,'msg'=>'密码错误次数超过'.$login_failure_times.'次,请'.$login_failure_min.'分钟之后重试!']); } $pass = md5(md5(input('post.password')) . $user_info['salt']); if($user_info['password']!==$pass){ Db::name('admin')->where('username',input('post.username'))->setInc('loginfailure'); return ['status'=>1002,'msg'=>'用户名或密码错误!!!']; }else{ if($user_info['status']!='normal'){ return ['status'=>1003,'msg'=>'该用户已被禁止访问']; }else{ $user_session_info = [ 'uid' => $user_info['id'], 'username' => $user_info['username'], 'nickname' => $user_info['nickname'], 'logintime'=> $user_info['logintime'], 'loginip' => $user_info['loginip'], ]; Cache::clear(); session('user_info', $user_session_info); $data = ['loginip'=>ip(),'loginfailure'=>0,'logintime'=>time()]; Db::name('admin')->where('username',input('post.username'))->data($data)->update(); return['status'=>1,'msg'=>'登录成功,正在跳转~~~']; } } }else{ return $this->fetch(); } } //自定义验证码类 public function verify() { $captcha = new Captcha(config('captcha.')); return $captcha->entry(); } /** * 退出 */ public function logout() { Session::clear(); Cookie::clear(); $this->success('退出成功!', 'index'); } /** *获取bing背景图 */ public function getbing_bgpic(){ $idx = input('idx'); $api = "http://cn.bing.com/HPImageArchive.aspx?format=js&idx=$idx&n=1"; $data = self::object2array(json_decode(self::get_url($api))); $pic_url = $data['images'][0]->{'url'}; //获取数据里的图片地址 if($pic_url){ $images_url ="https://cn.bing.com/".$pic_url; //如果图片地址存在,则输出图片地址 }else{ $images_url="https://s1.ax1x.com/2018/12/10/FGbI81.jpg"; //否则输入一个自定义图 } header("Location: $images_url"); //header跳转 } private function get_url($url) { $ch = curl_init(); $header[] = ""; curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); $content = curl_exec($ch); curl_close($ch); return $content; } private function object2array($object) { if (is_object($object)) { foreach ($object as $key => $value) { $array[$key] = $value; } } else { $array = $object; } return $array; } } ~~~ 登录页面HTML ~~~ <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>HuiCMF-后台管理系统-v3.0</title> <meta name="renderer" content="webkit|ie-comp|ie-stand"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta http-equiv="Cache-Control" content="no-siteapp" /> <link rel="shortcut icon" href="./favicon.ico" type="image/x-icon" /> <link rel="stylesheet" href="__STATIC_ADMIN__css/font.css"> <link rel="stylesheet" href="__STATIC_ADMIN__css/weadmin.css"> <link rel="stylesheet" href="__STATIC_ADMIN__css/style.css"> </head> <body class="login-bg" style="background: url({:url('getbing_bgpic')})"> <div class="login"> <div class="message">小灰灰内容管理系统-后台管理系统</div> <div id="darkbannerwrap"></div> <form method="post" class="layui-form" action="javascript:;" onsubmit="return dosub(this)"> <input name="username" id="username" placeholder="用户名" type="text" lay-verify="required" class="layui-input" required="required"> <hr class="hr15"> <input name="password" id="password" lay-verify="required" placeholder="密码" type="password" class="layui-input" required="required"> {if config('huiadmin.login_captcha')} <hr class="hr15"> <img src="{:url('verify')}" id="imgVcode" alt="captcha" title="点击刷新验证码" border="0" class="verifyimg" onclick="this.src=this.src+'?'" style="float:right;height: 50px;"/> <input type="text" name="code" id="code" class="login_input verify_input" lay-verify="required" placeholder="验证码" required="required" style="float:left; width: 40%"> {/if} <hr class="hr20"> <input type="hidden" id="dosubmit" value="1"> <input class="loginin" value="登录" lay-submit lay-filter="login" style="width:100%;" type="submit"> </form> </div> <div id="click_pic"> <a href="javascript:;" class="click_pic click_pic_a" title="上一个图像"></a> <a href="javascript:;" class="click_pic click_pic_b" title="下一个图像"></a> </div> <div id="focus_ovr" data-bm="21"></div> <!-- 底部结束 --> </body> </html> {include file='footer'/} <script> $(document).ready(function () { if (window != top) { top.location.href = location.href; } }); function dosub() { $.post("{:url('index')}",{ username:$('#username').val(), password:$('#password').val(), code:$('#code').val(), dosubmit:$('#dosubmit').val(), },function (res) { if(res.status=='1001'){ $('#imgVcode').attr("src","{:url('verify')}"+"?" + Math.random()); layer.msg(res.msg,{'icon':2}); } if(res.status=='1002'||res.status=='1003'){ layer.msg(res.msg,{'icon':2},function () { location.reload(); }); } if(res.status==1){ layer.msg(res.msg,{'icon':1},function () { window.location.href="{:url('index/index')}"; }); } }) } var i = 0; $(".click_pic_a").click(function () { i += 1; if (i <= 7) { $(".login-bg").css({ "background": "url({:url('getbing_bgpic')}?idx=" + i + ")", "transition": "500ms ease 500ms", "-webkit-transition": "500ms ease 500ms" }); $(".click_pic_a").css("opacity", "1") } else { i = 7; alert("没有了") } }); $(".click_pic_b").click(function () { i -= 1; if (i >= 0) { $(".login-bg").css({ "background": "url({:url('getbing_bgpic')}?idx=" + i + ")", "transition": "500ms ease 500ms", "-webkit-transition": "500ms ease 500ms" }); $(".click_pic_b").css("opacity", "1") } else { i = 0; alert("没有了"); } }) </script> ~~~