Action.php 4.6 KB

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