Action.php 4.2 KB

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