123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- <?php
- declare (strict_types = 1);
- namespace app\admin\controller;
- use think\App;
- use think\facade\Db;
- use think\facade\Validate;
- class Menu extends Base{
- public function __construct(App $app) {
- parent::__construct($app);
- }
- /** 进入页面获取权限列表
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function MenuList()
- {
- $condition = ["status" => 1, "is_del" => 0];
- $level = $this->request->post('level/d',$this->level,'trim');
- if ($level == '') return app_show(10000, "账户角色账户已禁用", []);
- if ($level != 1) {
- $role = Db::name("role_action")->where("role_id", "=", $this->roleid)->json(['action_conllect'])->find();
- if ($role == false) {
- return app_show(0, "获取成功", []);
- }
- $action = Db::name("action")->where(['id' => $role['action_conllect'], "status" => 1, "is_del" => 0])->column("id,menuid,action_code");
- if (empty($action)) return app_show(0, "获取成功", []);
- $MenuAction = [];
- foreach ($action as $value) {
- $MenuAction[$value['menuid']][] = $value['action_code'];
- }
- $menuid = array_column($action, "menuid");
- $condition['id'] = $menuid;
- $condition['level'] = [0,$level];
- } else {
- $action = Db::name("action")->where(["status" => 1, "is_del" => 0])->column("id,action_code,menuid");
- $MenuAction = [];
- foreach ($action as $value) {
- $MenuAction[$value['menuid']][] = $value['action_code'];
- }
- $menuid = array_column($action, "menuid");
- $condition['id'] = $menuid;
- $condition['level'] = [0,$level,2];
- }
-
- $menuAll = Db::name("admin_menu")
- ->where($condition)
- ->column("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight");
- $list = [];
- foreach ($menuAll as $value) {
- $value['action'] = $MenuAction[$value['id']] ?? [];
- makeMenu($value, $list);
- }
- $keys = array_column($list, 'weight');
- array_multisort($keys, SORT_DESC, $list);
- $list = MenuTree($list, 0);
- return app_show(0, "获取成功", $list);
- }
- /**菜单设置列表
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function MenuAllList(){
- $role = Db::name("role_action")->where("role_id","=",$this->roleid)->find();
- if($role==false){
- return app_show(0,"获取成功",[]);
- }
- $data = Db::name("admin_menu")
- ->where([["is_del", "=", 0]])
- ->field("id,menu_name,menu_img,menu_route,menu_url,pid,is_show,is_private,menu_type,level,status,weight")
- ->order("weight desc,id asc")
- ->select()
- ->toArray();
- $l = MenuTree($data, 0);
- return app_show(0, "获取成功", $l);
- }
- /**
- * 菜单新建
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function MenuAdd(){
- $post =$this->request->only([
- "menu_name"=>"",
- "menu_url"=>"",
- "menu_route"=>"",
- "menu_code"=>"",
- "menu_img"=>"",
- "pid"=>"0",
- "private"=>"0",
- "weight"=>"1",
- "menu_type"=>"1",
- "level"=>"",
- ],"post","trim");
- $valite =Validate::rule([
- "menu_name|菜单名称"=>"require|max:255",
- "menu_url|菜单链接"=>"max:255",
- "menu_route|菜单路由"=>"max:255",
- "menu_code|菜单编号"=>"max:255",
- "menu_img|菜单图标"=>"require|max:255",
- "pid|父级ID"=>"require|number",
- "private|是否私有"=>"require|number|in:0,1",
- "weight|权重"=>"require|number",
- "menu_type|菜单类型"=>"require|number|in:1,2",
- "level|菜单级别" => "requireIf:menu_type,2|in:0,1,2,3",
- ]);
- if($valite->check($post)==false)return error_show(1004,$valite->getError());
- if($post['pid']!=0 && $post['menu_route']==""){
- return error_show(1002,"子级菜单路由不能为空");
- }
- $data=[
- "menu_name"=>$post['menu_name'],
- "menu_url"=>$post['menu_url'],
- "menu_route"=>$post['menu_route'],
- "menu_code"=>$post['menu_code'],
- "menu_img"=>$post['menu_img'],
- "pid"=>$post['pid'],
- "weight"=>$post['weight'],
- "is_show"=>1,
- "is_private"=>$post['private'],
- "menu_type"=>$post['menu_type'],
- "level" => $post['menu_type'] == 2 ? $post['level'] : 0,
- "status"=>1,
- "addtime"=>date("Y-m-d H:i:s"),
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $result = Db::name("admin_menu")->insert($data);
- return $result ? app_show(0,"添加成功"): error_show(1003,"添加失败");
- }
- /**菜单编辑或新建
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function MenuEdit(){
- $post =$this->request->only([
- "id"=>"",
- "menu_name"=>"",
- "menu_url"=>"",
- "menu_route"=>"",
- "menu_code"=>"",
- "menu_img"=>"",
- "pid"=>"0",
- "private"=>"0",
- "weight"=>"1",
- "menu_type"=>"1",
- "level"=>"",
- ],"post","trim");
- $valite =Validate::rule([
- "menu_name|菜单名称"=>"require|max:255",
- "menu_url|菜单链接"=>"max:255",
- "menu_route|菜单路由"=>"max:255",
- "menu_code|菜单编号"=>"max:255",
- "menu_img|菜单图标"=>"require|max:255",
- "pid|父级ID"=>"require|number",
- "private|是否私有"=>"require|number|in:0,1",
- "weight|权重"=>"require|number",
- "menu_type|菜单类型"=>"require|number|in:1,2",
- "level|菜单级别" => "requireIf:menu_type,2|in:0,1,2,3",
- "id|菜单ID"=>"require|number|gt:0",
- ]);
- if($valite->check($post)==false)return error_show(1004,$valite->getError());
- if($post['pid']!=0 && $post['menu_route']==""){
- return error_show(1002,"子级菜单路由不能为空");
- }
- $menu = Db::name("admin_menu")->where([["id","=",$post['id']],["is_del","=",0]])->find();
- if($menu==false){
- return error_show(1003,"菜单不信息不存在");
- }
- if($post['id']!=0 && $post['menu_route']==""){
- return error_show(1002,"子级菜单路由不能为空");
- }
- $data=[
- "id"=>$post['id']??null,
- "menu_name"=>$post['menu_name'],
- "menu_url"=>$post['menu_url'],
- "menu_route"=>$post['menu_route'],
- "menu_code"=>$post['menu_code'],
- "menu_img"=>$post['menu_img'],
- "pid"=>$post['pid'],
- "weight"=>$post['weight'],
- "is_show"=>1,
- "is_private"=>$post['private'],
- "menu_type"=>$post['menu_type'],
- "level" => $post['menu_type'] == 2 ? $post['level'] : 0,
- "status"=>1,
- "updatetime"=>date("Y-m-d H:i:s"),
- ];
- $message="新建";
- if($post['id']!=""){
- $message="编辑";
- }
- $result = Db::name("admin_menu")->save($data);
- return $result ? app_show(0,"{$message}成功"): error_show(1003,"{$message}失败");
- }
- /**菜单状态更新
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function MenuStatus(){
- $post =$this->post;
- $id = isset($post['id']) ?intval($post['id']) :"";
- $menu = Db::name("admin_menu")->where("id","=",$id)->find();
- if($menu==false){
- return error_show(1003,"菜单信息不存在");
- }
- $statu = isset($post['status'])&&$post['status']!="" ? intval($post['status']) :"";
- if($statu===""){
- return error_show(1003,"菜单状态不能为空");
- }
- $menu['status']=$statu;
- $menu['updatetime']=date("Y-m-d H:i:s");
- $result = Db::name("admin_menu")->save($menu);
- return $result ? app_show(0,"状态更新成功"): error_show(1003,"状态更新失败");
- }
- //菜单删除
- public function MenuDel(){
- $post =$this->post;
- $id = isset($post['id']) ?intval($post['id']) :"";
- $menu = Db::name("admin_menu")->where([["id","=",$id],["is_del","=",0]])->find();
- if($menu==false){
- return error_show(1003,"菜单信息不存在");
- }
- $update =["is_del"=>1,"updatetime"=>date("Y-m-d H:i:s")];
- $result = Db::name("admin_menu")->where($menu)->update($update);
- return $result ? app_show(0,"删除成功"): error_show(1003,"删除失败");
- }
- /**
- * 获取菜单下的所有功能列表
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function ActionList(){
- $post =$this->post;
- $pageid = isset($post['id']) ? intval($post['id']) : "";
- if($pageid==""){
- return error_show(1001,'页面id不能为空');
- }
- $condition = ['menuid'=>$pageid,"a.is_del"=>0];
- $data=Db::name('action')->alias("a")->leftJoin("cfp_action_list l","a.action_code=l.action_code")->field
- ("a.*,action_name")->where($condition)->select();
- return app_show(0,"获取成功",$data);
- }
- /** 菜单下功能信息修改* **/
- public function ActionSave(){
- $post =$this->post;
- $actid = isset($post['id']) ? intval($post['id']) : "";
- if($actid==""){
- return error_show(1001,'功能id不能为空');
- }
- $menuid = isset($post['menuid']) ? intval($post['menuid']) : "";
- if($menuid==""){
- return error_show(1001,'页面menuid不能为空');
- }
- $code = isset($post['action_code']) ? trim($post['action_code']) : "";
- $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
- if($action_name==''){
- return error_show(1001,'参数 action_name 不能为空');
- }
- $status = isset($post['status']) ? intval($post['status']) : 1;
- if($code==""){
- return error_show(1002,'功能code不能为空');
- }
- $istrue =Db::name("action")->where(['menuid'=>$menuid,"action_code"=>$code,"is_del"=>0])->find();
- if($istrue && $istrue['id']!=$actid){
- return error_show(1005,'此功能已存在');
- }
- Db::startTrans();
- try{
- $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
- if($actioninfo){
- $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
- }else{
- $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
- $update = Db::name("action_list")->insert($data);
- }
- if($update==false){
- Db::rollback();
- return error_show(1002,'功能新建失败');
- }
- $data = ['action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
- $result=Db::name("action")->where("id","=",$actid)->save($data);
- if($result){
- Db::commit();
- event('rolesave');
- return app_show(0,"更新成功");
- }else{
- Db::rollback();
- return error_show(1004,"更新失败");
- }
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1003,$e->getMessage());
- }
- }
- /** 菜单下功能信息状态修改
- * @return \think\response\Json|void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- * @throws \think\exception\DbException
- */
- public function ActionStatus(){
- $post =$this->post;
- $actid = isset($post['id']) ? intval($post['id']) : "";
- if($actid==""){
- return error_show(1001,'功能id不能为空');
- }
- $status = isset($post['status']) ? intval($post['status']) : 1;
- try{
- $data = ['status'=>$status,"updatetime"=>date("Y-m-d H:i:s")];
- $result=Db::name("action")->where("id","=",$actid)->save($data);
- if($result){
- event('rolesave');
- return app_show(0,"更新成功");
- }else{
- return error_show(1004,"更新失败");
- }
- }catch (\Exception $e){
- return error_show(1003,$e->getMessage());
- }
- }
- /** 菜单下功能信息状态修改
- * @return \think\response\Json|void
- * @throws \think\exception\DbException
- */
- public function ActionAdd(){
- $post =$this->post;
- $pageid = isset($post['menuid']) ? intval($post['menuid']) : "";
- if($pageid==""){
- return error_show(1001,'页面id不能为空');
- }
- $code = isset($post['action_code']) ? trim($post['action_code']) : "";
- $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
- $status = isset($post['status']) ? intval($post['status']) : 1;
- if($code==""){
- return error_show(1002,'功能code不能为空');
- }
- if($action_name==""){
- return error_show(1002,'功能 action_name 不能为空');
- }
- Db::startTrans();
- try{
- $actioninfo =Db::name("action_list")->where(["action_code"=>$code,"is_del"=>0])->find();
- if($actioninfo){
- $update =Db::name("action_list")->where($actioninfo)->update(["action_name"=>$action_name,"updatetime"=>date("Y-m-d H:i:s")]);
- }else{
- $data=["action_name"=>$action_name,"action_code"=>$code,"addtime"=>date("Y-m-d H:i:s"),"updatetime"=>date("Y-m-d H:i:s")];
- $update = Db::name("action_list")->insert($data);
- }
- if($update==false){
- Db::rollback();
- return error_show(1002,'功能新建失败');
- }
- $where = ['menuid'=>$pageid,'action_code'=>$code,"is_del"=>0];
- $true =Db::name("action")->where($where)->find();
- if($true){
- Db::rollback();
- return error_show(1003,'此功能已存在');
- }else{
- $data = ['menuid'=>$pageid,'action_code'=>$code,'status'=>$status,"updatetime"=>date("Y-m-d H:i:s"),"addtime"=>date("Y-m-d H:i:s")];
- $in = Db::name("action")->insert($data);
- if($in){
- Db::commit();
- event("rolesave");
- return app_show(0,"添加成功");
- }
- }
- Db::rollback();
- return error_show(1005,'功能新建失败');
- }catch (\Exception $e){
- Db::rollback();
- return error_show(1005,$e->getMessage());
- }
- }
- //菜单下功能删除
- public function ActionDel(){
- $post =$this->post;
- $actid = isset($post['id']) ? intval($post['id']) : "";
- if($actid==""){
- return error_show(1001,'功能id不能为空');
- }
- $act=Db::name("action")->where([["id","=",$actid],["is_del","=",0]])->find();
- if($act==false){
- return error_show(1004,'未找到相关数据');
- }
- try{
- $data = ['is_del'=>1,"updatetime"=>date("Y-m-d H:i:s")];
- $result=Db::name("action")->where($act)->update($data);
- if($result){
- event('rolesave');
- return app_show(0,"删除成功");
- }else{
- return error_show(1004,"删除失败");
- }
- }catch (\Exception $e){
- return error_show(1003,$e->getMessage());
- }
- }
- }
|