前端到後台ThinkPHP開發整站(4)

來源:互聯網
上載者:User

標籤:ace admin   目的   array   status   php   list   eric   sel   前台   

  今晚繼續我的這個項目的開發,今晚也是寫的不多,主要寫了一個菜單管理功能的CURD方法,前端介面還沒有進行編寫。

  菜單管理Model層的代碼:

<?phpnamespace 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();}}?>

    菜單管理控制器類的代碼:

<?phpnamespace 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

前端到後台ThinkPHP開發整站(4)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.