基于ThinkPHP框架实现新闻管理发布系统

基于Think框架实现新闻管理发布系统

项目目标

通过新闻发布系统可以实现后台控制新闻类型管理、新闻管理、广告管理、评论管理的功能,以及后台管理员的登录;前台部分实现了用户的登录和注册,新闻标题栏的显示、新闻的内容页的显示以及新闻的分类显示,查询显示。在这里插入图片描述

设计要求

(1)实现用户、管理员的登录注册功能,登录加入验证码判断,用户可以的通过点击注册按钮跳入注册界面,注册界面录入用户的用户名、密码以及邮箱
(2)实现新闻类型的增删改查、新闻的增删改查、评论的增删改查、广告的正删改查。
(3)用户使用界面中首页显示,按关键字查询的功能,新闻页面的分类显示,新闻内容页实现评论及评论显示功能。

详细

后台部分

登录模块
代码:

<?php
class LoginAction extends Action{
    public function login() {
        $this->display();
    }
    public function doLogin() {
        if(!$this->isPost()){
            halt("页面不正确");
        }
        $validate = md5($_POST['validate']);
        if($_SESSION['verify']!=$validate){
            $this->error("验证码不正确");
        }
        $u_name = $_POST['u_name'];
        $where['u_name'] = $_POST['u_name'];
        $u_password=$_POST['u_password'];
        $user = M('user');
        $message = $user->where($where)->find();
        if(!$u_name||$message['u_password']!=$u_password){
            $this->error('用户名或者账号不正确');
        }else{
            $_SESSION['u_name'] = $message['u_name'];
            $_SESSION['u_id'] = $message['u_id'];
            redirect(__APP__);
        }
    }
    Public function verify() {
        import('ORG.Util.Image');
        Image::buildImageVerify();
    }
    
    Public function loginOut() {
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),time()-1,'/');
        }
        session_destroy();
        $this->redirect('Login/login');
    }
}

图示
在这里插入图片描述
后台主界面

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action
{
    public function index(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        $this->assign('u_name',$_SESSION['u_name']);
        $this->display();
    
    }
    Public function sysMessage(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        $this->systemMsg = systemMsg();
        $this->display();
         
    }
}

图示
在这里插入图片描述
分类列表界面

<?php
class TypeAction extends Action {
    Public function typeList(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        
        $type=M('type');
        $count=$type->count('t_id');
       
    
        import('ORG.Util.Page');
        $page=new page($count,7);
        $limit=$page->firstRow.','.$page->listRows;
        $list=$type->order('t_order ASC')->limit($limit)->select();
        $this->page=$page->show();
        $this->assign('list',$list);
	    $this->display();
    }
    Public function addType(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        $this->display();
    }


    Public function updateType(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        if(!isset($_GET['t_id'])){
            $this->error('非法操作');
        }
        $type=M('type');
        $where['t_id']=$_GET['t_id'];
        $list=$type->where($where)->find();
        $this->assign('list',$list);
        $this->display();
    }

    Public function update(){
        if(!isset($_POST['t_id'])){
            $this->error('非法操作');
        }
        if(empty($_POST['t_name'])){
            $this->error('分类名称不能为空');
        }
        $type=M('type');
        $type->create();
        $where['t_id']=$_POST['t_id'];
        if($type->where($where)->save()){ 
            $this->success('修改成功', U('typeList'));
        }
        else{
            $this->error('修改失败');
        }
    }
    

    Public function delete(){
        if(!isset($_SESSION['u_id'])){
            $this->redirect('Login/login');
        }
        if(!isset($_GET['t_id'])){
            $this->error('非法操作');
        }
        $type=M('type');
        $where['t_id']=$_GET['t_id'];
        if($type->where($where)->delete()){      
            $this->success('删除成功', U('typeList'));
        }
        else{
            $this->error('删除失败');
        }
    }     
}

在这里插入图片描述
相应的新闻、广告、评论同分类类似
在这里插入图片描述
在这里插入图片描述

