Rule.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. namespace app\admin\controller\user;
  3. use ba\Tree;
  4. use Throwable;
  5. use app\admin\model\UserRule;
  6. use app\admin\model\UserGroup;
  7. use app\common\controller\Backend;
  8. class Rule extends Backend
  9. {
  10. /**
  11. * @var object
  12. * @phpstan-var UserRule
  13. */
  14. protected object $model;
  15. /**
  16. * @var Tree
  17. */
  18. protected Tree $tree;
  19. protected array $noNeedLogin = ['index'];
  20. protected string|array $preExcludeFields = ['create_time', 'update_time'];
  21. protected string|array $quickSearchField = 'title';
  22. /**
  23. * 远程select初始化传值
  24. * @var array
  25. */
  26. protected array $initValue;
  27. /**
  28. * 是否组装Tree
  29. * @var bool
  30. */
  31. protected bool $assembleTree;
  32. /**
  33. * 搜索关键词
  34. * @var string
  35. */
  36. protected string $keyword;
  37. public function initialize(): void
  38. {
  39. parent::initialize();
  40. $this->model = new UserRule();
  41. $this->tree = Tree::instance();
  42. $isTree = $this->request->param('isTree', true);
  43. $this->initValue = $this->request->get("initValue/a", []);
  44. $this->initValue = array_filter($this->initValue);
  45. $this->keyword = $this->request->request('quickSearch', '');
  46. // 有初始化值时不组装树状(初始化出来的值更好看)
  47. $this->assembleTree = $isTree && !$this->initValue;
  48. }
  49. public function index(): void
  50. {
  51. if ($this->request->param('select')) {
  52. $this->select();
  53. }
  54. $this->success('', [
  55. 'list' => $this->getRules(),
  56. 'remark' => get_route_remark(),
  57. ]);
  58. }
  59. /**
  60. * 添加
  61. */
  62. public function add(): void
  63. {
  64. if ($this->request->isPost()) {
  65. $data = $this->request->post();
  66. if (!$data) {
  67. $this->error(__('Parameter %s can not be empty', ['']));
  68. }
  69. $data = $this->excludeFields($data);
  70. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  71. $data[$this->dataLimitField] = $this->auth->id;
  72. }
  73. $result = false;
  74. $this->model->startTrans();
  75. try {
  76. // 模型验证
  77. if ($this->modelValidate) {
  78. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  79. if (class_exists($validate)) {
  80. $validate = new $validate();
  81. if ($this->modelSceneValidate) $validate->scene('add');
  82. $validate->check($data);
  83. }
  84. }
  85. $result = $this->model->save($data);
  86. if (!empty($data['pid'])) {
  87. $groups = UserGroup::where('rules', '<>', '*')->select();
  88. foreach ($groups as $group) {
  89. $rules = explode(',', $group->rules);
  90. if (in_array($data['pid'], $rules) && !in_array($this->model->id, $rules)) {
  91. $rules[] = $this->model->id;
  92. $group->rules = implode(',', $rules);
  93. $group->save();
  94. }
  95. }
  96. }
  97. $this->model->commit();
  98. } catch (Throwable $e) {
  99. $this->model->rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($result !== false) {
  103. $this->success(__('Added successfully'));
  104. } else {
  105. $this->error(__('No rows were added'));
  106. }
  107. }
  108. $this->error(__('Parameter error'));
  109. }
  110. /**
  111. * 编辑
  112. * @throws Throwable
  113. */
  114. public function edit(): void
  115. {
  116. $id = $this->request->param($this->model->getPk());
  117. $row = $this->model->find($id);
  118. if (!$row) {
  119. $this->error(__('Record not found'));
  120. }
  121. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  122. if ($dataLimitAdminIds && !in_array($row[$this->dataLimitField], $dataLimitAdminIds)) {
  123. $this->error(__('You have no permission'));
  124. }
  125. if ($this->request->isPost()) {
  126. $data = $this->request->post();
  127. if (!$data) {
  128. $this->error(__('Parameter %s can not be empty', ['']));
  129. }
  130. $data = $this->excludeFields($data);
  131. $result = false;
  132. $this->model->startTrans();
  133. try {
  134. // 模型验证
  135. if ($this->modelValidate) {
  136. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  137. if (class_exists($validate)) {
  138. $validate = new $validate();
  139. if ($this->modelSceneValidate) $validate->scene('edit');
  140. $validate->check($data);
  141. }
  142. }
  143. if (isset($data['pid']) && $data['pid'] > 0) {
  144. // 满足意图并消除副作用
  145. $parent = $this->model->where('id', $data['pid'])->find();
  146. if ($parent['pid'] == $row['id']) {
  147. $parent->pid = 0;
  148. $parent->save();
  149. }
  150. }
  151. $result = $row->save($data);
  152. $this->model->commit();
  153. } catch (Throwable $e) {
  154. $this->model->rollback();
  155. $this->error($e->getMessage());
  156. }
  157. if ($result !== false) {
  158. $this->success(__('Update successful'));
  159. } else {
  160. $this->error(__('No rows updated'));
  161. }
  162. }
  163. $this->success('', [
  164. 'row' => $row
  165. ]);
  166. }
  167. /**
  168. * 删除
  169. * @param array $ids
  170. * @throws Throwable
  171. */
  172. public function del(array $ids = []): void
  173. {
  174. if (!$this->request->isDelete() || !$ids) {
  175. $this->error(__('Parameter error'));
  176. }
  177. // 子级元素检查
  178. $subData = $this->model->where('pid', 'in', $ids)->column('pid', 'id');
  179. foreach ($subData as $key => $subDatum) {
  180. if (!in_array($key, $ids)) {
  181. $this->error(__('Please delete the child element first, or use batch deletion'));
  182. }
  183. }
  184. parent::del($ids);
  185. }
  186. /**
  187. * 远程下拉
  188. * @throws Throwable
  189. */
  190. public function select(): void
  191. {
  192. $data = $this->getRules([['status', '=', '1']]);
  193. if ($this->assembleTree) {
  194. $data = $this->tree->assembleTree($this->tree->getTreeArray($data, 'title'));
  195. }
  196. $this->success('', [
  197. 'options' => $data
  198. ]);
  199. }
  200. /**
  201. * 获取菜单规则
  202. * @throws Throwable
  203. */
  204. public function getRules(array $where = []): array
  205. {
  206. $pk = $this->model->getPk();
  207. $initKey = $this->request->get("initKey/s", $pk);
  208. if ($this->keyword) {
  209. $keyword = explode(' ', $this->keyword);
  210. foreach ($keyword as $item) {
  211. $where[] = [$this->quickSearchField, 'like', '%' . $item . '%'];
  212. }
  213. }
  214. if ($this->initValue) {
  215. $where[] = [$initKey, 'in', $this->initValue];
  216. }
  217. $data = $this->model->where($where)->order('weigh desc,id asc')->select()->toArray();
  218. return $this->assembleTree ? $this->tree->assembleChild($data) : $data;
  219. }
  220. }