Rule.php 6.1 KB

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