AuthGroup.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\service\AuthService;
  4. use app\BaseController;
  5. use app\common\model\DepartmentModel;
  6. use think\App;
  7. use think\facade\View;
  8. class AuthGroup extends BaseController
  9. {
  10. private $authService;
  11. private $departmentModel;
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->authService = new AuthService();
  16. $this->departmentModel = new DepartmentModel();
  17. }
  18. public function index() {
  19. return view();
  20. }
  21. /**
  22. * @return \think\response\Json|\think\response\View
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public function add() {
  28. $all_department = $this->departmentModel->findAllDepartment();
  29. if ($this->request->isAjax()) {
  30. $params = $this->request->param();
  31. $res = $this->authService->authGroupModel->save([
  32. 'pid' => 0,
  33. 'name' => format_string($params['name'] ?? null),
  34. 'rules' => format_string($params['rule_ids'] ?? null),
  35. "reception_rules" => isset($params["reception_rules"]) ? join(',', $params['reception_rules']) : null,
  36. "data_rules" => isset($params["data_rules"]) ? join(',', $params['data_rules']) : null,
  37. "department_id" => format_string($params['department_id'] ?? null),
  38. 'describe' => format_string($params['describe'] ?? null),
  39. ]);
  40. return $this->ok($res);
  41. }
  42. View::assign([
  43. "reception_rules" => fetchReceptionRules(),
  44. "all_department" => recursion($all_department, 0)
  45. ]);
  46. return view();
  47. }
  48. /**
  49. * @return \think\response\Json|\think\response\View
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. public function edit() {
  55. $params = $this->request->param();
  56. $id = $params['id'];
  57. $group = $this->authService->authGroupModel->findById($id);
  58. $all_department = $this->departmentModel->findAllDepartment();
  59. if ($this->request->isAjax()) {
  60. $res = $this->authService->authGroupModel->where('id', $params['id'])->save([
  61. 'pid' => 0,
  62. 'name' => $params['name'] ?? $group->name,
  63. 'rules' => !isset($params['rule_ids']) ? null : $params['rule_ids'],
  64. "reception_rules" => isset($params["reception_rules"]) ? join(',', $params['reception_rules']) : null,
  65. "data_rules" => isset($params["data_rules"]) ? join(',', $params['data_rules']) : null,
  66. "department_id" => format_string($params['department_id'] ?? null),
  67. 'describe' => $params['describe'] ?? $group->describe,
  68. 'update_time' => time()
  69. ]);
  70. return $this->ok($res);
  71. }
  72. $reception_rule_ids = $group->reception_rules ? explode(',',$group->reception_rules) : [];
  73. $reception_rules = array_merge([], fetchReceptionRules());
  74. foreach ($reception_rules as &$item) $item['isChecked'] = in_array($item['id'], $reception_rule_ids);
  75. $data_rule_ids = $group->data_rules ? explode(',', $group->data_rules) : [];
  76. $data_rules = array_merge([], fetchReceptionRules());
  77. foreach ($data_rules as &$item) $item['isChecked'] = in_array($item['id'], $data_rule_ids);
  78. View::assign([
  79. 'group' => $group,
  80. "reception_rules" => $reception_rules,
  81. "data_rules" => $data_rules,
  82. "all_department" => recursion($all_department, 0)
  83. ]);
  84. return view();
  85. }
  86. public function delete(\think\Request $request) {
  87. $params = $request->param();
  88. if(!isset($params['ids']))
  89. return $this->fail(lang("Please select the data you want to delete"));
  90. $this->authService->authGroupModel->deleteByIds(explode(',', $params['ids']));
  91. return $this->ok(true);
  92. }
  93. public function findAllAuthGroups() {
  94. $groups = $this->authService->authGroupModel->fetchAllGroups()->toArray();
  95. $format_groups = array_map(function ($data) {
  96. return [
  97. 'id' => $data['id'],
  98. 'title' => $data['name'],
  99. 'field' => $data['name'],
  100. 'spread' => true,
  101. 'checked' => false,
  102. 'disabled' => $data['id'] == 1
  103. ];
  104. }, $groups);
  105. return $this->ok($format_groups);
  106. }
  107. }