Group.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace app\admin\controller\user;
  3. use Throwable;
  4. use app\admin\model\UserRule;
  5. use app\admin\model\UserGroup;
  6. use app\common\controller\Backend;
  7. class Group extends Backend
  8. {
  9. /**
  10. * @var object
  11. * @phpstan-var UserGroup
  12. */
  13. protected object $model;
  14. // 排除字段
  15. protected string|array $preExcludeFields = ['update_time', 'create_time'];
  16. protected string|array $quickSearchField = 'name';
  17. public function initialize(): void
  18. {
  19. parent::initialize();
  20. $this->model = new UserGroup();
  21. }
  22. /**
  23. * 添加
  24. * @throws Throwable
  25. */
  26. public function add(): void
  27. {
  28. if ($this->request->isPost()) {
  29. $data = $this->request->post();
  30. if (!$data) {
  31. $this->error(__('Parameter %s can not be empty', ['']));
  32. }
  33. $data = $this->excludeFields($data);
  34. $data = $this->handleRules($data);
  35. $result = false;
  36. $this->model->startTrans();
  37. try {
  38. // 模型验证
  39. if ($this->modelValidate) {
  40. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  41. if (class_exists($validate)) {
  42. $validate = new $validate();
  43. $validate->scene('add')->check($data);
  44. }
  45. }
  46. $result = $this->model->save($data);
  47. $this->model->commit();
  48. } catch (Throwable $e) {
  49. $this->model->rollback();
  50. $this->error($e->getMessage());
  51. }
  52. if ($result !== false) {
  53. $this->success(__('Added successfully'));
  54. } else {
  55. $this->error(__('No rows were added'));
  56. }
  57. }
  58. $this->error(__('Parameter error'));
  59. }
  60. /**
  61. * 编辑
  62. * @param string|int|null $id
  63. * @throws Throwable
  64. */
  65. public function edit(string|int $id = null): void
  66. {
  67. $row = $this->model->find($id);
  68. if (!$row) {
  69. $this->error(__('Record not found'));
  70. }
  71. if ($this->request->isPost()) {
  72. $data = $this->request->post();
  73. if (!$data) {
  74. $this->error(__('Parameter %s can not be empty', ['']));
  75. }
  76. $data = $this->excludeFields($data);
  77. $data = $this->handleRules($data);
  78. $result = false;
  79. $this->model->startTrans();
  80. try {
  81. // 模型验证
  82. if ($this->modelValidate) {
  83. $validate = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  84. if (class_exists($validate)) {
  85. $validate = new $validate();
  86. $validate->scene('edit')->check($data);
  87. }
  88. }
  89. $result = $row->save($data);
  90. $this->model->commit();
  91. } catch (Throwable $e) {
  92. $this->model->rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success(__('Update successful'));
  97. } else {
  98. $this->error(__('No rows updated'));
  99. }
  100. }
  101. // 读取所有pid,全部从节点数组移除,父级选择状态由子级决定
  102. $pidArr = UserRule::field('pid')
  103. ->distinct(true)
  104. ->where('id', 'in', $row->rules)
  105. ->select()
  106. ->toArray();
  107. $rules = $row->rules ? explode(',', $row->rules) : [];
  108. foreach ($pidArr as $item) {
  109. $ruKey = array_search($item['pid'], $rules);
  110. if ($ruKey !== false) {
  111. unset($rules[$ruKey]);
  112. }
  113. }
  114. $row->rules = array_values($rules);
  115. $this->success('', [
  116. 'row' => $row
  117. ]);
  118. }
  119. /**
  120. * 权限规则入库前处理
  121. * @param array $data 接受到的数据
  122. * @return array
  123. * @throws Throwable
  124. */
  125. public function handleRules(array &$data): array
  126. {
  127. if (is_array($data['rules']) && $data['rules']) {
  128. $rules = UserRule::select();
  129. $super = true;
  130. foreach ($rules as $rule) {
  131. if (!in_array($rule['id'], $data['rules'])) {
  132. $super = false;
  133. }
  134. }
  135. if ($super) {
  136. $data['rules'] = '*';
  137. } else {
  138. $data['rules'] = implode(',', $data['rules']);
  139. }
  140. } else {
  141. unset($data['rules']);
  142. }
  143. return $data;
  144. }
  145. }