| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- namespace app\admin\controller;
- use app\BaseController;
- use app\common\model\AuthGroupModel;
- use app\common\model\DepartmentModel;
- use think\App;
- use think\facade\View;
- class Department extends BaseController
- {
- private $departmentModel;
- private $authGroupModel;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->authGroupModel = new AuthGroupModel();
- $this->departmentModel = new DepartmentModel();
- }
- public function index() {
- return view();
- }
- /**
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function add() {
- $all_department = $this->departmentModel->fetchAllDepartment();
- // $all_group = $this->authGroupModel->fetchAllGroups();
- if ($this->request->isAjax()) {
- $params = $this->request->param();
- $db_department = $this->departmentModel->findByName(format_string($params['name'] ?? null));
- if ($db_department)
- return $this->fail('部门已经存在!');
- $department = [
- 'pid' => $params['pid'] ?? 0,
- 'name' => format_string($params['name'] ?? null),
- 'describe' => format_string($params['describe'] ?? null),
- ];
- // if($params['pid'] == 0) {
- // $auth_group_ids = array_merge($params['auth_group_ids'] ?? []);
- // $department['auth_group_ids'] = count($auth_group_ids) > 0 ? join(',', $auth_group_ids) : null;
- // }
- $res = $this->departmentModel->save($department);
- return $this->ok($res);
- }
- View::assign([
- "all_department" => recursion($all_department, 0),
- // "all_group" => $all_group
- ]);
- return view();
- }
- /**
- * @return \think\response\Json|\think\response\View
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function edit() {
- $params = $this->request->param();
- $id = $params['id'];
- $all_department = $this->departmentModel->fetchAllDepartment();
- $all_group = $this->authGroupModel->fetchAllGroups();
- $department = $this->departmentModel->findById($id);
- if (!$department) {
- return $this->fail(lang("Data not exist"));
- }
- if ($this->request->isAjax()) {
- $res = $this->departmentModel->where('id', $params['id'])->save([
- 'pid' => $params['pid'] ?? $department->pid,
- 'name' => $params['name'] ?? $department->name,
- // "auth_group_ids" => isset($params["auth_group_ids"]) ? join(',', array_merge($params['auth_group_ids'])) : null,
- 'describe' => $params['describe'] ?? $department->describe,
- 'update_time' => time()
- ]);
- return $this->ok($res);
- }
- $group_ids = $department->auth_group_ids ? explode(',',$department->auth_group_ids) : [];
- foreach ($all_group as &$item) $item->isChecked = in_array($item->id, $group_ids);
- View::assign([
- 'department' => $department,
- "all_department" => recursion($all_department, 0),
- // "all_group" => $all_group
- ]);
- return view();
- }
- public function delete(\think\Request $request) {
- $params = $request->param();
- if(!isset($params['ids']))
- return $this->fail(lang("Please select the data you want to delete"));
- $this->departmentModel->deleteByIds(explode(',', $params['ids']));
- return $this->ok(true);
- }
- /**
- * @return \think\response\Json
- */
- public function fetchAllDepartment() {
- $groups = $this->departmentModel->fetchAllDepartment()->toArray();
- $format_groups = array_map(function ($data) {
- return [
- 'id' => $data['id'],
- 'title' => $data['name'],
- 'pid' => $data['pid'],
- 'field' => $data['name'],
- 'spread' => true,
- 'checked' => false,
- 'disabled' => $data['id'] == 5
- ];
- }, $groups);
- return $this->ok(recursion($format_groups));
- }
- }
|