Group.php 4.8 KB

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