前端部分

前端用户登录

<?php
//Login登录模块
class LoginAction extends Action {
    //登录页面
    public function login(){
	    $this->display();
    }
    //登录过程
    public function doLogin(){
       // var_dump($_POST);
        $c_name=$_POST['c_name'];
        $where['c_name']=$_POST['c_name'];
        

        $c_password=$_POST['c_password'];    
        $customer=M('customer');  
        
        $message=$customer->where($where)->find();       
        if(!$message || $message['c_password']!=$c_password){
            $this->error('用户名或者账号不正确');
        }
        else{
            $_SESSION['c_name']=$message['c_name'];
            $_SESSION['c_id']=$message['c_id'];  
            $this->success("恭喜您,登陆成功",U('Index/index'));
        }
    }
     
 
    //退出登录
    Public function loginOut(){
        $_SESSION = array(); //清除SESSION值.
        if(isset($_COOKIE[session_name()])){  //判断客户端的cookie文件是否存在,存在的话将其设置为过期.
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();  //清除服务器的sesion文件
        $this->redirect('Index/index');
    }
    
}

在这里插入图片描述
用户注册

<?php
//注册模块
class RegisterAction extends Action {

    //注册页面
    Public function register(){
        $this->display();
    }
    //注册过程
    Public function doRegister(){
        if(empty($_POST['c_name'])){
            $this->error('注册失败!');
        }
        
        $customer=M('customer');      
        /* $data['c_name']=$_POST['c_name'];
        $data['c_password']=md5($_POST['c_password']);
        $data['c_register_time']=time();  
        $data['c_status']='1';  
        $data['c_email']=$_POST['c_email'];   */
        
        $data=$customer->create();
        $data['c_status']='1';
        $data['c_register_time']=time();
        
         
        if($lastid=$customer->add($data)){
            $this->success('注册成功', U('Login/login'));
        }else{
            $this->error('注册失败!');
        }       
    }
    
     
    Public function checkName(){
        $where['c_name']=$_POST['c_name'];
        $customer=M('customer');
        $id=$customer->where($where)->getField('c_id');
        if(isset($id)){
           $data['result']=0;
           echo json_encode($data);exit;
        }else{
           $data['result']=1;
           echo json_encode($data);exit;
        }
    }    
}

在这里插入图片描述
主界面部分

<?php
// 本类由系统自动生成,仅供测试用途
class IndexAction extends Action {
	//新闻首页
	Public function index(){
	    //类别
		$type=M('type');
		$this->t_list=$type->where('t_status=1')->order('t_order ASC')->select();//新闻分类导航栏
		//新闻
		$news=M('news');
		$this->t_list1=$news->where('n_type=5 and n_status=1 ')->order('n_addtime desc , n_nums desc')->limit(15)->select();
		$this->t_list2=$news->where('n_type=6 and n_status=1 ')->order('n_addtime desc , n_nums desc')->limit(15)->select();
		$this->t_list3=$news->where('n_type=7 and n_status=1 ')->order('n_addtime desc , n_nums desc')->limit(15)->select();
		$this->t_list4=$news->where('n_type=8 and n_status=1 ')->order('n_addtime desc , n_nums desc')->limit(15)->select();
		$this->assign('c_name',$_SESSION['c_name']);
		/* 友情链接 开始*/
		$ad=M('ad');
		$link_where['a_position']=3;
		$link_where['a_status']=1;
		$order='a_order ASC';
		$this->link=$ad->where($link_where)->order($order)->select();
		/*友情链接 结束*/
		$left_where['a_position']=1;
		$left_where['a_status']=1;
		$this->left=$ad->where($left_where)->order($order)->limit(1)->select();
		
		$right_where['a_position']=2;
		$right_where['a_status']=1;
		$this->right=$ad->where($right_where)->order($order)->limit(1)->select();
		$this->display();
	}

在这里插入图片描述
关键词查询部分

Public function select(){
		/*新闻分类导航栏 开始*/
		$type=M('type');
		$this->list_name=$type->where("t_id ='". $_GET['t_id']."'")->getField('t_name');
		$this->t_list=$type->where('t_status=1')->order('t_order ASC')->select();	
		/*获取新闻内容 结束*/

		/*获取关键字相关新闻 开始*/
		$keyword=(isset($_POST['keyword']))?$_POST['keyword']:$_GET['keyword'];
		$news=M('news');		
 		$count=$news->where("n_status=1 and n_title like '%".$keyword."%'")->count('n_id');
        import('ORG.Util.Page');//导入分页类
        $page=new page($count,10);
        $limit=$page->firstRow.','.$page->listRows;
        $this->n_list=$news->where("n_status=1 and n_title like '%".$keyword."%'")
        ->order('n_addtime desc , n_nums desc')->limit($limit)->select();
        $this->page=$page->show();	
        /*获取关键字相关新闻 结束*/
        
        /* 友情链接 开始*/
        $ad=M('ad');
        $where['a_position']=4;
        $where['a_status']=1;
        $order='a_order ASC';
        $this->link=$ad->where($where)->order($order)->select();
        /*友情链接 结束*/
        
        $this->assign('keyword',$keyword);
        $this->assign('count',$count);
		$this->display();
	}
	
}
?>

在这里插入图片描述
新闻评论功能实现

//新闻详情页面
	Public function message(){
		/*新闻分类导航栏 开始*/
		$type=M('type');
		$this->t_list=$type->where('t_status=1')->order('t_order ASC')->select();
		/*新闻分类导航栏 结束*/
		
		/*获取新闻内容 开始*/
		$where['n_id']=$_GET['n_id'];
		$news=M('news');
		$this->news=$news->where($where)->find();
		$news->where($where)->setInc('n_nums');   //新闻浏览次数+1
		/*获取新闻内容 结束*/
	
		/*当前位置导航 开始*/
		$n_type=$news->where($where)->getField('n_type');
		$n_author=$news->where($where)->getField('n_author');
		$this->type_id=$n_type;
		$this->type_name=$type->where("t_id = '" . $n_type ."'")->getField('t_name');
		/*当前位置导航 结束*/

		/*新闻评论 开始*/
		$user=M('user');
		$this->author=$user->where("u_id = '" . $n_author ."'")->getField('u_name');
		$comment=D('CommentView');		
		$count=$comment->where("co_news_id ='".$_GET['n_id']."' and co_status=1")->order('co_addtime DESC')->count('co_id');
		import('ORG.Util.Page');    //导入分页类
        $page=new page($count,10);
        $limit=$page->firstRow.','.$page->listRows;
		$c_list=$comment->where("co_news_id ='".$_GET['n_id']."' and co_status=1")->order('co_addtime DESC')->limit($limit)->select();
		/*新闻评论 结束*/
		
		/* 友情链接 开始*/
		$ad=M('ad');
		$where['a_position']=4;
		$where['a_status']=1;
		$order='a_order ASC';
		$this->link=$ad->where($where)->order($order)->select();
		/*友情链接 结束*/

		//以下为渲染模板输出
		$this->page=$page->show();	
		$this->assign('c_list',$c_list);
		$this->assign('c_name',$_SESSION['c_name']);
		$this->display();
	}

	//异步发布评论功能
	Public function addComment(){
		$data['co_news_id']		=	$_POST['n_id'];
		$data['co_message']		=	$_POST['co_message'];
		$data['co_customer_id']	=	$_SESSION['c_id'];
		$data['co_status']		=	'1';
		$data['co_addtime']		=	time();
		$comment=M('comment');
		if($lastid=$comment->add($data)){
			echo $lastid;
		}

	}

在这里插入图片描述
数据库部分
在这里插入图片描述

源码链接:https://github.com/lzlv587/Demo

发布了73 篇原创文章 · 获赞 20 · 访问量 4448

猜你喜欢

转载自blog.csdn.net/lzl980111/article/details/103845987