控制器代码:
~~~
<?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>
~~~
- thinkphp
- thinkphp笔记
- 后台登陆退出
- config配置
- 隐藏后台模块
- 单独调用腾讯云行为验证码
- api接口跨域问题
- api接口创建案例代码
- 使用gateway worker
- 使用swoole代码笔记
- 使用队列 think-queue笔记
- 后台布局
- MySQL
- 1、关于lnmp mysql的一个坑
- 2、mysql实现group by后取各分组的最新一条
- 其他
- 搞笑的注释代码
- 分页类
- nodejs 打包网址为exe
- 免费天气预报API接口
- Ajax
- 简单的ajax分页1
- 通用ajax-post提交
- 引用的类库文件
- Auth.php
- Auth.php权限控制对应的数据库表结构
- Layui.php
- Pinyin.php
- Random.php
- Tree.php
- Tree2.php
- Js-Jq
- Git的使用
- 3、bootstrap-datetimepicker实现两个时间范围输入
- CentOS安装SSR做梯子
- Python爬虫
- 1、安装Gerapy
- 2、安装Scrapy
- 3、Scrapy使用
- 4、Scrapy框架,爬取网站返回json数据(spider源码)
- 0、Python pip更换国内源(一句命令换源)
- 服务器运维
- 1、宝塔使用webhook更新服务器代码
- 2、搭建内网穿透
- 3、数据库主从同步
- 4、数据库复制
- hui-Shop问题
- 1、前端模板的注意事项
- 2、模板标签