Blogger Information
Blog 15
fans 2
comment 0
visits 35297
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
laravel \ layui 框架 (后台框架搭建)
w™下載一個妳
Original
3912 people have browsed it

laravel 后台搭建

1.后台登录界面演练代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" type="text/css" href="/static/plugins/layui/css/layui.css">
  7. <script type="text/javascript" src="/static/plugins/layui/layui.js"></script>
  8. <title>后台登录</title>
  9. </head>
  10. <body style="background-color: #1E9FFF;">
  11. <div style="position:absolute;left:50%;width:480px;top:50%;margin-left:-240px;
  12. background:#fff;margin-top:-200px;padding:20px;border-radius:4px;box-shadow:5px 5px 20px #444;">
  13. <div class="layui-form">
  14. @csrf
  15. <div class="layui-form-item" style="text-align: center;color:blue;font-weight:500;font-size:26px;">通用后台管理系统</div>
  16. <div class="layui-form-item">
  17. <label class="layui-form-label">用户名</label>
  18. <div class="layui-input-block">
  19. <input type="text" class="layui-input" name="username">
  20. </div>
  21. </div>
  22. <div class="layui-form-item">
  23. <label class="layui-form-label">密码</label>
  24. <div class="layui-input-block">
  25. <input type="password" class="layui-input" name="pwd">
  26. </div>
  27. </div>
  28. <div class="layui-form-item">
  29. <label class="layui-form-label">验证码</label>
  30. <div class="layui-input-inline">
  31. <input type="text" class="layui-input" name="vericode">
  32. </div>
  33. <img id="captcha" src="/admins/account/captcha" style="width: 168px;height:36px;border: 1px solid;color:#cdcdcd; cursor:pointer;" onclick="reload_captcha()">
  34. </div>
  35. <div class="layui-form-item">
  36. <div class="layui-input-block">
  37. <button class="layui-btn" onclick="dologin()">登录</button>
  38. </div>
  39. </div>
  40. </div>
  41. </div>
  42. </body>
  43. </html>
  44. <script type="text/javascript">
  45. layui.use(['layer'],function(){
  46. $ = layui.jquery;
  47. layer = layui.layer;
  48. });
  49. //登录
  50. function dologin(){
  51. var username = $.trim($('input[name="username"]').val());
  52. var pwd = $.trim($('input[name="pwd"]').val())
  53. var vericode = $.trim($('input[name="vericode"]').val());
  54. var _token = $('input[name="_token"]').val();
  55. if(username==''){
  56. return layer.alert('请输入用户名',{icon:2});
  57. }
  58. if(pwd==''){
  59. return layer.alert('请输入密码',{icon:2});
  60. }
  61. if(vericode==''){
  62. return layer.alert('请输入验证码',{icon:2});
  63. }
  64. $.post('/admins/account/dologin',{_token:_token,username:username,pwd:pwd,vericode:vericode},
  65. function(res){
  66. if(res.code>0){
  67. return layer.alert(res.msg,{icon:2});
  68. }
  69. layer.msg(res.msg);
  70. setTimeout(function(){window.location.href='/admins/home/index';},1000);
  71. },'json');
  72. }
  73. function reload_captcha(){
  74. $('#captcha').attr('src','/admins/account/captcha?rand='+Math.random());
  75. }
  76. </script>

