Action.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\bug\controller;
  4. use app\bug\model\ActionList;use think\App;use think\facade\Validate;
  5. class Action extends Base
  6. {
  7. protected $noLogin=['ActionList'];
  8. public function __construct(App $app)
  9. {
  10. parent::__construct($app);
  11. $this->model = new \app\bug\model\Action();
  12. }
  13. /** 获取页面id下功能按钮
  14. */
  15. public function ActionList(){
  16. $pageid =$this->request->post("id/d");
  17. if($pageid=="")return error('页面id不能为空');
  18. $condition = ['menuid'=>$pageid];
  19. $data=$this->model->with(["actionInfo"])->where($condition)->select();
  20. return success("获取成功",$data);
  21. }
  22. /** 新建功能按钮
  23. * @return \think\Response|\think\response\Json|void
  24. */
  25. public function ActionSave(){
  26. $post =$this->request->param(["id","action_name"=>"","action_desc"=>"","status"=>0,"action_code"=>""],
  27. "post","trim");
  28. $valid = Validate::rule([
  29. "id|主键Id"=>"number|gt:0",
  30. "action_name|功能名称"=>"require|max:255|unique:action_list,action_name,{$post['id']}",
  31. "status|功能状态"=>"number|in:0,1",
  32. "action_code|功能编号"=>"require|max:255",
  33. "action_desc|功能描述"=>"max:255",
  34. ]);
  35. if($valid->check($post)==false)return error($valid->getError());
  36. $isf= ActionList::where('id','=',$post['id'])->findOrEmpty();
  37. if($post['id']>0 && $isf->isEmpty())return error('未找到数据');
  38. try{
  39. $isf->save($post);
  40. return success("功能添加成功");
  41. }catch (\Exception $e){
  42. return error($e->getMessage());
  43. }
  44. }
  45. /**
  46. * 页面功能启禁用
  47. * @return \think\Response|\think\response\Json|void
  48. */
  49. public function ActionStatus(){
  50. $post =$this->request->param(['id'=>0,'status'=>0],'post','trim');
  51. $valid = Validate::rule([
  52. 'id|主键Id'=>'require|number|gt:0',
  53. 'status|功能状态'=>'number|in:0,1'
  54. ]);
  55. if($valid->check($post)==false)return error($valid->getError());
  56. $isf= $this->model->where('id','=',$post['id'])->findOrEmpty();
  57. if($isf->isEmpty())return error('未找到数据');
  58. try{
  59. $isf->status=$post['status'];
  60. $result=$isf->save();
  61. if($result==false) return error("{$this->model->status_cn[$isf->status]}失败");
  62. return success("{$this->model->status_cn[$isf->status]}成功");
  63. }catch (\Exception $e){
  64. return error($e->getMessage());
  65. }
  66. }
  67. /**页面功能添加
  68. * @return \think\response\Json|void
  69. * @throws \think\exception\DbException
  70. */
  71. public function ActionAdd(){
  72. $post =$this->request->param(['menuid'=>0,'status'=>1,"action_code"=>""],'post','trim');
  73. $valid = Validate::rule([
  74. 'menuid|菜单Id'=>'require|number|gt:0',
  75. 'action_code|功能编号'=>'require|number|gt:0',
  76. 'status|功能状态'=>'number|in:0,1'
  77. ]);
  78. if($valid->check($post)==false)return error($valid->getError());
  79. $isf = $this->model->where( ['menuid'=>$post['menuid'],'action_code'=>$post['action_code']])->findOrEmpty();
  80. if($isf->isEmpty()==false) return error('此功能已存在');
  81. try{
  82. $result=$isf->save($post);
  83. if($result==false)return error("创建失败");
  84. return success("创建成功");
  85. }catch (\Exception $e){
  86. return error($e->getMessage());
  87. }
  88. }
  89. /**
  90. * @return \think\response\Json
  91. * @throws \think\db\exception\DataNotFoundException
  92. * @throws \think\db\exception\DbException
  93. * @throws \think\db\exception\ModelNotFoundException
  94. * @throws \think\exception\DbException
  95. */
  96. public function index(){
  97. $post =$this->request->post();
  98. $data = Db::name("admin_menu")->where(["pid"=>0,"status"=>1,"is_del"=>0])->select();
  99. $result = [];
  100. if(empty($data)){
  101. return app_show(0,"获取成功",$result);
  102. }
  103. foreach ($data as $key=>$val){
  104. $val["child"]=[];
  105. $result[$val['id']] =$val;
  106. }
  107. $child =Db::name("admin_menu")->where([["pid","<>",0],['status',"=",1],["is_del","=",0]])->select();
  108. foreach ($child as $k=>$value){
  109. // $act = PasAction::all(['menuid'=>$value['id'],"status"=>1]);
  110. $act =Db::name("action")->alias("a")->leftJoin("action_list l","a.action_code=l.action_code")->field
  111. ("a.*,action_name")->where(['a.menuid'=>$value['id'],"a.status"=>1])->select();
  112. $act_data = Db::name("action_field")->where(['menuid'=>$value['id'],"status"=>1])->select();
  113. $value['action'] = $act;
  114. $value['action_data'] = $act_data;
  115. if(array_key_exists($value['pid'],$result)){
  116. $result[$value['pid']]["child"][]=$value;
  117. }
  118. }
  119. return app_show(0,"获取成功",array_values($result));
  120. }
  121. }