从头到尾实现一个用TP开发的个人站点【四】

上一章,我们开发了网站设置功能,那么这一章我们开发什么功能呢?

这一章我们做后台登录功能和管理员功能, 后台登录效果如下

这里写图片描述

在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文件;代码已经在上面实现登录的时候就贴出来,请认真看以上代码

管理员添加,效果图如下:
这里写图片描述

管理员添加和修改页面是一样的,修改的页面根据添加效果图即可,所有的逻辑代码以上已经有,请认真看清楚。

注:这一章的后台登录,退出,管理员模块的功能已经实现,期待下一章~~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

woody_deng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值