Department.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\AuthGroupModel;
  5. use app\common\model\DepartmentModel;
  6. use think\App;
  7. use think\facade\View;
  8. class Department extends BaseController
  9. {
  10. private $departmentModel;
  11. private $authGroupModel;
  12. public function __construct(App $app)
  13. {
  14. parent::__construct($app);
  15. $this->authGroupModel = new AuthGroupModel();
  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->fetchAllDepartment();
  29. // $all_group = $this->authGroupModel->fetchAllGroups();
  30. if ($this->request->isAjax()) {
  31. $params = $this->request->param();
  32. $db_department = $this->departmentModel->findByName(format_string($params['name'] ?? null));
  33. if ($db_department)
  34. return $this->fail('部门已经存在!');
  35. $department = [
  36. 'pid' => $params['pid'] ?? 0,
  37. 'name' => format_string($params['name'] ?? null),
  38. 'describe' => format_string($params['describe'] ?? null),
  39. ];
  40. // if($params['pid'] == 0) {
  41. // $auth_group_ids = array_merge($params['auth_group_ids'] ?? []);
  42. // $department['auth_group_ids'] = count($auth_group_ids) > 0 ? join(',', $auth_group_ids) : null;
  43. // }
  44. $res = $this->departmentModel->save($department);
  45. return $this->ok($res);
  46. }
  47. View::assign([
  48. "all_department" => recursion($all_department, 0),
  49. // "all_group" => $all_group
  50. ]);
  51. return view();
  52. }
  53. /**
  54. * @return \think\response\Json|\think\response\View
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. */
  59. public function edit() {
  60. $params = $this->request->param();
  61. $id = $params['id'];
  62. $all_department = $this->departmentModel->fetchAllDepartment();
  63. $all_group = $this->authGroupModel->fetchAllGroups();
  64. $department = $this->departmentModel->findById($id);
  65. if (!$department) {
  66. return $this->fail(lang("Data not exist"));
  67. }
  68. if ($this->request->isAjax()) {
  69. $res = $this->departmentModel->where('id', $params['id'])->save([
  70. 'pid' => $params['pid'] ?? $department->pid,
  71. 'name' => $params['name'] ?? $department->name,
  72. // "auth_group_ids" => isset($params["auth_group_ids"]) ? join(',', array_merge($params['auth_group_ids'])) : null,
  73. 'describe' => $params['describe'] ?? $department->describe,
  74. 'update_time' => time()
  75. ]);
  76. return $this->ok($res);
  77. }
  78. $group_ids = $department->auth_group_ids ? explode(',',$department->auth_group_ids) : [];
  79. foreach ($all_group as &$item) $item->isChecked = in_array($item->id, $group_ids);
  80. View::assign([
  81. 'department' => $department,
  82. "all_department" => recursion($all_department, 0),
  83. // "all_group" => $all_group
  84. ]);
  85. return view();
  86. }
  87. public function delete(\think\Request $request) {
  88. $params = $request->param();
  89. if(!isset($params['ids']))
  90. return $this->fail(lang("Please select the data you want to delete"));
  91. $this->departmentModel->deleteByIds(explode(',', $params['ids']));
  92. return $this->ok(true);
  93. }
  94. /**
  95. * @return \think\response\Json
  96. */
  97. public function fetchAllDepartment() {
  98. $groups = $this->departmentModel->fetchAllDepartment()->toArray();
  99. $format_groups = array_map(function ($data) {
  100. return [
  101. 'id' => $data['id'],
  102. 'title' => $data['name'],
  103. 'pid' => $data['pid'],
  104. 'field' => $data['name'],
  105. 'spread' => true,
  106. 'checked' => false,
  107. 'disabled' => $data['id'] == 5
  108. ];
  109. }, $groups);
  110. return $this->ok(recursion($format_groups));
  111. }
  112. }