TP6登录
登录
- TP6登录
- 前言
- 总结
前言
主要就是为了实现登录的一些封装,查找跟之前写的普通登录区别
提示:以下是本篇文章正文内容,下面案例可供参考
- 图片示例:
2.主要代码实现:
controller层:
public function index(){if ($this->request->isPost()){$param = $this->request->param();
// dd($param);$validate = new Login();if (!$validate->scene('login')->check($param)) $this->error($validate->getError());$data =['username'=>$param['username'],'password'=>md5($param['password'])];$admin_data = (new User())->login($data);if (empty($admin_data)) $this->error('用户名或密码不正确');Session::set('login',$admin_data);//return show(config("status.api.success"),'登录成功',$data);return json(["code"=>"200","msg"=>"登录成功","data"=>$admin_data]);}return view();}
其中的validate和在common/business层创建一个类
validate/Login:
business/User:
v层代码实现 (用的是layui前端框架):
layui官网连接
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/layui/layui/css/layui.css"><script src="/layui/layui/layui.js"></script><script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body><form class="layui-form" action=""><div class="layui-form-item"><div class="layui-inline"><label class="layui-form-label">账号:</label><div class="layui-input-inline"><input type="text" name="username" lay-verify="required" 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="password" name="password" lay-verify="pass" placeholder="请输入密码" autocomplete="off" class="layui-input"></div><div class="layui-form-mid layui-word-aux">请填写6到20位密码</div></div><div class="layui-form-item"><div class="layui-row"><label class="layui-form-label layadmin-user-login-icon layui-icon layui-icon-vercode">验证码:</label><div class="layui-col-xs7 layui-input-inline">
<!-- <label class="layui-form-label">验证码:</label>--><input type="text" name="vercode" lay-verify="required" placeholder="图形验证码" class="layui-input"></div><div class="layui-col-xs5"><div style="margin-left: 10px"><img src="{:url('usercontroller/verify')}" class="ladmin-user-login-codeimg" id="vercode" alt=""></div></div></div></div><div class="layui-form-item"><div class="layui-input-block"><button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button><button type="reset" class="layui-btn layui-btn-primary">重置</button></div></div></div>
</form>
</body>
</html>
<script>layui.use(['layer','form','jquery'], function() {var $ = layui.$;var form = layui.form;var layer=layui.layer;form.render();//自定义验证规则form.verify({pass: [/^[\S]{6,20}$/,'密码必须6到20位,且不能出现空格'],content: function(value){layedit.sync(editIndex);}});//监听提交form.on('submit(demo1)', function(data){$.ajax({url:"{:url('UserController/index')}",type:'POST',data:data.field,dataType:'json',success:function (res) {console.log(res.code)if (res.code == 200){layer.msg('登录成功',{offset:'15px',icon:1,time:1000},function () {location.href="{:url('Approve/index')}"});}else {layer.msg(res.msg);}}})return false;});// 验证码$('#vercode').click(function () {$(this).attr('src',"{:url('usercontroller/verify')}?" + Date.parse(new Date()))})})
</script>
3.在config下的captcha文件具体修改验证码样式等
总结
以上就是今天要讲的内容,本文仅仅简单介绍了一下tp6登录封装方法的使用,代码的优化会提高效率。