Action.php 4.1 KB

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