zoukankan      html  css  js  c++  java
  • 前端到后台ThinkPHP开发整站(4)

      今晚继续我的这个项目的开发,今晚也是写的不多,主要写了一个菜单管理功能的CURD方法,前端界面还没有进行编写。

      菜单管理Model层的代码:

    <?php
    namespace Common\Model;
    use Think\Model;
    
    class MenuModel extends Model{
    	private $_db='';
    	
    	public function __construct(){
    		$this->_db=M("menu");
    	}
    	
    	/**
    	 * 插入菜单数据
    	 */
    	public function insert($data=array()){
    		if(!data || !is_array($data)){
    			return 0;
    		}
    		
    		return $this->_db->add($data);
    	}
    	
    	/**
    	 * 获取菜单数据
    	 */
    	public function getMenus($data,$pageIndex,$pageSize=10){
    		$data['status']=array('neq',-1);
    		$offset=($pageIndex-1)*$pageSize;
    		$list=$this->_db->where($data)->order('listorder desc,menu_id desc')->limit($offset,$pageSize);
    		return $list;
    	}
    	
    	/**
    	 * 获取菜单总数
    	 */
    	public function getMenusCount($data=array()){
    		$data['status']=array('neq',-1);
    		return $this->_db->where($data)->count();
    	}
    	
    	/**
    	 * 根据ID获取菜单ID
    	 */
    	public function find($id){
    		if(!$id || !is_numeric($id)){
    			return array();
    		}
    		return $this->_db->where("menu_id={}$id")->find();
    	}
    	
    	/**
    	 * 根据ID更新菜单
    	 */
    	public function updateMenuById($id,$data){
    		if(!$id || !is_numeric($id)){
    			throw_exception("ID不合法");
    		}
    		
    		if(!$data || !is_array($data)){
    			throw_exception('更新的数据不合法');
    		}
    		
    		return $this->_db->where("menu_id={$id}")->save($data);
    	}
    	
    	/**
    	 * 更新排队序号
    	 */
    	public function updateMenuListOrderById($id,$listorder){
    		if(!$id || !is_numeric($id)){
    			throw_exception('ID不合法');
    		}
    		$data=array(
    			'listorder'=>intval($listorder);
    		);
    		
    		return $this->_db->where("menu_id={$id}")->save($data);
    	}
    	
    	/**
    	 * 获取后台菜单
    	 */
    	public function getAdminMenus(){
    		$data=array(
    			'status'=>array('neq',-1),
    			'type'=>1
    		);
    		
    		return $this->_db->where($data)->order('listorder desc,menu_id desc')->select();
    	}
    	
    	/**
    	 * 获取前台菜单
    	 */
    	public function getBarMenus(){
    		$data=array(
    			'status'=>1,
    			'type'=>0
    		);
    		return $this->_db->where($data)->order('listordre desc,menu_id desc')->select();
    	}
    }
    ?>
    

        菜单管理控制器类的代码:

    <?php
    namespace Admin\Controller;
    use Think\Controller;
    
    class MenuController extends  CommonController{
    	
    	public function index(){
    		$data=array();
    		if(isset($_REQUEST['type']) && in_array($_REQUEST, array(0,1))){
    			$data['type']=intval($_REQUEST['type']);
    			$this->assign('type',$data['type']);
    		}else{
    			$this->assign('type',-100);
    		}
    	}
    	
    	public function add(){
    		if($_POST){
    			if(!isset($_POST['name']) || !$_POST['name']){
    				return jsonResult(0, '菜单名不能为空');
    			}
    			if(!isset($_POST['m']) || !$_POST['m']){
    				return jsonResult(0, '模块名不能为空');
    			}
    			if(!isset($_POST['c']) || !$_POST['c']){
    				return jsonResult(0, '控制器不能为空');
    			}
    			if(!isset($_POST['f']) || !$_POST['f']){
    				return jsonResult(0, '方法名不能为空');
    			}
    			if($_POST['menu_id']){
    				return $this->save($_POST);
    			}
    			$menuId=D("Menu")->insert($_POST);
    			if($menuId){
    				return jsonResult(1, '新增成功', $menuId);
    			}
    			return jsonResult(0, '新增失败', $menuId);
    		}else{
    			$this->display();
    		}
    	}
    	
    	public function edit(){
    		$menuId=$_REQUEST['id'];
    		$menu=D("Menu")->find($menuId);
    		$this->assign('menu',$menu);
    		$this->display();		
    	}
    	
    	public function save($data){
    		$menuId=$data['menu_id'];
    		unset($data['menu_id']);
    		
    		try{
    			$id=D("Menu")->updateMenuById($menuid,$data);
    			if($id===FALSE){
    				return jsonResult(0, '保存失败');
    			}
    			return jsonResult(0,'保存成');
    		}catch(Exception $ex){
    			return jsonResult(0,$ex->getMessage());
    		}
    	}
    	
    	public function setStatus(){
    		try{
    			if($_POST){
    				$id=$_POST['id'];
    				$status=$_POST['status'];
    				$ret=D("Menu")->updateStatusById($id,$status);
    				if($ret){
    					return jsonResult(1,'操作成功');
    				}else{
    					return jsonResult(0,'操作失败');
    				}
    			}
    		}catch(Exception $ex){
    			return jsonResult(0,$ex->getMessage());
    		}
    		return jsonResult(0,'没有提交数据');
    	}
    	
    	/**
    	 * 数据排序
    	 */
    	public function listorder(){
    		$listoreder=$_POST['listorder'];
    		$data =array('jump_url'=> $_SERVER['HTTP_REFERER']);
    		$errors=array();
    		if($listoreder){
    			try{
    				foreach($listorder as $emnuId=>$v){
    					$id=D("Menu")->updateMenuListorderById($menuId,$v);
    					if($id===false){
    						$errors[]=$menuId;
    					}
    				}
    			}catch(Exception $ex){
    				return jsonResult(0, $ex->getMessage(), $data)
    			}
    			if($errors){
    				return jsonResult(0,"排序失败-".implode(',', $errors), $data);
    			}
    			return jsonResult(1, '排序成功', $data)
    		}
    		return jsonResult(0,'数据排序失败', $data);
    	}
    }
    ?>
    

      今晚就暂时写这么点,明晚开始做前端的开发,明天就周五了,如果周六不用加班,我会加大马力在这周内结束该项目的。(^_−)☆

    源码地址:https://github.com/YoZiLin/TP-CMS

  • 相关阅读:
    ROS+clion多节点调试
    argparse模块用法实例详解
    Python3中的bytes和str类型
    elk日志过滤文档
    centos7普通用户无法切换为root用户处理
    Hyper-V迁移方案
    中小互联网电商(电商)公司研发部门组织架构
    基于Redis实现令牌桶限流
    异步与协程
    C# 同步上下文及死锁
  • 原文地址:https://www.cnblogs.com/lzy138/p/7215380.html
Copyright © 2011-2022 走看看