Action.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. return app_show(0, "获取成功", array_values($menu));
  35. }
  36. public function ActionList(){
  37. $post=$this->post;
  38. $page = isset($post['page']) ? intval($post['page']) : 1;
  39. $size = isset($post['size']) ? intval($post['size']) : 10;
  40. $count = Db::name("action_list")->where(["is_del"=>0])->count();
  41. $total = ceil($count/$size)>1 ? ceil($count/$size) : 1;
  42. $page = $page>=$total?intval($total):$page;
  43. $list = Db::name("action_list")->where(["is_del"=>0])->page($page,$size)->select();
  44. $data =['list'=>$list,"count"=>$count];
  45. return app_show(0,'获取成功',$data);
  46. }
  47. /** 菜单下功能信息状态修改
  48. * @return \think\response\Json|void
  49. * @throws \think\exception\DbException
  50. */
  51. public function ActionAdd(){
  52. $post =$this->post;
  53. $action_name = isset($post['action_name']) ? trim($post['action_name']) : "";
  54. if($action_name==""){
  55. return error_show(1001,'功能名称不能为空');
  56. }
  57. $action_desc = isset($post['action_desc']) ? trim($post['action_desc']) : "";
  58. $code = isset($post['action_code']) ? trim($post['action_code']) : "";
  59. if($code==""){
  60. return error_show(1001,'功能代码不能为空');
  61. }
  62. $status = isset($post['status']) ? intval($post['status']) : 1;
  63. try{
  64. $action=[
  65. "action_name"=>$action_name,
  66. "action_code"=>$code,
  67. "action_desc"=>$action_desc,
  68. "status"=>$status,
  69. "is_show"=>1,
  70. "addtime"=>date("Y-m-d H:i:s"),
  71. "updatetime"=>date("Y-m-d H:i:s")
  72. ];
  73. $up =Db::name("action_list")->insert($action);
  74. return $up ? app_show(0,"新建成功") :error_show(1004,"新建失败");
  75. }catch (\Exception $e){
  76. return error_show(1005,$e->getMessage());
  77. }
  78. }
  79. /** 菜单下功能信息状态修改
  80. * @return \think\response\Json|void
  81. * @throws \think\exception\DbException
  82. */
  83. public function ActionDel(){
  84. $post =$this->post;
  85. $action_id = isset($post['action_id']) ? intval($post['action_id']) : "";
  86. if($action_id===""){
  87. return error_show(1001,'参数action_id不能为空');
  88. }
  89. $action =Db::name("action_list")->where(["id"=>$action_id,"is_del"=>0])->find();
  90. if($action==false){
  91. return error_show(1004,"未找到功能数据");
  92. }
  93. $upda=["is_del"=>0,"updatetime"=>date("Y-m-d H:i:s")];
  94. Db::startTrans();
  95. try {
  96. $up =Db::name("action_list")->where($action)->update($upda);
  97. if($up){
  98. $upall =Db::name("action")->where(["action_code"=>$action['action_code'],"is_del"=>0])->update($upda);
  99. Db::commit();
  100. return app_show(0,"删除成功");
  101. }
  102. Db::rollback();
  103. return error_show(1005,"删除失败");
  104. }catch (\Exception $e){
  105. Db::rollback();
  106. return error_show(1005,$e->getMessage());
  107. }
  108. }
  109. }