Action.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;
  5. use think\App;
  6. use think\facade\Db;
  7. class Action extends BaseController{
  8. public function __construct(App $app) {parent::__construct($app);}
  9. //获取素有菜单列表数据
  10. public function index(){
  11. $post=$this->request->post();
  12. $where=[["status","=",1],["is_del","=",0],["level",">=",$post["level"]??1],['menu_type',"=",2]];
  13. $data = Db::name("admin_menu")->where($where)->order("weight desc")->column("id,menu_name,menu_img,menu_route,menu_url,pid,level,is_show,is_private,menu_type,status");
  14. $result = [];
  15. if(empty($data)){
  16. return app_show(0,"获取成功",$result);
  17. }
  18. $list=[];
  19. $menu=[];
  20. foreach ($data as $k=>$value){
  21. $action = Db::name("action")->alias("a")->leftJoin("action_list b","a.action_code=b.action_code")->where
  22. (["menuid"=>$value['id'],"a.status"=>1,"a.is_del"=>0,"b.is_del"=>0])->column("a.id,a.action_code,b.action_name");
  23. if($value['menu_type']==2) $value['action']=$action;
  24. $list[]=$value;
  25. }
  26. menuAction($list,$menu);
  27. return app_show(0,"获取成功",array_values($menu));
  28. }
  29. public function ActionList(){
  30. $post=$this->post;
  31. $page = isset($post['page']) ? intval($post['page']) : 1;
  32. $size = isset($post['size']) ? intval($post['size']) : 10;
  33. $count = Db::name("action_list")->where(["is_del"=>0])->count();
  34. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  35. $page = $page>=$total?intval($total):$page;
  36. $list = Db::name("action_list")->where(["is_del"=>0])->page($page,$size)->select();
  37. $data =['list'=>$list,"count"=>$count];
  38. return app_show(0,'获取成功',$data);
  39. }
  40. /** 菜单下功能信息状态修改
  41. * @return \think\response\Json|void
  42. * @throws \think\exception\DbException
  43. */
  44. public function ActionAdd(){
  45. $post =$this->post;
  46. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  47. if($action_name==""){
  48. return error_show(1001,'功能名称不能为空');
  49. }
  50. $action_desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  51. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  52. if($code==""){
  53. return error_show(1001,'功能代码不能为空');
  54. }
  55. $status = isset($post['status']) ? intval($post['status']) : 1;
  56. try{
  57. $action=[
  58. "action_name"=>$action_name,
  59. "action_code"=>$code,
  60. "action_desc"=>$action_desc,
  61. "status"=>$status,
  62. "is_show"=>1,
  63. "addtime"=>date("Y-m-d H:i:s"),
  64. "updatetime"=>date("Y-m-d H:i:s")
  65. ];
  66. $up =Db::name("action_list")->insert($action);
  67. return $up ? app_show(0,"新建成功") :error_show(1004,"新建失败");
  68. }catch (\Exception $e){
  69. return error_show(1005,$e->getMessage());
  70. }
  71. }
  72. /** 菜单下功能信息状态修改
  73. * @return \think\response\Json|void
  74. * @throws \think\exception\DbException
  75. */
  76. public function ActionDel(){
  77. $post =$this->post;
  78. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  79. if($action_id===""){
  80. return error_show(1001,'参数action_id不能为空');
  81. }
  82. $action =Db::name("action_list")->where(["id"=>$action_id,"is_del"=>0])->find();
  83. if($action==false){
  84. return error_show(1004,"未找到功能数据");
  85. }
  86. $upda=["is_del"=>0,"updatetime"=>date("Y-m-d H:i:s")];
  87. Db::startTrans();
  88. try {
  89. $up =Db::name("action_list")->where($action)->update($upda);
  90. if($up){
  91. $upall =Db::name("action")->where(["action_code"=>$action['action_code'],"is_del"=>0])->update($upda);
  92. Db::commit();
  93. return app_show(0,"删除成功");
  94. }
  95. Db::rollback();
  96. return error_show(1005,"删除失败");
  97. }catch (\Exception $e){
  98. Db::rollback();
  99. return error_show(1005,$e->getMessage());
  100. }
  101. }
  102. }