🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
``` <?php /** * Created by PhpStorm. * User: admin * Date: 2018/11/29 0029 * Time: 11:46 */ namespace Wechat\Controller; use Common\Controller\AdminbaseController; use Think\Controller; class WechatMenuController extends AdminbaseController { public function index(){ $data = M('weixin_menu')->order('menu_id asc')->select(); $list = $this->getTree($data); $this->assign('list',$list); $this->display(); } public function add(){ if (IS_POST){ $menu_name = I('menu_name','','trim'); if (empty($menu_name)){ $this->ajaxReturn(array('code'=>'2','msg'=>'请输入分类名')); } $menu_type = I('menu_type','','trim'); if ($menu_type == 'view'){ $menu_url = I('menu_url'); if(empty($menu_url)){ $this->ajaxReturn(array('code'=>'2','msg'=>'请输入URL地址')); } } if ($menu_type == 'click'){ $menu_key = I('menu_key'); if(empty($menu_key)){ $this->ajaxReturn(array('code'=>'3','msg'=>'请输入关键词')); } } $menu_pid = I('menu_pid'); $count = M('weixin_menu')->where(array('menu_pid'=>$menu_pid))->count(); if ($menu_pid == 0 && $count >= 3){ $this->ajaxReturn(array('code'=>'3','msg'=>'只能添加 3 条一级菜单')); } if ($menu_pid != 0 && $count >= 5){ $this->ajaxReturn(array('code'=>'3','msg'=>'只能添加 5 条子级菜单')); } // if ($menu_pid != 0){ // $this->ajaxReturn(array('code'=>'3','msg'=>'只能添加二级菜单')); // } $model = M("weixin_menu"); $data = $model->create(); if ($data) { $data['menu_pid']=$menu_pid; $data['menu_name']=$menu_name; $data['menu_type']=$menu_type; $data['menu_url'] =$menu_url; $data['menu_key']=$menu_key; } if ($model->add($data)) { $this->ajaxReturn(array('code' => '1', 'msg' => '添加成功')); } else { $this->ajaxReturn(array('code' => '0', 'msg' => '添加失败')); } }else{ $data = M('weixin_menu')->order('menu_order desc')->select(); $menus = $this->getTree($data); $this->assign('menus',$menus); $this->display(); } } public function edit(){ if (IS_POST){ $menu_pid = I('menu_pid'); $menu_name = I('menu_name','','trim'); if (empty($menu_name)){ $this->ajaxReturn(array('code'=>'1','msg'=>'请输入分类名')); } $menu_type = I('menu_type','','trim'); $menu_url = I('menu_url'); if ($menu_type == $menu_url){ if(empty($menu_url)){ $this->ajaxReturn(array('code'=>'2','msg'=>'请输入URL地址')); } } $menu_key = I('menu_key','','trim'); if ($menu_type == $menu_key){ if(empty($menu_key)){ $this->ajaxReturn(array('code'=>'3','msg'=>'请输入关键词')); } } $id = I('id'); $model = M("weixin_menu"); $data = $model->create(); if ($data) { $data['menu_id']=$id; $data['menu_pid']=$menu_pid; $data['menu_name']=$menu_name; $data['menu_type']=$menu_type; if ($data['menu_url']) $data['menu_url'] =$menu_url; if ($data['menu_key']) $data['menu_key']=$menu_key; } $res = $model->save($data); if ($res !== false) { $this->ajaxReturn(array("code" => '1', 'msg' => '修改成功')); } else { $this->ajaxReturn(array("code" => '1', 'msg' => '修改失败')); } }else{ $id = I('id'); $info = M('weixin_menu')->where(array('menu_id'=>$id))->find(); $data = M('weixin_menu')->order('menu_order desc')->select(); $menus = $this->getTree($data); $this->assign('menus',$menus); $this->assign('info',$info); $this->display(); } } public function delete(){ $id = I('id'); $info = M('weixin_menu')->where(array('menu_id'=>$id))->getField(); $data = M('weixin_menu')->where(array('menu_pid'=>$info))->select(); if (!empty($data)){ $this->ajaxReturn(array("code" => '1', 'msg' => '请先删除子分类')); }else{ $del = M('weixin_menu')->where(array('menu_id'=>$id))->delete(); if ($del){ $this->ajaxReturn(array("code" => '2', 'msg' => '删除成功')); }else{ $this->ajaxReturn(array("code" => '3', 'msg' => '删除失败')); } } } public function getTree($data,$parent_id=0,$level=0){ static $arr=array(); foreach($data as $key=>$value){ if($value['menu_pid'] == $parent_id){ $value['level']=$level; $arr[] = $value; $this->getTree($data,$value['menu_id'],$level+1); } } return $arr; } } ```