Action.php 4.5 KB

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