免費個人部落格開發-編寫登錄功能
- 2019 年 12 月 27 日
- 筆記
本節我們將實現以下功能:創建並連接資料庫、創建運營表、創建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 <[email protected]> // +---------------------------------------------------------------------- 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 appcommonmodel; use thinkModel; class User extends Model { /** * 數據分頁列表 * @param $where * @param string $field * @param string $order * @param int $pageNum * @return thinkPaginator * @throws thinkexceptionDbException */ 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|thinkCollection * @throws thinkdbexceptionDataNotFoundException * @throws thinkdbexceptionModelNotFoundException * @throws thinkexceptionDbException */ 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 thinkdbexceptionDataNotFoundException * @throws thinkdbexceptionModelNotFoundException * @throws thinkexceptionDbException */ 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 thinkException * @throws thinkexceptionPDOException */ 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 appadmincontroller; use appcommonmodelUser; use thinkcaptchaCaptcha; use thinkController; class Login extends Controller { protected $user_model; public function __construct() { parent::__construct(); $this->user_model = new User(); } /** * 登錄頁面 * @return thinkresponseView */ public function index() { return view(); } /** * 登錄處理 * @throws thinkdbexceptionDataNotFoundException * @throws thinkdbexceptionModelNotFoundException * @throws thinkexceptionDbException */ 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-captcha
2、自動寫入創建和更新的時間戳欄位,需要在database.php中開啟,設置如下:
// 自動寫入時間戳欄位 'auto_timestamp' => true,
3、相關靜態文件未寫入文章,請前往碼雲下載;

