本节我们将实现以下功能:创建并连接数据库、创建运营表、创建model类、编写login模板和编写登录功能,下面开始写bug
一、创建并连接数据库
使用Navicat Premium创建一个连接本地数据库,并创建一个数据库,我的库名称为:local_blog_com。
连接数据库:
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
return [
// 数据库类型
'type' => 'mysql',
// 服务器地址
'hostname' => '127.0.0.1',
// 数据库名
'database' => 'local_blog_com',
// 用户名
'username' => 'root',
// 密码
'password' => 'shenlin',
// 端口
'hostport' => '3306',
// 连接dsn
'dsn' => '',
// 数据库连接参数
'params' => [],
// 数据库编码默认采用utf8
'charset' => 'utf8',
// 数据库表前缀
'prefix' => 'blog_',
// 数据库调试模式
'debug' => true,
// 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'deploy' => 0,
// 数据库读写是否分离 主从式有效
'rw_separate' => false,
// 读写分离后 主服务器数量
'master_num' => 1,
// 指定从服务器序号
'slave_no' => '',
// 自动读取主库数据
'read_master' => false,
// 是否严格检查字段是否存在
'fields_strict' => true,
// 数据集返回类型
'resultset_type' => 'array',
// 自动写入时间戳字段
'auto_timestamp' => true,
// 时间字段取出后的默认时间格式
'datetime_format' => 'Y-m-d H:i:s',
// 是否需要进行SQL性能分析
'sql_explain' => false,
// Builder类
'builder' => '',
// Query类
'query' => '\\think\\db\\Query',
// 是否需要断线重连
'break_reconnect' => false,
// 断线标识字符串
'break_match_str' => [],
];二、创建运营表

使用 Navicat 在此数据库内创建blog_user表,字段如下:
/*
Navicat MySQL Data Transfer
Source Server : 本地数据库
Source Server Version : 50554
Source Host : localhost:3306
Source Database : local_blog_com
Target Server Type : MYSQL
Target Server Version : 50554
File Encoding : 65001
Date: 2019-05-10 20:32:16
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for blog_user
-- ----------------------------
DROP TABLE IF EXISTS `blog_user`;
CREATE TABLE `blog_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`create_time` int(11) DEFAULT NULL COMMENT '创建时间',
`update_time` int(11) DEFAULT NULL COMMENT '修改时间',
`user_name` varchar(32) NOT NULL COMMENT '用户名',
`user_pass` varchar(32) NOT NULL COMMENT '密码',
`user_status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '0:正常1:冻结',
`user_phone` varchar(32) DEFAULT NULL COMMENT '手机号',
`qq_token` varchar(32) DEFAULT NULL COMMENT 'QQtoken',
`wx_token` varchar(32) DEFAULT NULL COMMENT '微信token',
`bd_token` varchar(32) DEFAULT NULL COMMENT '百度token',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='运营表';三、创建model类
我们将编写获取列表(分页)、获取列表、获取单数据、添加数据、修改数据、删除数据;

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/10 0010
* Time: 下午 8:33
*/
namespace app\common\model;
use think\Model;
class User extends Model
{
/**
* 数据分页列表
* @param $where
* @param string $field
* @param string $order
* @param int $pageNum
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function getPageList($where,$field = '*',$order = 'id desc',$pageNum = 15)
{
$data = $this->where($where)->field($field)->order($order)->paginate($pageNum);
return $data;
}
/**
* 数据列表
* @param $where
* @param string $field
* @param string $order
* @return array|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getDataList($where,$field = '*',$order = 'id desc')
{
$data = $this->where($where)->field($field)->order($order)->select();
return $data;
}
/**
* 获取单条数据
* @param $where
* @param string $field
* @return array|\PDOStatement|string|Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getDataOne($where,$field = '*')
{
$data = $this->where($where)->field($field)->find();
return $data;
}
/**
* 添加数据
* @param $data
* @return int|string
*/
public function getAddData($data)
{
$res = $this->save($data);
return $res;
}
/**
* 修改数据
* @param $where
* @param $data
* @return int|string
* @throws \think\Exception
* @throws \think\exception\PDOException
*/
public function getUpdateData($where,$data)
{
$res = $this->where($where)->update($data);
return $res;
}
/**
* 删除数据
* @param $id
* @return bool
* @throws \Exception
*/
public function getDeleteData($id)
{
if(!$id){
return false;
}
$res = $this->delete($id);
return $res;
}
}四、编写login模板
代码请参考码云提交编码

五、编写登录功能
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2019/5/9 0009
* Time: 下午 9:38
*/
namespace app\admin\controller;
use app\common\model\User;
use think\captcha\Captcha;
use think\Controller;
class Login extends Controller
{
protected $user_model;
public function __construct()
{
parent::__construct();
$this->user_model = new User();
}
/**
* 登录页面
* @return \think\response\View
*/
public function index()
{
return view();
}
/**
* 登录处理
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getLogin()
{
$userName = input('post.userName'); //账户
$userPass = input('post.userPass'); //密码
$code = input('post.code'); //验证码
if(!captcha_check($code)){
// 验证失败
$this->error('验证码错误');
};
if(!$userName || !$userPass){
//数据不完整
$this->error('请填写账号或密码');
}
$res = $this->user_model->getDataOne(['user_name'=>$userName],'user_status,user_pass');
if(!$res){
$this->error('账号或密码错误');
}
if($res['user_pass'] !== md5($userPass)){
$this->error('账号或密码错误');
}
if($res['user_status'] == 2){
$this->error('账户已冻结');
}
//记录session
session('userName',$userName,'thinkBlog');
$this->success('登录成功','/admin');
}
/**
* 验证码
* @return mixed
*/
public function code()
{
$config = [
// 验证码字体大小
'fontSize' => 30,
// 验证码位数
'length' => 3,
// 关闭验证码杂点
'useNoise' => false,
];
$captcha = new Captcha($config);
return $captcha->entry();
}
}六、注意事项
1、验证码使用compoer进行安装,在根目录(composer.json同级目录)运行命令:
composer require topthink/think-captcha2、自动写入创建和更新的时间戳字段,需要在database.php中开启,设置如下:
// 自动写入时间戳字段
'auto_timestamp' => true,3、相关静态文件未写入文章,请前往码云下载;