Action.php 5.0 KB

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