Action.php 4.5 KB

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