上一章,我们开发了网站设置功能,那么这一章我们开发什么功能呢?
这一章我们做后台登录功能和管理员功能, 后台登录效果如下
在Application\Admin\Controller控制器目录中添加LoginController.class.php文件
// 后台登录视图
public function index(){
$this->display();
}
// 检查登录
public function check(){
//接收数据
$data = I('post.');
if(!$data['username']){
return show(0, '用户名不能为空');
}
if(!$data['password']){
return show(0, '密码不能为空');
}
// 查找该用户是否存在
$info = D('Admin')->getAdminByUser($data['username'],$data['lang']);
if(!$info){
return show(0, '用户不存在');
}
// 数据库的密码和传递过来的密码是否一样
if($info['password'] !== getPasswordEncrypt($data['password'])){
return show(0, '密码错误');
}
// 安全认证码验证
if(trim($data['vertify']) !== C('ADMIN_CODE')){
return show(0, '安全认证码错误');
}
// 登录成功写入session
session('adminUser', $info);
return show(1,'登录成功');
}
// 退出登录
public function loginOut(){
session('adminUser',null);
$this->redirect('index');
}
在Application\Common\Model模型目录中添加AdminModel.class.php模型文件
注:AdminModel.class.php文件中是已经包括了管理员的增删改查的功能实现
下面说管理员的功能就不贴代码了。
<?php
/**
* @Author: 穆沂华
* @Date: 2017-09-23 15:40:40
* @Last Modified by: 穆沂华
* @Last Modified time: 2017-09-23 15:40:40
*/
namespace Common\Model;
use Think\Model;
class AdminModel extends Model {
private $_db = '';
public function __construct(){
$this->_db = M('admin');
}
/**
* 通过用户查找该用户
* @param [type] $username [description]
* @return [type] [description]
*/
public function getAdminByUser($username,$lang){
$condition['username'] = $username;
$condition['lang'] = $lang;
$res = $this->_db->where($condition)->find();
return $res;
}
/**
* 添加用户
* @param array $data 数据
* @return boolen
*/
public function addInfo($data=array()){
if(!$data || !is_array($data)){
throw_exception('数据有误');
}
$res = $this->_db->add($data);
return $res;
}
/**
* 获取所有用户名
* @param $lang
* @return mixed
*/
public function getAll($lang){
$condition['lang'] = $lang;
$res = $this->_db->where($condition)->select();
return $res;
}
/**
* 单条数据
* @param $where
* @return mixed
*/
public function getOne($where){
$res = $this->_db->where($where)->find();
return $res;
}
/**
* 更新
* @param array $data
* @param $where
* @return bool
*/
public function editAdmin($data=array(), $where){
if(!$data || !is_array($data)){
throw_exception('数据有误');
}
$res = $this->_db->where($where)->setField($data);
return $res;
}
/**
* 删除
* @param $where
* @return mixed
*/
public function delAdmin($where){
$res = $this->_db->where($where)->delete();
return $res;
}
}
在此,登录和退出的功能已经实现
下面我们来开发管理员功能
管理员列表效果图如下:
代码的实现:
在Application\Admin\Controller\添加AdminController.class.php文件;代码已经在上面实现登录的时候就贴出来,请认真看以上代码
管理员添加,效果图如下:
管理员添加和修改页面是一样的,修改的页面根据添加效果图即可,所有的逻辑代码以上已经有,请认真看清楚。
注:这一章的后台登录,退出,管理员模块的功能已经实现,期待下一章~~~~