## 登錄/注冊
### 創建登錄控制器
```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記錄
- 函數庫