Group.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * @throws Throwable
  63. */
  64. public function edit(): void
  65. {
  66. $pk = $this->model->getPk();
  67. $id = $this->request->param($pk);
  68. $row = $this->model->find($id);
  69. if (!$row) {
  70. $this->error(__('Record not found'));
  71. }
  72. if ($this->request->isPost()) {
  73. $data = $this->request->post();
  74. if (!$data) {
  75. $this->error(__('Parameter %s can not be empty', ['']));
  76. }
  77. $data = $this->excludeFields($data);
  78. $data = $this->handleRules($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. $validate->scene('edit')->check($data);
  88. }
  89. }
  90. $result = $row->save($data);
  91. $this->model->commit();
  92. } catch (Throwable $e) {
  93. $this->model->rollback();
  94. $this->error($e->getMessage());
  95. }
  96. if ($result !== false) {
  97. $this->success(__('Update successful'));
  98. } else {
  99. $this->error(__('No rows updated'));
  100. }
  101. }
  102. // 读取所有pid,全部从节点数组移除,父级选择状态由子级决定
  103. $pidArr = UserRule::field('pid')
  104. ->distinct(true)
  105. ->where('id', 'in', $row->rules)
  106. ->select()
  107. ->toArray();
  108. $rules = $row->rules ? explode(',', $row->rules) : [];
  109. foreach ($pidArr as $item) {
  110. $ruKey = array_search($item['pid'], $rules);
  111. if ($ruKey !== false) {
  112. unset($rules[$ruKey]);
  113. }
  114. }
  115. $row->rules = array_values($rules);
  116. $this->success('', [
  117. 'row' => $row
  118. ]);
  119. }
  120. /**
  121. * 权限规则入库前处理
  122. * @param array $data 接受到的数据
  123. * @return array
  124. * @throws Throwable
  125. */
  126. private function handleRules(array &$data): array
  127. {
  128. if (is_array($data['rules']) && $data['rules']) {
  129. $rules = UserRule::select();
  130. $super = true;
  131. foreach ($rules as $rule) {
  132. if (!in_array($rule['id'], $data['rules'])) {
  133. $super = false;
  134. }
  135. }
  136. if ($super) {
  137. $data['rules'] = '*';
  138. } else {
  139. $data['rules'] = implode(',', $data['rules']);
  140. }
  141. } else {
  142. unset($data['rules']);
  143. }
  144. return $data;
  145. }
  146. }