专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > php

前端到后台老板ThinkPHP开发整站(4)

发布时间:2010-05-20 14:01:29 文章来源:www.iduyao.cn 采编人员:星星草
前端到后台ThinkPHP开发整站(4)

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

  菜单管理Model层的代码:

<?php
namespace CommonModel;
use ThinkModel;

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 AdminController;
use ThinkController;

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

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: