request()->employee->root_id])->select(); return ['code' => 0, 'data' => $list]; } public function add() { $grant = GrantModel::where(['root_id'=>request()->employee->root_id, 'type'=>'m'])->find(); if($grant == null) $grant = GrantModel::where(['root_id' => 0, 'type'=>'m'])->find(); $arr = [['id', 'in', (array)$grant->permission]]; $data = (new Permission())->getTree($arr, request()->employee->root_id); View::assign('data', $data); return View::fetch(); } public function add_grant() { $params = input(); $grant = GrantModel::where(['id' => request()->employee->grant_id])->find(); // $auth = Permission::where([['id', 'in', $params['auth']], ['id', 'in', (array)$grant->permission]])->column('id'); $auth = Permission::where([['id', 'in', $params['auth']]])->column('id'); $menu1 = Permission::where([['id', 'in', $auth], ['pid', '<>', 0]])->column('pid'); $menu2 = Permission::where([['id', 'in', $menu1], ['pid', '<>', 0]])->column('pid'); $ids = array_merge($auth, $menu1, $menu2); $ids = array_unique($ids); sort($ids); $data = [ 'permission' => array_values($ids), 'name' => $params['title'], 'root_id' => request()->employee->root_id ]; GrantModel::create($data); return json(['code' => 0, 'msg' => '权限组添加成功']); } public function edit() { $gant_id = input('grant_id'); $grant = GrantModel::where(['root_id'=>request()->employee->root_id, 'type'=>'m'])->find(); if($grant == null) $grant = GrantModel::where(['root_id' => 0, 'type'=>'m'])->find(); $arr = [['id', 'in', (array)$grant->permission]]; $data = (new Permission())->getTree($arr, request()->employee->root_id); View::assign('data', $data); $grant = GrantModel::where(['id' => $gant_id, 'root_id' => request()->employee->root_id])->find(); View::assign('permission', (array)$grant->permission); View::assign('grant_name', $grant->name); return View::fetch(); } public function edit_grant() { $params = input(); $grant = GrantModel::where(['id' => $params['grant_id'], 'root_id' => request()->employee->root_id])->find(); if (empty($grant)) return json(['code' => 1, 'msg' => '数据不存在']); $parent = GrantModel::where(['id' => request()->employee->grant_id])->find(); // 防止超出权限范围 // $auth = Permission::where([['id', 'in', $params['auth']], ['id', 'in', (array)$parent->permission]])->column('id'); $auth = Permission::where([['id', 'in', $params['auth']]])->column('id'); $menu1 = Permission::where([['id', 'in', $auth], ['pid', '<>', 0]])->column('pid'); $menu2 = Permission::where([['id', 'in', $menu1], ['pid', '<>', 0]])->column('pid'); $ids = array_merge($auth, $menu1, $menu2); $ids = array_unique($ids); sort($ids); $data = [ 'permission' => array_values($ids), 'name' => $params['title'] ]; $grant->save($data); return json(['code' => 0, 'msg' => '权限组修改成功']); } public function del($id) { $grant = GrantModel::where(['id' => $id, 'root_id' => request()->employee->root_id])->find(); if (empty($grant)) return json(['code' => 0, 'msg' => '权限组删除成功']); $use = Employee::where(['grant_id' => $id])->count(); if ($use) return json(['code' => 1, 'msg' => '权限组还在使用中,不可删除。']); $grant->delete(); return json(['code' => 0, 'msg' => '权限组删除成功']); } }