## 登录/注册
### 创建登录控制器
```php
php think make:controller admin/Login --plain
```
> —plain是为了创建控制器更纯净,没有先创建好的一些方法
### 下载验证码插件
这是配合ThinkPHP 5.1版本的
```composer
composer require topthink/think captcha=2.0.*
```
在`Login`控制器里添加`verify()`方法用于验证码
```php
public function verify()
{
$config = [
// 验证码字体大小
'fontSize' => 16,
'length' => 3,
// 关闭验证码杂点
'useNoise' => false,
'useCurve' => false,
];
$captcha = new Captcha($config);
return $captcha->entry();
}
```
在*html*页面中引入
```php+HTML
<img src="{:url('Login/verify')}" class="reloadverify">
```
#### 点击验证码图片随机变换的JS
```javascript
<script>
$(function () {
var verifyimg = $(".reloadverify").attr("src");
$(".reloadverify").click(function () {
if (verifyimg.indexOf('?') > 0) {
$(".reloadverify").attr("src", verifyimg + '&random=' + Math.random());
} else {
$(".reloadverify").attr("src", verifyimg.replace(/\?.*$/, '') + '?' + Math.random());
}
});
});
</script>
```
> 这里的`reloadverify`必须要在`img`中进行定义**class**
### 登录使用Ajax进行登录
> 必须引入jQuery,在配合layui的layer.js进行弹窗效果提示
>
> 在input框中必须输入对应的name值
js代码:
```javascript
$(function () {
$('#login').click(function () {
$.ajax({
url: "{:url('admin/Login/login')}",
type: 'post',
data: $('form').serialize(),
dataType: 'json',
success: function (data) {
if (data.code == 1) {
layer.msg(data.msg, {
icon: 6,
time: 1000
}, function () {
location.href = "{:url('admin/Index/index')}";
})
} else {
layer.open({
title: '登录失败',
content: data.msg,
ico: 5,
anim: 6
})
}
}
});
return false;
});
});
```
### 用Ajax进行注册
```javascript
$(function () {
$('#register').click(function () {
$.ajax({
url: "{:url('admin/Register/register')}",
type: 'post',
data: $('form').serialize(),
dataType: 'json',
success: function (data) {
if (data.code == 1) {
layer.msg(data.msg, {
icon: 6,
time: 1000
}, function () {
location.href = "{:url('admin/Login/index')}";
})
} else {
layer.open({
title: '注册失败',
content: data.msg,
ico: 5,
anim: 6
})
}
}
});
return false;
});
});
```
## 创建模型
我会将模型创建在`application`下的`common`目录,属于前台和后台的公共使用
```php
php think make:model common/Manager
```
用模型进行处理数据
```php
use SoftDelete;
// 自动引入时间戳
protected $autoWriteTimestamp = true;
// 用户登录验证
public static function login($data)
{
$validate = new LoginValidate();
if (!$validate->scene('login')->check($data)) {
return json(['code' => 0, 'msg' => $validate->getError()]);
}
$res = self::get(['username' => $data['username']]);
if (!$res) {
return json(['code' => 0, 'msg' => '账号不存在']);
}
if (!$res['status']) {
return json(['code' => 0, 'msg' => '账号禁用']);
}
if (md5($data['password']) == $res['password']) {
session('adminid', $res['id']);
session('username', $res['username']);
session('nickname', $res['nickname']);
return json(['code' => 1, 'msg' => '登录成功']);
}
return json(['code' => 0, 'msg' => '密码不正确']);
}
// 获取所有管理员的信息
public static function getAllManager()
{
$lists = self::where('id', 'asc');
return $lists;
}
// 添加修改管理员
public static function store($data)
{
if (isset($data['id'])) {
$scene = 'edit';
$msg = '修改';
$action = "update";
} else {
$scene = 'add';
$msg = '添加';
$action = "create";
}
$data['password'] = md5($data['password']);
// 写入数据库
unset($data['confirm_password']);
$result = self::$action($data);
if (!$result) {
return json(['code' => 0, 'msg' => $msg.'失败']);
}
return json(['code' => 1, 'msg' => $msg.'成功']);
}
public static function del($id)
{
$result = self::destroy($id);
if ($result) {
return json(['code' => 1, 'msg' => '删除成功']);
}
return json(['code' => 0, 'msg' => '删除失败']);
}
```
## 创建验证器
```php
php think make:validate common/LoginValidate
```
```php
/**
* 定义验证规则
* 格式:'字段名' => ['规则1','规则2'...]
*
* @var array
*/
protected $rule = [
'username' => 'require|unique:manager',
'password' => 'require|length:6,20',
'confirm_password' => 'require|confirm:password',
'code' => 'require',
'email' => 'require|email|unique:manager'
];
/**
* 定义错误信息
* 格式:'字段名.规则名' => '错误信息'
*
* @var array
*/
protected $message = [
'id.require' => 'id不正确',
'username.require' => '用户名不能为空',
'username.unique' => '用户名必须唯一',
'password.require' => '密码不能为空',
'password.length' => '密码长度在6~20个字符之内',
'confirm_password.require' => '确认密码不能为空',
'confirm_password.confirm' => '两次密码输入不相同',
'code.require' => '验证码不能为空',
'email.require' => '邮箱不能为空',
'email.email' => '邮箱格式不正确',
'email.unique' => '该邮箱已存在'
];
protected $scene = [
'login' => ['username' => 'require|unique:manager',"password"],
'add' => ['username', 'password', 'confirm_password', 'email'],
'edit' => ['id']
];
```
## 登录控制器
```php
/**
* 显示登录页面
* @return \think\response\View
*/
public function index()
{
return view();
}
public function login()
{
$data = input('post.');
$result = Manager::login($data);
return $result;
}
```
## 注册控制器
```php
// 注册页面
public function index()
{
return view();
}
public function register()
{
$data = input('post.');
$validate = new LoginValidate();
if (!$validate->scene('add')->check($data)) {
return json(['code' => 0, 'msg' => $validate->getError()]);
}
$data['created_at'] = time();
$data['status'] = 1;
$result = Manager::store($data);
return $result;
}
```
## ThinkPHP5 模型时间戳设置
> 单独在模型里面设置(推荐)
```
protected $autoWriteTimestamp = true; // int 型
protected $autoWriteTimestamp = 'datetime'; // datetime 类型
protected $autoWriteTimestamp = false; // 关闭自动写入时间戳
protected $updateTime = false; // 只关闭自动写入update_time字段
```
> 在config里添加全局设置
```
/ 开启自动写入时间戳字段(官方手册这么说,自己并没有测试出来)
'auto_timestamp' => true, // 默认为int型
'auto_timestamp' => 'datetime', // datetime类
'auto_timestamp' => false, // 关闭全局自动写入时间字段
```
> 如果数据表字段不是默认值(假设数据库字段名为create_at和update_at)
```
// 定义时间戳字段名
protected $createTime = 'create_at'; // 默认的字段为create_time 和 update_time
protected $updateTime = 'update_at';
```
- PHP获取客户端浏览器信息和版本
- PHP获取客户端操作系统信息
- 无限级分类
- git使用
- 权限检测思路
- Vue学习
- 遇到的一些问题
- PHP的编码思维和技巧
- mysql复习
- tp5
- ThinkPHP5.x 公共函数
- TP5登录注册
- TP5使用模板继承
- ThinkPHP5.1 清除缓存
- thinkphp5实现安装程序
- 安全
- tp中实现跨域代码
- ThinkPHP5.1配合pjax实现菜单栏无刷新跳转
- 获取数据库版本和数据库大小
- 模型的基本CURD操作
- 商品spu
- 全局异常处理类
- ExceptionHandler
- BaseException
- PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明
- 微信小程序
- wx:for
- tp6
- 分离的一些模块
- session开启
- Spring
- 依赖注入
- 数据结构
- 二叉树
- js获取地址栏变量
- PHP设计模式
- 面向对象
- PHP1
- PHP性能优化
- Java学习
- static关键字
- 多态
- 接口、阶乘
- 大佬给的面试题
- 访问量为5000万的博客系统设计
- PHP可变参数
- Nginx的配置案例
- 求数组中的最大值,并返回数组索引
- PHP面试方向
- PHP数组工具类ArrUtil
- 字符串工具类StrUtil
- PHP使用curl发送请求
- mysql
- PHP上传base64图片处理函数
- webstorm小程序常用配置
- 邮箱正则表达式
- leetcode mysql记录
- 函数库