Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:不错
Account.php
账户控制器
<?php
namespace App\Http\Controllers\admins;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
// 账户相关控制器
class Account extends Controller
{
// 控制登录页面
public function login(){
return view('/admins/account/login');
}
// 生成验证码图片
public function get_captcha(){
VeriCode::create();
}
// 执行登录操作
public function doLogin(Request $request){
// 获取账户信息以及验证码
$username = trim($request->username);
$pwd = trim($request->password);
$vericode = trim($request->vericode);
session_start();
$code = $_SESSION['code'];
// 验证账户信息
if($vericode == '')
exit(json_encode(['code'=>401,'msg'=>'请输入验证码']));
if(strtolower($vericode) != strtolower($code))
exit(json_encode(['code'=>401,'msg'=>'验证码错误']));
if($username=='' || $pwd=='')
exit(json_encode(['code'=>401,'msg'=>'用户名或密码不能为空']));
if(!Auth::attempt(['username'=>$username,'password'=>$pwd]))
exit(json_encode(['code'=>401,'msg'=>'用户名或密码错误']));
dump($username);
dump($pwd);
dump($vericode);
}
}
/**
* 验证码类
*/
class VeriCode{
// 获取验证码配置
private static function _getCodeConfig(){
return [
// 验证码字符集
'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
// 验证码个数
'codeCount' => 4,
// 字体大小
'fontsize' =>16,
// 验证码的宽度
'width' => 100,
// 验证码高度
'height' => 36,
// 是否有干扰点?true有,false没有
'disturbPoint' => true,
// 干扰点个数,disturbPoint开启后生效
'pointCount' => 200,
// 是否有干扰条?true有,false没有
'disturbLine' => true,
// 干扰条个数,disturbLine开启后生效
'lineCount' => 3
];
}
// 创建图片验证码
public static function create(){
// 配置
$config = self::_getCodeConfig();
//创建画布
$image = imagecreatetruecolor($config['width'],$config['height']);
//背景颜色
$bgcolor=imagecolorallocate($image,255,255,255);
imagefill($image,0,0,$bgcolor);
$captch_code = '';//存储验证码
$captchCodeArr = str_split($config['codeStr']);
//随机选取4个候选字符
for($i=0;$i<$config['codeCount'];$i++){
$fontsize = $config['fontsize'];
$fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
$fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];
$captch_code.=$fontcontent;
$_x = $config['width']/$config['codeCount'];
$x=($i*(int)$_x)+rand(5,10); //随机坐标
$y=rand(5,10);
imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串
}
session_start();
$_SESSION['code']=$captch_code;
//增加干扰点
if($config['disturbPoint']){
for($i=0;$i<$config['pointCount'];$i++){
$pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
}
}
//增加干扰线
if($config['disturbLine']){
for($i=0;$i<$config['lineCount'];$i++){
$linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
}
}
//输出格式
header('content-type:image/png');
imagepng($image);
//销毁图片
imagedestroy($image);
}
}
User.php
中增加项:
class User extends Authenticatable
{
protected $table = 'admin';
}
Web.php
路由增加项:
Route::get('/admins/account/login','admins\Account@login');
Route::get('/admins/account/get_captcha','admins\Account@get_captcha');
Route::post('/admins/account/doLogin','admins\Account@doLogin');
login.blade.php
登录页面
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>后台登录系统</title>
<link rel="stylesheet" href="/static/layui/css/layui.css">
<link rel="stylesheet" href="/static/css/admins.css">
<script src="/static/layui/layui.js"></script>
</head>
<body>
<form class="layui-form" action="" >
@csrf
<p>后台登录系统</p>
<hr>
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-block">
<input type="text" id="username" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-block">
<input type="password" id="pwd" name="pwd" placeholder="请输入密码" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">验证码</label>
<div class="layui-input-inline">
<input type="text" id="vericode" name="vericode" placeholder="请输入验证码" autocomplete="off" class="layui-input">
</div>
<img src="/admins/account/get_captcha" alt="看不清,点击刷新" style="cursor: pointer">
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button type="submit" class="layui-btn btn-login">登录</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
<script>
// 获取layui弹出层组件
layui.use('layer',function () {
layer = layui.layer;
$ = layui.jquery;
// layer.msg('hello');
})
// 验证码实时刷新
const img = document.querySelector('img');
img.addEventListener('click',function () {
img.src = '/admins/account/get_captcha?'+Math.random();
});
// 执行登录操作
document.querySelector('.btn-login').addEventListener('click',function (ev) {
// 禁用表单默认提交行为
ev.preventDefault();
// console.log('111');
// 获取用户输入的信息
let username = $('#username').val();
let pwd = $('#pwd').val();
let vericode = $('#vericode').val();
let _token = $('input[name="_token"]').val();
// 提交信息去验证
$.post('/admins/account/doLogin',{username:username,password:pwd,vericode:vericode,_token:_token},function (data) {},'json');
})
</script>
</body>
</html>
登录效果如下:
先创建账户控制器,然后定义一个方法用于跳转到登录页面,切记要写路由
登录页面样式写好以后,设置点击登录按钮时将信息提交处理(还有一个重点部分在于表单中的验证码如何引入)
在控制器中又创建方法用于接收账户信息,进行相应的处理和过滤