Rule.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\controller\auth;
  12. use app\common\controller\Backend;
  13. use app\admin\traits\Actions;
  14. use think\annotation\route\Route;
  15. use think\annotation\route\Group;
  16. use app\common\model\AuthRule;
  17. use think\facade\Cache;
  18. use think\facade\Db;
  19. /**
  20. * 规则管理
  21. */
  22. #[Group("auth/rule")]
  23. class Rule extends Backend
  24. {
  25. use Actions{
  26. add as private _add;
  27. edit as private _edit;
  28. }
  29. protected function _initialize()
  30. {
  31. parent::_initialize();
  32. $this->model = new AuthRule();
  33. Cache::delete('admin_rule_list');
  34. Cache::delete('admin_menu_list');
  35. }
  36. #[Route('GET,JSON','index')]
  37. public function index()
  38. {
  39. if (false === $this->request->isAjax()) {
  40. return $this->fetch();
  41. }
  42. $tree=AuthRule::getRuleListTree('*');
  43. $result = ['total' => 1000, 'rows' => $tree];
  44. return json($result);
  45. }
  46. #[Route('GET,POST','add')]
  47. public function add()
  48. {
  49. $this->beforeAction();
  50. return $this->_add();
  51. }
  52. #[Route('GET,POST','edit')]
  53. public function edit()
  54. {
  55. $this->beforeAction();
  56. return $this->_edit();
  57. }
  58. #[Route('GET,POST','del')]
  59. public function del()
  60. {
  61. $ids = $this->request->param("ids");
  62. $list = $this->model->where('id', 'in', $ids)->select();
  63. foreach ($list as $item) {
  64. $ins=AuthRule::where(['pid'=>$item->id,'ismenu'=>1])->count();
  65. if($ins>0){
  66. $this->error(__('请先删除【%s】的子菜单',['s'=>$item->title]));
  67. }
  68. }
  69. $count = 0;
  70. Db::startTrans();
  71. try {
  72. foreach ($list as $item) {
  73. AuthRule::where(['pid'=>$item->id])->delete();
  74. $count += $item->delete();
  75. }
  76. Db::commit();
  77. } catch (\Exception $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. }
  81. if ($count) {
  82. $this->success();
  83. }
  84. $this->error(__('没有记录被删除'));
  85. }
  86. private function beforeAction()
  87. {
  88. if(!$this->request->isPost()){
  89. $tree=AuthRule::getRuleListTree('*',true);
  90. $ruledata=array_merge(array([
  91. 'id'=>'0',
  92. 'title'=>__('无'),
  93. 'childlist'=>[]
  94. ]),$tree);
  95. $this->assign('ruledata',$ruledata);
  96. $this->assign('menutypeList',AuthRule::menutypeList);
  97. }else{
  98. $ismenu=$this->request->post('row.ismenu');
  99. $controller=$this->request->post('row.controller');
  100. if($controller && !class_exists($controller)){
  101. $this->error(__('控制器不存在'));
  102. }
  103. if($ismenu){
  104. $action=$this->request->post('row.action');
  105. if($action){
  106. if(!method_exists($controller,$action)){
  107. $this->error(__('方法%s不存在',['s'=>$action]));
  108. }
  109. }
  110. }else{
  111. $this->postParams['menutype']='';
  112. $this->postParams['icon']='';
  113. $this->postParams['extend']='';
  114. $this->postParams['status']='';
  115. $actions=$this->request->post('row.actions');
  116. if(!$actions){
  117. $this->error(__('请填写方法列表'));
  118. }
  119. $actions=json_decode(htmlspecialchars_decode($actions),true);
  120. $title=[];
  121. $action=[];
  122. foreach ($actions as $key=>$value){
  123. if(!method_exists($controller,$key)){
  124. $this->error(__('方法%s不存在',['s'=>$key]));
  125. }
  126. $action[]=$key;
  127. $title[]=$value;
  128. }
  129. $this->postParams['action']=json_encode($action,JSON_UNESCAPED_UNICODE);
  130. $this->postParams['title']=json_encode($title,JSON_UNESCAPED_UNICODE);
  131. }
  132. }
  133. }
  134. }