2.控制器演练代码:

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. //后台账号
  7. class Account extends Controller
  8. {
  9. //后台登录
  10. public function login(){
  11. return view('/admins/account/login');
  12. }
  13. public function dologin(Request $request){
  14. $username = $request->username;
  15. $pwd = $request->pwd;
  16. $VeriCode = $request->vericode;
  17. session_start();
  18. $sess_code = $_SESSION['code'];
  19. if($VeriCode!=$sess_code){
  20. exit(json_encode(array('code'=>1,'msg'=>'验证码错误')));
  21. }
  22. //查询数据库,校验用户名和密码
  23. if($username==''){
  24. exit(json_encode(array('code'=>1,'msg'=>'用户名不能为空')));
  25. }
  26. if($pwd==''){
  27. exit(json_encode(array('code'=>1,'msg'=>'密码不能为空')));
  28. }
  29. $res = Auth::attempt(['username'=>$username,'password'=>$pwd]);
  30. if(!$res){
  31. exit(json_encode(array('code'=>1,'msg'=>'帐号或密码错误')));
  32. }
  33. exit(json_encode(array('code'=>0,'msg'=>'登录成功')));
  34. }
  35. //验证码
  36. public function captcha(){
  37. VeriCode::create();
  38. }
  39. }
  40. /**
  41. * 验证码类
  42. */
  43. class VeriCode{
  44. // 获取验证码配置
  45. private static function _getCodeConfig(){
  46. return [
  47. // 验证码字符集
  48. 'codeStr' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789',
  49. // 验证码个数
  50. 'codeCount' => 4,
  51. // 字体大小
  52. 'fontsize' =>16,
  53. // 验证码的宽度
  54. 'width' => 100,
  55. // 验证码高度
  56. 'height' => 36,
  57. // 是否有干扰点?true有,false没有
  58. 'disturbPoint' => true,
  59. // 干扰点个数,disturbPoint开启后生效
  60. 'pointCount' => 200,
  61. // 是否有干扰条?true有,false没有
  62. 'disturbLine' => true,
  63. // 干扰条个数,disturbLine开启后生效
  64. 'lineCount' => 3
  65. ];
  66. }
  67. // 创建图片验证码
  68. public static function create(){
  69. // 配置
  70. $config = self::_getCodeConfig();
  71. //创建画布
  72. $image = imagecreatetruecolor($config['width'],$config['height']);
  73. //背景颜色
  74. $bgcolor=imagecolorallocate($image,255,255,255);
  75. imagefill($image,0,0,$bgcolor);
  76. $captch_code = '';//存储验证码
  77. $captchCodeArr = str_split($config['codeStr']);
  78. //随机选取4个候选字符
  79. for($i=0;$i<$config['codeCount'];$i++){
  80. $fontsize = $config['fontsize'];
  81. $fontcolor=imagecolorallocate($image,rand(0,120),rand(0,120),rand(0,120));//随机颜色
  82. $fontcontent = $captchCodeArr[rand(0,strlen($config['codeStr'])-1)];
  83. $captch_code.=$fontcontent;
  84. $_x = $config['width']/$config['codeCount'];
  85. $x=($i*(int)$_x)+rand(5,10); //随机坐标
  86. $y=rand(5,10);
  87. imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor); // 水平地画一行字符串
  88. }
  89. session_start();
  90. $_SESSION['code']=$captch_code;
  91. //增加干扰点
  92. if($config['disturbPoint']){
  93. for($i=0;$i<$config['pointCount'];$i++){
  94. $pointcolor=imagecolorallocate($image,rand(50,200),rand(50,200),rand(50,200));
  95. imagesetpixel($image,rand(1,99),rand(1,29),$pointcolor);
  96. }
  97. }
  98. //增加干扰线
  99. if($config['disturbLine']){
  100. for($i=0;$i<$config['lineCount'];$i++){
  101. $linecolor=imagecolorallocate($image,rand(80,280),rand(80,220),rand(80,220));
  102. imageline($image,rand(1,99),rand(1,29),rand(1,99),rand(1,29),$linecolor);
  103. }
  104. }
  105. //输出格式
  106. header('content-type:image/png');
  107. imagepng($image);
  108. //销毁图片
  109. imagedestroy($image);
  110. }
  111. }

4.路由演练代码:

  1. <?php
  2. use Illuminate\Support\Facades\Route;
  3. /*
  4. |--------------------------------------------------------------------------
  5. | Web Routes
  6. |--------------------------------------------------------------------------
  7. |
  8. | Here is where you can register web routes for your application. These
  9. | routes are loaded by the RouteServiceProvider within a group which
  10. | contains the "web" middleware group. Now create something great!
  11. |
  12. */
  13. Route::get('/', function () {
  14. return view('welcome');//视图、视图引擎
  15. });
  16. //后台登录
  17. Route::get('/admins/account/login','admins\Account@login');
  18. Route::post('admins/account/dologin','admins\Account@dologin');
  19. //后台验证码
  20. Route::get('/admins/account/captcha','admins\Account@captcha');
  21. Route::get('/admins/home/index','admins\Home@index');

5.Home 演练代码:

  1. <?php
  2. namespace App\Http\Controllers\admins;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Auth;
  6. //后台主页
  7. class Home extends Controller
  8. {
  9. //后台登录
  10. public function index(){
  11. return view('/admins/home/index');
  12. }
  13. }

6.演练效果图:

7.学习总结:

在没有学习过后台框架搭建之前,会认为这是多么神秘且很难的事情,学习过之后发现,虽然知道大致的方法,知道后台是由哪些部分组成的,但是实际操作起来还是不容易,看着老师是轻轻松松的搭建完成,课后看着录播跟着实操演练困难重重,就这一个后台框架搭建,我足足重复练习了5次才通过,途中有出现代码写错,单词写错,步骤写错,等等一系列原因,其中大部分因素是自己不够细心,其二是自己对基础不熟练,另外因为工作导致没有太多时间进行复习演练,之前对于程序是没有任何功底,所以前面的基础也没能跟上,现在三阶段实战也没能及时跟上。

Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:写的还好!一定勤加练习,尽量按照自己的想法去实现一些功能。
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post